import javax.swing.*; import java.awt.*; import java.util.*; public class JTableExample extends JFrame { private JTable table; private JScrollPane scrollPane; private Vector> data; private Vector columnNames; public JTableExample() { initializeData(); initializeUI(); } private void initializeData() { data = new Vector >(); columnNames = new Vector (); columnNames.add("Name"); columnNames.add("Age"); columnNames.add("Gender"); columnNames.add("Country"); // Add some data to the table Vector row1 = new Vector (); row1.add("John"); row1.add("25"); row1.add("Male"); row1.add("USA"); data.add(row1); Vector row2 = new Vector (); row2.add("Amy"); row2.add("30"); row2.add("Female"); row2.add("Canada"); data.add(row2); } private void initializeUI() { table = new JTable(data, columnNames); scrollPane = new JScrollPane(table); getContentPane().add(scrollPane); setSize(500, 300); setVisible(true); // Update the data dynamically data.get(0).set(1, "26"); table.revalidate(); } public static void main(String[] args) { JTableExample example = new JTableExample(); } }
import javax.swing.*; import java.awt.*; import java.util.*; public class JTableExample extends JFrame { private JTable table; private JScrollPane scrollPane; private VectorIn both examples, the package library used is javax.swing.> data; private Vector columnNames; public JTableExample() { initializeData(); initializeUI(); } private void initializeData() { data = new Vector >(); columnNames = new Vector (); columnNames.add("Name"); columnNames.add("Age"); columnNames.add("Gender"); columnNames.add("Country"); // Add some data to the table Vector row1 = new Vector (); row1.add("John"); row1.add("25"); row1.add("Male"); row1.add("USA"); data.add(row1); Vector row2 = new Vector (); row2.add("Amy"); row2.add("30"); row2.add("Female"); row2.add("Canada"); data.add(row2); } private void initializeUI() { table = new JTable(data, columnNames); scrollPane = new JScrollPane(table); getContentPane().add(scrollPane); setSize(500, 300); setVisible(true); // Add a new row to the table Vector newRow = new Vector (); newRow.add("Bob"); newRow.add("40"); newRow.add("Male"); newRow.add("UK"); data.add(newRow); table.revalidate(); } public static void main(String[] args) { JTableExample example = new JTableExample(); } }