Studyon Minte9.com
Java SE




Table



Table create

package testexample; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.table.DefaultTableModel; public class Test extends JFrame { public static void main(String[] args) { Test frame = new Test(); frame.setBounds(200, 200, 600, 400); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.setVisible(true); } public Test() { initMenu(); } public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu menuOpen = new JMenu("Open"); JMenuItem menuOpenItem = new JMenuItem("Table"); menuBar.add(menuOpen); menuOpen.add(menuOpenItem); setJMenuBar(menuBar); // --- Look here --- // menuOpenItem.addActionListener(new MenuAction(new Action1())); } private class MenuAction implements ActionListener { private JPanel panel; private MenuAction(JPanel panel) { this.panel = panel; } @Override public void actionPerformed(ActionEvent e) { changePanel(panel); } } public void changePanel(JPanel panel) { getContentPane().removeAll(); getContentPane().add(panel, BorderLayout.CENTER); getContentPane().doLayout(); validate(); repaint(); } } class Action1 extends JPanel { public Action1() { // --- Look here --- // // table model Object[][] data = new Object[][] { }; String[] columns = new String[] {"Total", "Max" }; DefaultTableModel tableModel = new DefaultTableModel(data, columns); JTable table = new JTable(tableModel); // table add rows tableModel.addRow(new Object[] {10,20}); tableModel.addRow(new Object[] {15,25}); // table dimensions and scrollPane (to show columns) table.setPreferredScrollableViewportSize(new Dimension(280, 100)); table.setFillsViewportHeight(true); JScrollPane scrollPane = new JScrollPane(table); this.add(scrollPane); } }
  Table cell color
package testexample; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; public class Test extends JFrame { public static void main(String[] args) { //setLookAndFill(); Test frame = new Test(); frame.setBounds(200, 200, 600, 400); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.setVisible(true); } public Test() { initMenu(); } public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu menuOpen = new JMenu("Open"); JMenuItem menuOpenItem = new JMenuItem("Table"); menuBar.add(menuOpen); menuOpen.add(menuOpenItem); setJMenuBar(menuBar); menuOpenItem.addActionListener(new MenuAction(new Action1())); } private class MenuAction implements ActionListener { private JPanel panel; private MenuAction(JPanel panel) { this.panel = panel; } @Override public void actionPerformed(ActionEvent e) { changePanel(panel); } } public void changePanel(JPanel panel) { getContentPane().removeAll(); getContentPane().add(panel, BorderLayout.CENTER); getContentPane().doLayout(); validate(); repaint(); } } class Action1 extends JPanel { public Action1() { // table model Object[][] data = new Object[][] { }; String[] columns = new String[] {"Total", "Max" }; DefaultTableModel tableModel = new DefaultTableModel(data, columns); JTable table = new JTable(tableModel); // table add rows tableModel.addRow(new Object[] {10,20}); tableModel.addRow(new Object[] {15,25}); tableModel.addRow(new Object[] {11,22}); tableModel.addRow(new Object[] {33,44}); // table dimensions and scrollPane (to show columns) table.setPreferredScrollableViewportSize(new Dimension(280, 100)); table.setFillsViewportHeight(true); table.setGridColor(Color.decode("#CCCCCC")); // --- Look here --- // table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer(0, 3)); // col / row table.getColumnModel().getColumn(1).setCellRenderer(new CustomRenderer(1, 2)); JScrollPane scrollPane = new JScrollPane(table); this.add(scrollPane); } } // --- Look here --- // class CustomRenderer extends DefaultTableCellRenderer { int col; int row; public CustomRenderer (int col, int row) { this.col = col; this.row = row; } @Override public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); setForeground( (column == this.col && row == this.row) ? Color.red : Color.black); setBackground( (column == this.col && row == this.row) ? Color.decode("#FBEFF5") : Color.white); return c; } }