import javax.swing.JTable; public class MyTable { public static void main(String[] args) { JTable table = new JTable(new Object[][]{{"John", 25}, {"Mary", 30}}, new Object[]{"Name", "Age"}); int columnCount = table.getColumnCount(); System.out.println("The table has " + columnCount + " columns."); } }
import javax.swing.*; import java.awt.*; public class MyTable { public static void main(String[] args) { JFrame frame = new JFrame("MyTable Example"); JTable table = new JTable(new Object[][]{{"John", 25, "Male"}, {"Mary", 30, "Female"}}, new Object[]{"Name", "Age", "Gender"}); JLabel label = new JLabel("The table has " + table.getColumnCount() + " columns."); frame.getContentPane().add(table, BorderLayout.CENTER); frame.getContentPane().add(label, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } }In this example, we create a JTable with three columns (name, age, gender) and two rows with data. We then create a JFrame with the table and a label that displays the number of columns in the table. The frame is displayed with the table and label. The output should be "The table has 3 columns." Package library: javax.swing