Question

I'm trying to create a side menu that has a small button at the top, two different size tables beneath it (the first one above the second), and then two JLabels beneath that (again, the first JLabel being on top of the other).

<Button>
<Table1>
<Table2>
<Label1>
<Label2>

The problem I am running into is that whatever layout I use, one of the components will run into some sort of trouble, e.g. tables messing up, button expanding to fill the whole line, being unable to move the JLabel. What layout should I use for the menu I desire?

Here is a copy of my current code:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import javax.swing.ButtonGroup;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;

public class Hello{
final Object[][] data = new Object[7][6];
String[] columnNames = {"Player", "Taxi", "Bus", "Undrgrd", "Secret", "Double"};

String[] mrXcolumns = {"Move","Transport"};
final Object[][] mrXdata = new Object[24][2];

final JFrame w = new JFrame();

int whosGo;
int roundNumber;

public static void main(String[] args) {
    Hello hello = new Hello();
    hello.run();
}

@SuppressWarnings("static-access")
public void run()
    {
        JPanel sidepanel = new JPanel(new BorderLayout());
        JButton newgame = new JButton("New Game");          
        JTable ticketTable = new JTable( data, columnNames );
        JScrollPane ticketView = new JScrollPane( ticketTable );
        ticketView.setBounds(25,50,300,150);

        JTable mrXTable = new JTable(mrXdata, mrXcolumns);
        JScrollPane moveView = new JScrollPane( mrXTable );
        moveView.setBounds(25,210,300,400);

        JLabel turn = new JLabel("Detective "+whosGo+"'s go");
        JLabel round = new JLabel("ROUND: " +roundNumber);

        sidepanel.setBounds(1018,0,382,809);
        sidepanel.setBackground(Color.GREEN);
        sidepanel.add(newgame);
        sidepanel.add(ticketView);
        sidepanel.add(moveView);
        sidepanel.add(turn);
        sidepanel.add(round);

        w.add(sidepanel);
        w.setDefaultCloseOperation(w.EXIT_ON_CLOSE);
        w.setPreferredSize(new Dimension(1400,809));
        w.setLayout(null);
        w.pack();
        w.setLocationByPlatform(true);
        w.setVisible(true);
    }

}

Was it helpful?

Solution

There are a number of possible ways to achieve this depending on the desired result.

You Could...

Use a GridLayout which will allow you to add components into a single column. The downside to this is the width of each component will be the width of the parent container and each component will be given an equal amount of the height (divided amongst the components);

sidepanel.setLayout(new GridLayout(0, 1));
sidepanel.add(newgame);
sidepanel.add(ticketView);
sidepanel.add(moveView);
sidepanel.add(turn);
sidepanel.add(round);

You Could...

Use a GridBagLayout which will allow you to control how components fill there column

sidepanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// One component per row...
gbc.gridwidth = GridBagConstraints.REMAINDER;
// Fill the available column space, this does not mean
// fill to fit the available parent container width though
gbc.fill = GridBagConstraints.HORIZONTAL;
sidepanel.add(newgame, gbc);
sidepanel.add(ticketView, gbc);
sidepanel.add(moveView, gbc);
sidepanel.add(turn, gbc);
sidepanel.add(round, gbc);

The drawback to this is is that, by default, the GridBagLayout will align all it's components to the center position of the container. There are tricks you can employ to change this, but it's a little messy

You Could...

Use the VertcialLayout from SwingX libraries

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top