How to make JContainer re-plan itself?

I have a JFrame with BorderLayout : JTextArea in NORTH and JButton in SOUTH . I pack() at the beginning.

My code resizes the font for the text area. How to force a dialog box and its components to reconfigure themselves?

So far I have tried several combinations:

  • more pack()
  • repaint()
  • revalidate()

It doesn't seem to help.

Is there a guaranteed brute force approach? What is the correct way to achieve this result?

UPDATE:

When creating SCCE (see below), I found two errors in my source code and fixed them. Now the frame becomes more convenient.

I still have a question if this is the correct way.

 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MyFrame extends JFrame implements ActionListener{ private JTextArea txt; private JButton bis; private JFrame frame; int size = 10; private void BuildMainGUI() { txt = new JTextArea("This is just a line of text"); bis = new JButton("Increase size"); JPanel p1 = new JPanel(); bis.addActionListener(this); BorderLayout bl = new BorderLayout(); p1.setLayout(bl); p1.add(txt, BorderLayout.NORTH); p1.add(bis, BorderLayout.SOUTH); frame = new JFrame(); frame.setContentPane(p1); frame.setVisible(true); frame.pack(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { size += 2; Font newFont = new Font("Courier", Font.PLAIN, size); txt.setFont(newFont); frame.revalidate(); frame.pack(); } /** * @param args */ public static void main(String[] args) { MyFrame myGUI = new MyFrame(); myGUI.BuildMainGUI(); } } 
+4
source share
1 answer

all three alternatives are described in ActionListener, it makes it easier to work with them, it seems that the coordinates are the same (I think that it takes the deepest look at TextLayout ???)

enter image description hereenter image description here

 import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; public class ResizeJTextArea { private JFrame frame = new JFrame(); private JScrollPane scrollPane = new JScrollPane(); private JTextArea textArea = new JTextArea(10, 15); private JButton button = new JButton("change"); private Font newFont = new Font("Courier", Font.PLAIN, 10); public ResizeJTextArea() { textArea.setText("This is just a line of text"); textArea.setFont(newFont); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textArea.setFont(textArea.getFont().deriveFont(20f)); //2. choice //textArea.setColumns(20); //textArea.setRows(20); //3rd. coice //override PreferredScrollableViewportSize frame.pack(); } }); scrollPane.setViewportView(textArea); frame.add(scrollPane); frame.add(button, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(100, 100); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ResizeJTextArea fs = new ResizeJTextArea(); } }); } } 
+3
source

All Articles