Search code examples
javaswingjpaneljtextareafont-size

Modifying the font size for all JPanels in an ArrayList<JPanel>


So, I have an ArrayList that contains JPanels; all the JPanels have a BorderLayout with a JPanel (that contains two JLabels) set on NORTH and a JTextArea (containing text, of course) set on CENTER. My question is how do I modify each JTextArea's font size?


Solution

  • Here is some simple code that allows the JTextArea font sizes to be set via a setFontSize(int index, int fontSize) method. Note that this will only work for the text areas in the panels array list. In the following example, I have changed the font on text areas #1 and #3 (see the main method for the calls which do this).

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.util.ArrayList;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    public class SimpleFrame extends JFrame {
       ArrayList<JPanel> panels = new ArrayList<JPanel>();
    
       public SimpleFrame() {
          super("Simple Panel List Example");
    
          JPanel content = (JPanel)getContentPane();
          content.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    
          // add some panels to the array list
          for(int i = 0; i < 5; i++) {
             BorderLayout b = new BorderLayout();
             JPanel p = new JPanel(b);
             JLabel north = new JLabel("Label #"+i);
             JTextArea center = new JTextArea("TextArea #"+i);
             p.add("North", north);
             p.add("Center", center);
    
             panels.add(p);
             content.add(p);
          }
       }
    
       // change the font size of the JTextArea on panel #i
       public void setFontSize(int i, int fontSize) {
          JPanel p = panels.get(i);
          JTextArea t = (JTextArea)((BorderLayout)p.getLayout()).getLayoutComponent("Center");
          Font f = t.getFont();
          Font f2 = f.deriveFont((float)fontSize);
          t.setFont(f2);
       }
    
       public static void main(String[] argv) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                SimpleFrame c = new SimpleFrame();
                c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                c.pack();
                c.setVisible(true);
    
                // we can change the font size using our setFontSize method
                c.setFontSize(1, 8);
                c.setFontSize(3, 16);
             }
          });
       }
    }