How to resize JLabel when font size changes?
I have a JLabel. The code for the JLabel looks like this.
panelmain = new JPanel();
panelmain.setLayout(null);
panelmain.setPreferredSize(new java.awt.Dimension(800, 600));
panelmain.addComponentListener(listen);
panelmain.setBorder(null);
titlebar = new JLabel("Hello World");
titlebar.setBounds(10, 10, 100, 30);
panelmain.add(titlebar);
My job is that if I change the font titlebar
(i.e. JLabel) then how do I change the size (which is already set in the code as titlebar.setBounds(10, 10, 100, 30);
) titlebar
?
Edit with Girish
My complete code looks like this.
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class IFrame extends JInternalFrame {
/**
*
*/
private static final long serialVersionUID = 6526561589695424088L;
private JScrollPane jsp;
private IFListen listen;
private JPanel panelmain;
protected JPanel panel;
private String title;
private JLabel titlebar;
public IFrame()
{
this.title="";
init();
}
public IFrame(String title)
{
this.title=title;
init();
}
private void init()
{
setLayout(null);
listen=new IFListen();
panelmain=new JPanel();
panelmain.setLayout(null);
panelmain.setPreferredSize(new java.awt.Dimension(800, 600));
panelmain.addComponentListener(listen);
panelmain.setBorder(null);
titlebar=new JLabel("Hello World");
titlebar.setFont(new java.awt.Font("Monotype Corsiva", 1, 48));
panelmain.add(titlebar);
panel=new JPanel();
panel.setBorder(javax.swing.BorderFactory.createTitledBorder(title));
panel.setMinimumSize(new java.awt.Dimension(400, 400));
panel.setSize(400, 400);
panelmain.add(panel);
jsp=new JScrollPane(panelmain);
jsp.setBorder(null);
add(jsp);
this.addComponentListener(listen);
}
//INFO Custom Methods
public void setTitleFont(java.awt.Font font)
{
titlebar.setFont(font); //Here I want to change size of label.
}
//INFO Listener Class for IFrame
private class IFListen implements ComponentListener
{
//INFO Overridden Methods
@Override
public void componentResized(ComponentEvent e)
{
if(e.getSource() instanceof IFrame)
jsp.setBounds(5, 5, getWidth()-20, getHeight()-20);
else if(e.getSource()==panelmain)
{
panel.setLocation(Integer.parseInt(panelmain.getWidth()/2-panel.getWidth()/2+""), 0);
}
}
//INFO Unimplemented Methods
@Override
public void componentShown(ComponentEvent arg0) {}
@Override
public void componentHidden(ComponentEvent arg0) {}
@Override
public void componentMoved(ComponentEvent arg0) {}
}
}
I have commented where the font size changes and I want to change the size of the jlabel.
source to share
Don't use setPreferredSize
, you've just removed all the calculations the label uses to calculate the size it would like to be.
Avoid using layouts null
, pixel perfect layouts are an illusion in modern ui design. There are too many factors that affect the individual size of components, none of which you can control. Swing was designed to work with layout managers in the kernel, discarding them so that it won't create problems and problems that you will spend more and more time fixing.
Use appropriate layouts. The important thing is that no layout will ever do everything you want. You will need to learn how to take advantage of each layout (and disadvantages) and use them to your advantage. These are what are commonly referred to as "compound layouts". See Exposing Components Inside a Container for more details and ideas.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label;
private JSlider slider;
public TestPane() {
label = new JLabel("Look, no hands!");
setLayout(new BorderLayout());
JPanel panel = new JPanel(new GridBagLayout());
panel.add(label);
add(panel);
slider = new JSlider(8, 96);
add(slider, BorderLayout.SOUTH);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
Font font = label.getFont();
font = font.deriveFont((float)slider.getValue());
label.setFont(font);
}
});
slider.setValue(8);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
}
JLabel
is an amazing pro, changing the font (text / icon) will automatically invalidate the layout and ask for redrawing on its own ...
source to share