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.
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