In this example we are going to see how to use Java JTable Example.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { final JFrame frame = new JFrame("JTable Demo"); String[] columns = { "Code", "Name", "High", "Low", "Close", "Volume", "Change", "Change %" }; Object[][] data = { { "MBF", "CITYGROUP", 10.16, 10.16, 10.16, 200, 0.08, 0.79 }, { "MBL", "BANK OF AMERICA", 12.66, 12.66, 12.66, 6600, 0.13, 1.04 }, { "MJP", "Morgan Stanley Dean Witter & Co.", 24.97, 24.97, 24.97, 1000, -0.04, -0.16 } }; JTable table = new JTable(data, columns); JScrollPane scrollPane = new JScrollPane(table); table.setFillsViewportHeight(true); JLabel lblHeading = new JLabel("Stock Quotes"); lblHeading.setFont(new Font("Arial", Font.TRUETYPE_FONT, 24)); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(lblHeading, BorderLayout.PAGE_START); frame.getContentPane().add(scrollPane, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(550, 200); frame.setVisible(true); } } |
Output: