Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make JFrame resize automatically to display all buttons
    text
    copied!<p>I have a simple swing application which consists of a JLabel and three buttons. The three buttons are in their own JPanel which is in a JFrame along with the JLabel. The JPanel uses flowlayout manager to arrange the buttons horizontally and the JFrame uses the BorderLayout manager to arrange the JLabel and JPanel vertically.</p> <p>My problem is when I launch the application, during the course of use the text on one of the buttons changes which increases its width. However, the window doesn't resize to accomdate this and one of the buttons disappears. I thought about calling pack() again, but the JFrame is a local variable in my constructor, also, I shouldn't have to tell my program to resize, right? I haven't been able to find anything on google or here to help me but there must be a simple solution, what am I missing? Code is below.</p> <pre><code> playButton = new JButton("Play"); pauseButton = new JButton("Pause"); stopButton = new JButton("Stop"); curTrackLabel = new JLabel("No Track Selected"); JFrame myFrame = new JFrame("MediaPlayer"); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setTitle("MediaPlayer"); myFrame.setLocation(400,300); JPanel topPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); myFrame.add(topPanel); JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(playButton); buttonPanel.add(pauseButton); buttonPanel.add(stopButton); topPanel.add(buttonPanel, BorderLayout.CENTER); topPanel.add(curTrackLabel, BorderLayout.NORTH); playButton.addActionListener(new playButtonHandler()); pauseButton.addActionListener(new pauseButtonHandler()); stopButton.addActionListener(new stopButtonHandler()); myFrame.pack(); myFrame.setVisible(true); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload