Java TableLayout
Asked Answered
W

1

9

Im currently wanting to construct a table type of layout for JPanels. I found out there is a TableLayout for Java but I don't how to import it. On the other hand i found out there is a GridBagLayOut which also can construct a table like layout.But it seems more complicated. Any advice.

Wine answered 7/5, 2012 at 4:52 Comment(3)
This doesn't strike me as specific enough to remain an open SO question ... to answer your question, persevere with GridBagLayout - it's not too complicated once you grok it.Ploce
Well I did some research on how to construct a table in java. And i came across TableLayout and GridBagLayout. TableLayout seems the way to go, but i couldnt really get it to work.Wine
What's your question? GridBagLayout should do the job perfectly as far as I understand.Invert
S
15

Here is an SSCCE of using a TableLayout, (Introduction to TableLayout)

import javax.swing.JButton;
import javax.swing.JFrame;
import layout.TableLayout;

public class TestTableLayout {

    public static void main(String args[]) {

        JFrame frame = new JFrame("Example of TableLayout");
        frame.setSize(450, 450);

        double size[][] = {{10, 75, 75, 75, 75, 75, 10}, // Columns
            {10, 75, 75, 75, 75, 75, 10}}; // Rows

        frame.setLayout(new TableLayout(size));


        String label[] = {"(1,1)", "(1,5)", "(1,3)", "(5,3)", "(3,3)"};
        JButton button[] = new JButton[label.length];

        for (int i = 0; i < label.length; i++) {
            button[i] = new JButton(label[i]);
        }


        frame.add(button[0], "1, 1");
        frame.add(button[1], "1, 5");
        frame.add(button[2], "1, 3");
        frame.add(button[3], "5, 3");
        frame.add(button[4], "3, 3");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

The required jar for the TableLayout can be downloaded from here


Also have a look at : A Visual Guide to Layout Managers ,In case.


In case you go for GridBagLayout, have a look at : How to Use GridBagLayout

Shelve answered 7/5, 2012 at 6:24 Comment(2)
Thank you very much for you time and effort. Really appreciate it. It helped me alotWine
Is it possible to customize alignment of objects in cells of this table layout (for example, center objects inside of cells)?Galop

© 2022 - 2024 — McMap. All rights reserved.