javaswingjbuttonsetbounds

Changing the Bounds of a Swing GUI and Naming of the JButtons


I know how to set the the bounds, so in the end a new setbounds() call would give the new bounds, but I dont know how long/wide should the new bound be, it depends on the input number of buttons like here for example :

import java.awt.BorderLayout;
import java.awt.Color; 
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.EmptyBorder;

public class Book_GUI extends JFrame {

private EconomyClass eco;
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Book_GUI frame = new Book_GUI();
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Book_GUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    //contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
    //this.add(contentPane);
    JButton btnBookFlight;

    //eco = new EconomyClass();
    //eco.setSeats(5);
    for(int i=0;i<45;i++){
    btnBookFlight = new JButton("Book" +i);
        btnBookFlight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JButton button = (JButton)arg0.getSource();;
                button.setBackground(Color.RED);
                button.setOpaque(true);
            }
        });
        btnBookFlight.setBounds(77, 351, 100, 23);
        contentPane.add(btnBookFlight);
    }       
}

}

As you see the last 5 buttons are not visible, one has to enlarge the GUI a little bit with mouse... and first 10 buttons are smaller than others because after 9 the number digits increase which is logical but can I align all of them at the same order and size? Another issue, the button name "Book" is just for test it should be 1A Window, 1B Middle, 1C Aisle some space 1D Aisle,1E Middle,1F Middle, 1G Aisle some space 1H Aisle, 1I Middle, 1J Window and below these 2A Window... Just like in a plane, any hints how I can arrange the namings and the necessary space between them is highly appreciated!


Solution

  • You should avoid using null layout or absolute positioning for arranging swing components. Always use the best appropriate layout manager in the situation since it has a lot of advantages. The best layout to handle your current situation is GridLayout

    Here is the modified version of your code using GridLayout

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JToggleButton;
    import javax.swing.border.EmptyBorder;
    
    public class Book_GUI extends JFrame {
    
        // private EconomyClass eco;
        private JPanel contentPane;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Book_GUI frame = new Book_GUI();
                        frame.setVisible(true);
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public Book_GUI() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // setBounds(100, 100, 450, 300);
    
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new GridLayout(9, 5));
            // contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);
            // this.add(contentPane);
            JButton btnBookFlight;
    
            // eco = new EconomyClass();
            // eco.setSeats(5);
            for (int i = 0; i < 45; i++) {
                btnBookFlight = new JButton("Book" + i);
                btnBookFlight.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        JButton button = (JButton) arg0.getSource();
                        ;
                        button.setBackground(Color.RED);
                        button.setOpaque(true);
                    }
                });
                // btnBookFlight.setBounds(77, 351, 100, 23);
                contentPane.add(btnBookFlight);
            }
            pack();
        }
    
    }
    

    Further read : A Visual Guide to Layout Managers