2

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!

Anarkie
  • 657
  • 3
  • 19
  • 46
  • why are you not using some layout manager to add buttons properly? any specific reason why you are using setBounds() method? – voidMainReturn Jun 17 '13 at 10:25
  • @tejas This is my second program implementing GUIs so I use Eclipse Window Builder plugin to do some work for me already ... and setBounds() was in the auto generated code, another way I dont know... – Anarkie Jun 17 '13 at 15:35

2 Answers2

5

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

Bnrdo
  • 5,325
  • 3
  • 35
  • 63
  • 2
    *"You should avoid using null layout or absolute positioning"* +1 for that line alone (sorry, did not carefully read the rest). For the benefit of the OP. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space. – Andrew Thompson Jun 17 '13 at 11:06
  • Thanks to you now I understand GridLayout better, and it arranges rows and columns in the beginning thus I have an ugly view when I add some labels: http://www.copypastecode.com/640484/ is there a way I can make it look nicer?Economy Class label should come on top of JButtons and be there alone? – Anarkie Jun 17 '13 at 17:26
  • 1
    See the [Nested Layout Example](http://stackoverflow.com/a/5630271/418556) for ideas about how to *combine* layouts to create the required layout. – Andrew Thompson Jun 17 '13 at 17:39
  • Mr. Thompson is right. You should use nested layout to achieve what you want. I would use `BorderLayout` and put the Economy Class label at the `BorderLayout.PAGE_START` section of the main panel. Then the buttons in the `BorderLayout.CENTER` section. See [How to Use BorderLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html) – Bnrdo Jun 18 '13 at 01:08
1

for assign dynamically names to a collection of JButton, you can using this:

List<JButton> listOfButtons = new ArrayList<JButton>(collection.size());
       for (int i=0; i < collection.size(); i++) {
              JButton button = new JButton();
              listOfButtons.add(button);
}