How to Resize a Component in Java?
hi everyone,
Last time you guys helped me a lot while solving the java error. So I thought instead of wasting time on searching I should directly post to you guys.!! Now I want a smaller version of a component to place on a tool palette or tool bar, which is causing me a problem. I have tried lot of things but nothing worked. Please tell me how to Resize a Component in Java? Hoping that I will get sooner response.!!:rolleyes:
Re: How to Resize a Component in Java?
Many times there is a condition where you want to create a smaller version of a component so that it can fit on a tool palette or tool bar. By setting a client property on the component, you can resize a component. The following three sizes are supported along with the regular :
- Mini
- Small
- Large
Resizable components are most often used when creating charts, diagrams and similar.
Re: How to Resize a Component in Java?
You should keep in mind that the one component that does not support the size variants property is JLabel. But for doing that, you have another option. You will have to change the size of a label by changing the size of its font. Before the component is displayed, you can set the size of a component with one line of code. The following code would help you to understand this :
Code:
myButton.putClientProperty("JComponent.sizeVariant", "mini");
In the above code, you will have to change the size according to your need.
Re: How to Resize a Component in Java?
Many times it happens that even after setting the size variants property correctly, the components are displayed in its original size. When you are facing such things, then you will have to force an update to the UI. For doing this, you will have to invoke the SwingUtilities.updateComponentTreeUI(Component) method before the window is displayed. The following sample of a code may help you in that case :
Code:
JFrame frame = ...;
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();
frame.setVisible(true);
Re: How to Resize a Component in Java?
You need a panel in which absolute positioning should be enabled. The following coding may help you for the re-sizable component :
Code:
package resizablecomponent;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class ResizableComponentTrial extends JFrame {
private JPanel panel = new JPanel(null);
private Resizable resizer;
public ResizableComponentTrial() {
add(panel);
JPanel area = new JPanel();
area.setBackground(Color.gray);
resizer = new Resizable(area);
resizer.setBounds(70, 80, 180, 160);
panel.add(resizer);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(370, 280));
setTitle("Resizable Component");
setLocationRelativeTo(null);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
requestFocus();
resizer.repaint();
}
});
}
public static void main(String[] args) {
ResizableComponent rc = new ResizableComponent();
rc.setVisible(true);
}
}