Tables are useful for presenting data in a structured and organized manner. In Java AWT (Abstract Window Toolkit), you can create tables to display data using the Table
class. This allows you to handle various operations such as adding, removing, and editing table data.
Table Creation
To create a table in Java AWT, you need to follow these steps:
- Import the necessary classes:
import java.awt.*; import java.awt.event.*;
- Create a
Frame
object to hold the table:Frame frame = new Frame("Table Example");
- Create an array of
String
arrays to store the table data:String[][] data = { {"John", "Doe", "30"}, {"Jane", "Smith", "25"}, {"David", "Brown", "35"} };
- Create an array of
String
to specify the column names:String[] columns = {"First Name", "Last Name", "Age"};
- Create a
Table
object using the data and column names:Table table = new Table(data, columns);
- Set the size and layout of the table:
table.setBounds(30, 40, 200, 300); table.setLayout(new BorderLayout());
- Add the table to the frame and make it visible:
frame.add(table); frame.setSize(300, 400); frame.setLayout(null); frame.setVisible(true);
Table Operations
Once the table is created, you can perform various operations on it.
Adding Rows
You can add rows to the table using the addRow
method. For example:
table.addRow(new String[]{"Sarah", "Williams", "28"});
Removing Rows
To remove a row, you need to specify the index of the row you want to remove using the removeRow
method. For example, to remove the second row:
table.removeRow(1);
Editing Cells
You can edit the data in a specific cell using the setValueAt
method. For example, to change the age in the third row to “40”:
table.setValueAt("40", 2, 2);
Getting Selected Row
To get the selected row and its data, you can use the getSelectedRow
and getDataAtRow
methods. For example:
int selectedRow = table.getSelectedRow();
String[] rowData = table.getDataAtRow(selectedRow);
Conclusion
Working with tables in Java AWT provides a convenient way to display and manipulate tabular data. By following the steps outlined in this article, you can create tables, add or remove rows, edit cell values, and retrieve selected row data.