How to change the font size for all private JLabels

I saw similar questions like this , but in my case I have 25+ private JLabels that I give value when I declare it. I use GridBagLayout in my constructor to add these JLabels to JPanel. If I use something in accordance with the answers provided in the link, this will only change the JLabels that were added to the constructor.

So, it seems to me that if I want to change the font size of my personal JLabels, I will either have to determine the font size for them separately, or change the default value in my constructor, and then add my JLabels. Nevertheless, I am sure that there should be a different, simpler way, but I simply do not have the experience or understanding to understand this, or make the correct corrections from the answers to other similar questions.

I don't think it really requires me to show my code, but if that helps, I can do it.

public class LaborRecordPanel implements
Printable, ActionListener {

private Color shade = new Color(201,201,201);

private ImageIcon logoIcon = new ImageIcon("Images/fici_logo1.jpg");
private JLabel logoLabel = new JLabel(logoIcon);

private JLabel authorizedBy = new JLabel("AUTHORIZED BY:");
private JLabel toCertify = new JLabel("THIS IS TO CERTIFY THAT THE ABOVE LABOR HAS BEEN PERFORMED.");

private JLabel laborRecordNO = new JLabel("NO.");
private JLabel nameOfJob = new JLabel("NAME OF JOB:");
private JLabel customerPO = new JLabel("CUSTOMER PO #:");
private JLabel contractNO = new JLabel("CONTRACT NO.");
private JLabel weekEnding = new JLabel("WEEK ENDING");
private JComboBox laborRateDescription = new JComboBox();
//...

JPanel rp = new JPanel();
JScrollPane scrollPane = new JScrollPane(rp);
LaborRecordPanel(){
    //Font oldLabelFont = UIManager.getFont("Label.font");
    //UIManager.put("Label.font",oldLabelFont.deriveFont(Font.PLAIN));
    //UIManager.put("Label.font", UIManager.getFont("Label.font").deriveFont(40));
    //SwingUtilities.updateComponentTreeUI(rp);
    rp.setPreferredSize(new Dimension(1295,1830 ));
    scrollPane.getVerticalScrollBar().setUnitIncrement(16); //increase the scroll speed

    GridBagLayout gridbag = new GridBagLayout();
    rp.setBackground(Color.WHITE);
    rp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    GridBagConstraints gbc = new GridBagConstraints();
    rp.setLayout(gridbag);
    //gbc.insets = new Insets(5, 5, 5, 5);

    //row 0////////////////////////////////////////////////////////////
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridx = 10;
    gbc.gridy = 0;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    laborRecordNO.setHorizontalAlignment(JLabel.CENTER);
    laborRecordNO.setFont(new Font("Dialog", Font.PLAIN, 18));
    gridbag.setConstraints(laborRecordNO, gbc);
    rp.add(laborRecordNO, gbc);

    //row 1////////////////////////////////////////////////////////////
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 13;
    gridbag.setConstraints(logoLabel, gbc);
    rp.add(logoLabel, gbc);

    //row 2////////////////////////////////////////////////////////////
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gridbag.setConstraints(nameOfJob, gbc);
    rp.add(nameOfJob, gbc);

    gbc.gridx = 6;
    gbc.gridy = 2;
    gbc.gridheight = 1;
    gbc.gridwidth = 3;
    gridbag.setConstraints(contractNO, gbc);
    rp.add(contractNO, gbc);
 //... more setting constraints and adding
 }
 }
+3
source share
3 answers

UIManager . , deriveFont() . EventQueue.invokeLater(), .

Font f = (Font) UIManager.get("Label.font");
UIManager.put("Label.font", f.deriveFont(36f));
EventQueue.invokeLater(new Runnable() {…}
+6

JLabels GUI, - factory.

private JLabel createLabel(String text, float points) {
  JLabel label = new JLabel(text);
  label.setFont(label.getFont().deriveFont(points));
  return label;
}

, , List<JLabel> GUI , for.

+3

, JLable :

!

setUIFont(new FontUIResource(new Font("Arial", 0, 20)));

Arial , 20 !

+1

All Articles