List of usage examples for javax.swing JTable JTable
public JTable(final Object[][] rowData, final Object[] columnNames)
JTable
to display the values in the two dimensional array, rowData
, with column names, columnNames
. From source file:MainClass.java
public static void main(String args[]) { String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } }; String headers[] = { "Upper", "Lower" }; final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN, JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF, JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS }; final JTable table = new JTable(rows, headers); JScrollPane scrollPane = new JScrollPane(table); String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column", "Resize Off", "Resize Subsequent Columns" }; final JComboBox resizeModeComboBox = new JComboBox(modes); int defaultMode = 4; table.setAutoResizeMode(modeKey[defaultMode]); resizeModeComboBox.setSelectedIndex(defaultMode); ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent e) { int index = resizeModeComboBox.getSelectedIndex(); table.setAutoResizeMode(modeKey[index]); }/* ww w.j a v a 2 s. c o m*/ }; resizeModeComboBox.addItemListener(itemListener); JFrame frame = new JFrame("Resizing Table"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(resizeModeComboBox, BorderLayout.NORTH); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTable table; String[] columnTitles = { "A", "B", "C", "D" }; Object[][] rowData = { { "11", "12", "13", "14" }, { "21", "22", "23", "24" }, { "31", "32", "33", "34" }, { "41", "42", "44", "44" } }; table = new JTable(rowData, columnTitles); table.setCellSelectionEnabled(true); ListSelectionModel cellSelectionModel = table.getSelectionModel(); cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); cellSelectionModel.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { String selectedData = null; int[] selectedRow = table.getSelectedRows(); int[] selectedColumns = table.getSelectedColumns(); for (int i = 0; i < selectedRow.length; i++) { for (int j = 0; j < selectedColumns.length; j++) { selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]); }/* w w w . j av a 2s . c o m*/ } System.out.println("Selected: " + selectedData); } }); frame.add(new JScrollPane(table)); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); Integer[][] board = { { 1, 1, 1, 1, 2, 0, 0, 0, 0 }, { 0, 0, 5, 0, 1, 0, 0, 2, 4 }, { 1, 0, 0, 4, 0, 0, 0, 3, 8 }, { 0, 0, 0, 6, 1, 0, 0, 3, 7 }, { 0, 0, 4, 5, 3, 8, 9, 4, 0 }, { 8, 0, 0, 0, 1, 7, 0, 4, 0 }, { 7, 4, 0, 0, 1, 6, 0, 3, 1 }, { 6, 1, 0, 0, 1, 0, 3, 3, 0 }, { 0, 0, 0, 0, 1, 1, 1, 1, 0 } }; String col[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; JTable table = new JTable(board, col); panel.add(table, BorderLayout.CENTER); frame.add(panel);//ww w. jav a 2 s . c o m frame.setSize(800, 500); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
From source file:Main.java
public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Vector<Vector<String>> rowData = new Vector<Vector<String>>(); Vector<String> columnName = new Vector<String>(Arrays.asList("Column 1")); for (int i = 0; i < 2000; i++) { rowData.add(new Vector<String>(Arrays.asList(Integer.toString(i)))); }//from www .j a va2 s . co m JTable table = new JTable(rowData, columnName); JScrollPane scrollPane = new JScrollPane(table); JScrollBar vertical = scrollPane.getVerticalScrollBar(); vertical.setPreferredSize(new Dimension(0, 0)); f.add(scrollPane); f.pack(); f.setVisible(true); JViewport view = scrollPane.getViewport(); Component[] components = view.getComponents(); for (int i1 = 0; i1 < components.length; i1++) { if (components[i1] instanceof JTable) { System.out.println("got"); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Object[][] data = { { "A", new Integer(3), new Double(7.23), new Boolean(true) }, { "J", new Integer(2), new Double(4.64), new Boolean(false) }, { "S", new Integer(1), new Double(8.81), new Boolean(true) } }; String[] columns = { "Col", "Col", "Col", "Col" }; JTable table = new JTable(data, columns); JScrollPane scroll = new JScrollPane(table); JFrame f = new JFrame(); f.setContentPane(scroll);//from w w w. ja va 2s . co m f.pack(); int x = (int) table.getTableHeader().getSize().getWidth(); int y = (int) table.getTableHeader().getSize().getHeight() + (int) table.getSize().getHeight(); BufferedImage bi = new BufferedImage((int) x, (int) y, BufferedImage.TYPE_INT_RGB); Graphics g = bi.createGraphics(); table.getTableHeader().paint(g); g.translate(0, table.getTableHeader().getHeight()); table.paint(g); g.dispose(); JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi))); ImageIO.write(bi, "png", new File("c:/Java_Dev/table.png")); System.exit(0); }
From source file:SimpleTableSample.java
public static void main(String args[]) { Object rows[][] = { { "one", "ichi - \u4E00" }, { "two", "ni - \u4E8C" }, { "three", "san - \u4E09" }, { "four", "shi - \u56DB" }, { "five", "go - \u4E94" }, { "six", "roku - \u516D" }, { "seven", "shichi - \u4E03" }, { "eight", "hachi - \u516B" }, { "nine", "kyu - \u4E5D" }, { "ten", "ju - \u5341" } }; Object headers[] = { "English", "Japanese" }; String title = (args.length == 0 ? "JTable Sample" : args[0]); JFrame frame = new JFrame(title); JTable table = new JTable(rows, headers); JScrollPane scrollPane = new JScrollPane(table); frame.getContentPane().add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150);// w ww . ja v a 2 s. c o m frame.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) { Object rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } }; Object headers[] = { "Upper", "Lower" }; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTable table = new JTable(rows, headers); TableColumnModelListener tableColumnModelListener = new TableColumnModelListener() { public void columnAdded(TableColumnModelEvent e) { System.out.println("Added"); }/*from ww w .ja v a 2s. c o m*/ public void columnMarginChanged(ChangeEvent e) { System.out.println("Margin"); } public void columnMoved(TableColumnModelEvent e) { System.out.println("Moved"); } public void columnRemoved(TableColumnModelEvent e) { System.out.println("Removed"); } public void columnSelectionChanged(ListSelectionEvent e) { System.out.println("Selection Changed"); } }; TableColumnModel columnModel = table.getColumnModel(); columnModel.addColumnModelListener(tableColumnModelListener); columnModel.setColumnMargin(12); TableColumn column = new TableColumn(1); columnModel.addColumn(column); JScrollPane pane = new JScrollPane(table); frame.add(pane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) { String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } }; String headers[] = { "Upper", "Lower" }; JFrame frame = new JFrame("Label Header"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTable table = new JTable(rows, headers); JScrollPane scrollPane = new JScrollPane(table); Border headerBorder = UIManager.getBorder("TableHeader.cellBorder"); JLabel headerLabel1 = new JLabel(headers[0], JLabel.CENTER); headerLabel1.setBorder(headerBorder); JLabel headerLabel2 = new JLabel(headers[1], JLabel.CENTER); headerLabel2.setBorder(headerBorder); TableCellRenderer renderer = new JComponentTableCellRenderer(); TableColumnModel columnModel = table.getColumnModel(); TableColumn column0 = columnModel.getColumn(0); TableColumn column1 = columnModel.getColumn(1); column0.setHeaderRenderer(renderer); column0.setHeaderValue(headerLabel1); column1.setHeaderRenderer(renderer); column1.setHeaderValue(headerLabel2); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150);// w w w . j av a 2 s .c om frame.setVisible(true); }
From source file:ColumnModelSample.java
public static void main(String args[]) { final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } }; final Object headers[] = { "English", "#" }; JFrame frame = new JFrame("Scrollless Table"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTable table = new JTable(rows, headers); TableColumnModelListener tableColumnModelListener = new TableColumnModelListener() { public void columnAdded(TableColumnModelEvent e) { System.out.println("Added"); }//from ww w.j a v a 2 s .c om public void columnMarginChanged(ChangeEvent e) { System.out.println("Margin"); } public void columnMoved(TableColumnModelEvent e) { System.out.println("Moved"); } public void columnRemoved(TableColumnModelEvent e) { System.out.println("Removed"); } public void columnSelectionChanged(ListSelectionEvent e) { System.out.println("Selection Changed"); } }; TableColumnModel columnModel = table.getColumnModel(); columnModel.addColumnModelListener(tableColumnModelListener); columnModel.setColumnMargin(12); TableColumn column = new TableColumn(1); columnModel.addColumn(column); JScrollPane pane = new JScrollPane(table); frame.add(pane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.setUI(new MetalTabbedPaneUI() { @Override// w w w .j a va 2 s .com protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) { int width = super.calculateTabWidth(tabPlacement, tabIndex, metrics); int extra = tabIndex * 50; return width + extra; } }); tabbedPane.addTab("JTable", new JScrollPane(new JTable(5, 5))); tabbedPane.addTab("JTree", new JScrollPane(new JTree())); tabbedPane.addTab("JSplitPane", new JSplitPane()); JPanel p = new JPanel(); p.add(tabbedPane); JFrame frame = new JFrame(); frame.setContentPane(p); frame.pack(); frame.setVisible(true); }