3

I have a GUI, from this GUI I choose an input file with the dimensions of a Booking system(new GUI) If the plane has 100 seats the GUI will be for example 10x10, if the plane has 200 seats it will be 10x20, all the seats are going to be buttons, and should store passenger information on them if they are booked.Also a booked seat shall not be booked again.

The question is the dimensions of the GUI will change according to the seat numbers and orientation, which means in one version there can be lots of buttons on another less, also more seats means the GUI will be longer or wider maybe 10 cm x 10 cm or 10 cm x 20 cm, or 15 cm x 25 cm...

I am thinking to do this with a for loop but I dont want to put buttons out of the GUI.I dont know how can I add a button next to the other button arbitrarily. I always used Eclipse Window Builder for building GUIs but I guess I need to write this one on my own... can someone help me?Some hints?Thanks for your time!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Anarkie
  • 657
  • 3
  • 19
  • 46

2 Answers2

2

Start with a GridLayout. Each time you need to change the seat layout, reapply the GridLayout.

Ie, if you need 100 seats, use something like, new GridLayout(20, 10)...

Personally, I'd use a GridBagLayout, but it might be to complex for the task at hand

Its a little more difficult to add/rmove new content. I would, personally, rebuild the UI and reapply the model to it.

As for the size, you may not have much control over it, as it will appear differently on different screens. Instead, you should provide means by which you can prevent the UI from over sizing the screen.

This is achievable by placing the seating pane within a JScrollPane. This will allow the seating pane to expand and shrink in size without it potentially over sizing on the screen

Take a look at

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

Try something like this:

// The size of the window.
Rectangle bounds = this.getBounds();

// How many rows of buttons.
int numOfRows = 100;

// How many buttons per row.
int numOfColumns = 50;

int buttonWidth = bounds.width / numOfRows;
int buttonHeight = bounds.height / numOfColumns;

int x = 0;
int y = 0;

for(int i = 0; i < numOfColumns; i++){
    for(int j = 0; j < numOfRows; j++){
        // Make a button
        button.setBounds(x, y, buttonWidth, buttonHeight);
        y += buttonHeight;
    }
    x += buttonWidth;
}

This will make all the buttons fit inside of your window.

Here's a link on rectangles, they come in pretty handy when doing things like this. http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fgraphics%2FRectangle.html

Also, you can still use windowbuilder, infact I recommend you do. It really helps you to visualize the dimensions you want. AND you can also create stuff (buttons, lists, dropdowns... etc) manually with code and, assuming you put the variables where WindowBuilder would, it will still display them in the 'design' tab.

Hope this helps!

EDIT: I was kinda vague on how to make buttons using a loop

To make buttons with a loop you will need a buffer (to store a temporary button long enough to add to a list) and... a list :D.

This is how it should look:

Outside loop:

// The list to store your buttons in.
ArrayList<Button> buttons = new ArrayList<Button>();

// The empty button to use as a buffer.
Button button;

Inside loop (where the '//Make a button' comment is):

button = new Button(this, SWT.NONE);
buttons.add(button);

Now you have a list of all the buttons, and can easily access them and make changes such as change the buttons text like so;

buttons.get(indexOfButton).setText("SomeText");

EDIT:

Seeing as you're new to swt (and I couldn't get awt/JFrame to work) here's the full code.

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ButtonTest extends Shell {

    /**
     * Launch the application.
     * @param args
     */

    // Code starts here in main.
    public static void main(String args[]) {
        try {
            Display display = Display.getDefault();
            // Creates a window (shell) called "shell"
            ButtonTest shell = new ButtonTest(display);
            // These two lines start the shell.
            shell.open();
            shell.layout();

            // Now we can start adding stuff to our shell.

            // The size of the window.
            Rectangle bounds = shell.getBounds();

            // How many rows of buttons.
            int numOfRows = 5;

            // How many buttons per row.
            int numOfColumns = 3;

            int buttonWidth = bounds.width / numOfRows;
            int buttonHeight = bounds.height / numOfColumns;

        Button button;

        int x = 0;
        int y = 0;

        for(int i = 0; i < numOfColumns; i++){
            for(int j = 0; j < numOfRows; j++){
                button = new Button(shell, SWT.NONE);
                button.setBounds(x, y, buttonWidth, buttonHeight);
                x += buttonWidth;
            }
            x = 0;
            y += buttonHeight;
        }



        // This loop keeps the shell from killing itself the moment it's opened
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Create the shell.
 * @param display
 */
public ButtonTest(Display display) {
    super(display, SWT.SHELL_TRIM);
    createContents();
}

/**
 * Create contents of the shell.
 */
protected void createContents() {
    setText("SWT Application");
    setSize(800, 800);

}

@Override
protected void checkSubclass() {
    // Disable the check that prevents subclassing of SWT components
}

Also, I made a little typo in my nested loop (and forgot to reset the x value to 0 after each row). It's fixed in this edit.

Also you will need to import swt for this to work. Go here: http://www.eclipse.org/swt/ and download the latest version for your operating system, then go in eclipse, right click your project > Build Path > configure Build Path > Libraries tab > Add external JAR's > find the swt file you downloaded and click.

Sorry for such a long answer, hope it helps :D

Austin
  • 170
  • 1
  • 7
  • Yes Window Builder is a great plugin and helping me visualize things but in Window Builder I put all the buttons manually by myself, lets say 20 buttons I put myself ... but I cant do this always... it needs to be automazied like the for loop you gave, I was also thinking something similar to this, thanks, Im still trying and gonna update :) – Anarkie Jun 09 '13 at 21:13
  • http://pastecode.org/index.php/view/13730698 This is what I could come up with, probably I am doing something wrong with the bounds and in Main calling the GUI wrongly :/ – Anarkie Jun 09 '13 at 22:02
  • 1
    AHA, change "public class GUI_Rec {" to "public class GUI_Rec extends JFrame{. That fixes the problem with calling the GUI. (I think there's still more to fix, lemme see) – Austin Jun 09 '13 at 22:15
  • well I can use swt as well whatever works :) when I googled it said class Rectangle is in awt lib, I added the `extends Jframe` and commented in `frame.setVisible(true);`, upper left corner a mini window opened but without any buttons – Anarkie Jun 09 '13 at 22:22
  • 1
    To add the buttons in awt you need to do add(button) to add it to the frame. However when I do this weeeeird stuff happens... so seeing as swt is fine too, I'll make a quick example for that. – Austin Jun 09 '13 at 22:28
  • Updated my answer, should work for you now. And yes, Rectangle is an awt lib. However, swt has one too :D (awt and swt are VERY similar, I just happen to like swt more :D) – Austin Jun 09 '13 at 22:44
  • I'm downloading swt :) millions of thanks for all the time you have spent! – Anarkie Jun 09 '13 at 22:49