1

I want all of my buttons to be the same size. The one with text "Open Save" is the widest, so I want to use that one as the metric for resizing the other buttons. However, when I have this line of code in the program, the button won't display: newButton.setPreferredSize (openSaveButton.getSize ());

Here's the full program so far:

public class Project1 implements ActionListener {

    // The frames
    private JFrame mainMenu = new JFrame("Main Menu");
    private JPanel mainPanel = new JPanel();
    private JFrame game = new JFrame("Game");
    private JPanel gamePanel = new JPanel();
    // Controls
    private JButton newButton = new JButton("New");
    private JButton openSaveButton = new JButton("Open Save");
    private JButton exitButton = new JButton("Exit");
    private JTextArea textArea = new JTextArea();

    public static void main(String[] args) {
        new Project1();
    }

// Constructors
    public Project1() {

        mainPanel.setLayout(new GridBagLayout());
        addItem(mainPanel, newButton, 0, 0, 1, 1, GridBagConstraints.CENTER);
        newButton.addActionListener(this);
        newButton.setPreferredSize(openSaveButton.getSize());
        addItem(mainPanel, openSaveButton, 0, 1, 1, 1, GridBagConstraints.CENTER);
        openSaveButton.addActionListener(this);
        addItem(mainPanel, exitButton, 0, 2, 1, 1, GridBagConstraints.CENTER);
        exitButton.addActionListener(this);

        mainMenu.add(mainPanel, BorderLayout.NORTH);
        openFrame(mainMenu, 10, 10, JFrame.EXIT_ON_CLOSE);

        gamePanel.setLayout(new GridBagLayout());
        addItem(gamePanel, textArea, 0, 0, 1, 1, GridBagConstraints.CENTER);
        openFrame(game, 200, 10, JFrame.DO_NOTHING_ON_CLOSE);

    }

// Setters
// Getters
// ActionListener override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == newButton) {
            newGame();
        } else if (e.getSource() == openSaveButton) {
            openFile();
        } else if (e.getSource() == exitButton) {
            System.exit(0);
        }
    }

// Other functions and procedures
    private void openFrame(JFrame what, int x, int y, int operation) {

        what.pack();
        what.setLocation(x, y);
        what.setVisible(true);
        what.setResizable(false);
        what.setDefaultCloseOperation(operation);
    }

    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height,
                    int align) {

        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(5, 5, 5, 5);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
    }

    private void newGame() {

    }

    private void openFile() {

    }

}

Here are two screen shots of what happens, without the line and with:

without resizingwith resizing

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
failure
  • 215
  • 1
  • 3
  • 12

1 Answers1

1

However, when I have this line of code in the program, the button won't display: newButton.setPreferredSize (openSaveButton.getSize ());

Then, don't do this.

When you call openSaveButton.getSize(), openSaveButton has no size, it's 0x0, so now you're telling the layout manager the newButton should have a preferred size of 0x0, which it's kindly applying and now you button is no longer visible.

Now, if I surmise correctly with what you're trying to do, try chaning gc.fill = GridBagConstraints.NONE; to gc.fill = GridBagConstraints.HORIZONTAL; in your addItem method.

See How to Use GridBagLayout for more details

You may also want to take a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?, The Use of Multiple JFrames, Good/Bad Practice? and Initial Threads

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I did try putting the resize command after openSaveButton was placed. Is it still size 0x0? When do its dimensions get set? – failure Aug 25 '15 at 01:54
  • 1
    First, just don't, don't, don't do this. A container will be laid out when it's first realised on the screen or when it's been marked as invalid (needing layout). So, you now have a catch 22 problem. You can't set the size of the component until the container is laid out, but the layout can't occur until the component's sizing hints are set ... as I said, use the layout constraints instead – MadProgrammer Aug 25 '15 at 01:57
  • So following some of what you're saying, if dimensions don't get applied until they appear on screen, I went ahead and put the lines after the frames are invoked. This worked just fine. I'll look into layout constraints some other time, now I just need to finish the project. – failure Aug 26 '15 at 00:45
  • Beware, if for any reason the container is invalidated, the components WILL be reset to the layouts determined sizes – MadProgrammer Aug 26 '15 at 00:52