I've a JLabel. The code for JLabel is as follows.
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 questing is that if I change font of titlebar
(i.e. JLabel), then how to change size (which is already set in code as titlebar.setBounds(10, 10, 100, 30);
) of titlebar
?
Edit by Girish
My full code is as below.
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've commented where the font size is changed and I want to change size of jlabel.
Don't use setPreferredSize
, you've just removed all the calculations that the label uses to calculate the size it would like to be.
Avoid using null
layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
Make use of appropriate layouts. The important thing here is, no one layout will ever do everything you want. You will need to learn to take advantage of the each layout's strengths (and weaknesses) and use them to your advantage. This is what is commonly known as "compound layouts". Have a look at Laying Out Components Within 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 surprising proficient, changing the font (text/icon) will automatically cause it to invalidate the layout and request a repaint all by itself...