This discussion is archived
1 Reply Latest reply: Jun 9, 2010 11:44 AM by 843807 RSS

Re-sizing JButtons

843807 Newbie
Currently Being Moderated
Having some problems with my JDialog Box. I like the size of the JDialog Box and the JTextArea, but I don't want the JButton's so big. Is there any way to re-size them. I have tried the setPreferredSize method, but it doesn't do anything. I am using GridBoxLayout.
public class CustomDialog extends JDialog implements ActionListener {
       
        private JPanel panel1 = null;
        private JPanel panel2 = null;
        private JPanel mainPanel = null;
        private JTextArea txtDatabaseSettings = null;
        private JButton cmdSave = null;
        private JButton cmdCancel = null;
    
        private boolean answer = false;
        
        public boolean getAnswer() { return answer; }

    public CustomDialog(JFrame frame, boolean modal, String myMessage) {
        super(frame, modal);

        txtDatabaseSettings = new JTextArea();
                
        cmdSave = new JButton("Save");
        cmdSave.setHorizontalAlignment(SwingConstants.CENTER);
        cmdSave.addActionListener(this);
        
        cmdCancel = new JButton("Cancel");
        cmdCancel.setHorizontalAlignment(SwingConstants.CENTER);
        cmdCancel.addActionListener(this);
          
        panel1 = new JPanel();
        panel1.setLayout(new GridLayout(1, 1));
        panel1.add(new JScrollPane(txtDatabaseSettings));
        
        panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1, 2));
        panel2.add(cmdSave);
        panel2.add(cmdCancel);
        
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(2, 1));
        mainPanel.setPreferredSize(new Dimension(350, 150));
        getContentPane().add(mainPanel);
        
        mainPanel.add(panel1);
        mainPanel.add(panel2);
     
        setTitle(myMessage);
        pack();
        setLocationRelativeTo(frame);
        setVisible(true);
    }
Any help would be appreciated.

Thanks!
  • 1. Re: Re-sizing JButtons
    843807 Newbie
    Currently Being Moderated
    GridLayout is one the layouts that mostly ignores the preferred size. In GridLayout, a component is the same size as the grid cell. So you need to use a different layout.

    For example, you can use BoxLayout. Add the buttons to a horizontal box. Then add this horizontal box and the text field to a vertical box. Finally add the vertical box to the dialog.

    Or perhaps BorderLayout? Add the textfield to the CENTER. Then add the buttons to a panel that's placed in the SOUTH.