| 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.Color; import java.awt.event.*; import javax.swing.table.AbstractTableModel; // Class implements three different types of listener. public class TableExample { public Container createContentPane() { // The data used as the titles for the table. String[] title = {"No.", "Country", "Player", "Position"}; // The data used in the table, placed as a multi-dimensional array. Object[][] playerdata = { {new Integer(4), "United States", "Sterling Davis" , "Forward"}, {new Integer(6), "Germany", "Moritz Wohlers", "Forward/Centre"}, {new Integer(7), "United Kingdom", "Ross Hutton", "Centre"}, {new Integer(8), "Belgium", "Hugo Sterk", "Guard"}, {new Integer(10), "United Kingdom", "Andy Pearson", "Forward"}, {new Integer(11), "United States", "Robert Yanders", "Guard"}, {new Integer(12), "United Kingdom", "Graham Hunter", "Guard"}, {new Integer(14), "United Kingdom", "Julius Joseph", "Guard/Forward"}, {new Integer(15), "United Kingdom", "Gareth Murray", "Forward"}, {new Integer(21), "United States", "Maurice Hampton", "Guard"}}; // Table instantiated using the two sets of data. JTable table = new JTable(playerdata, title); // The table displayed in a Scrollpane. JScrollPane scrollPane = new JScrollPane(table); scrollPane.setPreferredSize(new Dimension(500, 150)); JPanel totalGUI = new JPanel(); totalGUI.add(scrollPane); totalGUI.setOpaque(true); return totalGUI; } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("[=] JTable [=]"); //Create and set up the ContentPane TableExample demo = new TableExample(); frame.setContentPane(demo.createContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } |
| 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | String[] title = {"No.", "Country", "Player", "Position"}; // The data used in the table, placed as a multi-dimensional array. Object[][] playerdata = { {new Integer(4), "United States", "Sterling Davis" , "Forward"}, {new Integer(6), "Germany", "Moritz Wohlers", "Forward/Centre"}, {new Integer(7), "United Kingdom", "Ross Hutton", "Centre"}, {new Integer(8), "Belgium", "Hugo Sterk", "Guard"}, {new Integer(10), "United Kingdom", "Andy Pearson", "Forward"}, {new Integer(11), "United States", "Robert Yanders", "Guard"}, {new Integer(12), "United Kingdom", "Graham Hunter", "Guard"}, {new Integer(14), "United Kingdom", "Julius Joseph", "Guard/Forward"}, {new Integer(15), "United Kingdom", "Gareth Murray", "Forward"}, {new Integer(21), "United States", "Maurice Hampton", "Guard"}}; |
| 31 | JTable table = new JTable(playerdata, title); |
| 34 35 | JScrollPane scrollPane = new JScrollPane(table); scrollPane.setPreferredSize(new Dimension(500, 150)); |
