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.
android.widget.TableLayout. A layout that arranges its children into rows and columns. A TableLayout consists of a number of TableRow objects, each defining a row (actually, you can have other children, which will be explained below). TableLayout containers do not display border lines for their rows, columns, or cells.
Android TableLayout going to be arranged groups of views into rows and columns. You will use the <TableRow> element to build a row in the table. Each row has zero or more cells; each cell can hold one View object. TableLayout containers do not display border lines for their rows, columns, or cells.
TableLayout is a ViewGroup that displays child View elements in rows and columns. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout. TableLayout positions its children into rows and columns.
android.widget.TableRow. A layout that arranges its children horizontally. A TableRow should always be used as a child of a TableLayout . If a TableRow's parent is not a TableLayout, the TableRow will behave as an horizontal LinearLayout .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With