Question

I have some tables which should draw from right to left and top to bottom at the frame. Right now I used absolute layout and working with coordination. Is there any BoxLayout or any other Java layout can do it? I should mention that the number of tables is dynamic.

My second question is how can I dock these tables to frame? I mean I want when the frame resize, tables keep their positions on the screen.

Was it helpful?

Solution

Most layout managers will respect the orientation of the component:

panel.setComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT );
panel.add(...);

Or you can always just add the components to the beginning of the container

panel.add(component1, 0);
panel.add(component2, 0);

OTHER TIPS

You may want to use a grid if you are arranging things into a table. All of the elements in a grid should be the same size.

To arrange some items vertically where the size of each row can vary, try this:

static JPanel buildPanel() {
    JPanel vPanel = new JPanel();
    BoxLayout layout = new BoxLayout(vPanel, BoxLayout.Y_AXIS);
    vPanel.setLayout(layout);
    JPanel[] rowPanels = new JPanel[5];
    int counter=1;
    for (int i = 0; i < rowPanels.length; i++) {
        rowPanels[i] = new JPanel();
        rowPanels[i].setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
        rowPanels[i].add(new JButton("button " + counter++));
        rowPanels[i].add(new JButton("Your button " + counter++));
        rowPanels[i].add(new JButton("Shabutton"));
        vPanel.add(rowPanels[i]);
    }
    return vPanel;
}

public static void main(String[] args) {
    JFrame gridFrame = new JFrame();
    gridFrame.add(buildPanel() );
    gridFrame.pack();
    gridFrame.setVisible(true);
}

You can prevent the whole JFrame from resizing using gridFrame.setResizable(false); You can prevent sapce from being added between the rows when the window is resized with a method call like this:

rowPanels[i].setMaximumSize(new Dimentsion(400,32));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top