📄 tabledialogeditdemo.java
字号:
package boco.test ;import javax.swing.JTable;import javax.swing.table.AbstractTableModel;import javax.swing.DefaultCellEditor;import javax.swing.table.TableCellRenderer;import javax.swing.JLabel;import javax.swing.JDialog;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JColorChooser;import javax.swing.BorderFactory;import javax.swing.border.Border;import javax.swing.JScrollPane;import javax.swing.JFrame;import javax.swing.SwingUtilities;import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * This is like TableEditDemo, except that it substitutes a * Favorite Color column for the Last Name column and specifies * a custom cell renderer and editor for the color data. */public class TableDialogEditDemo extends JFrame { private boolean DEBUG = false; public TableDialogEditDemo() { super("TableDialogEditDemo"); MyTableModel myModel = new MyTableModel(); JTable table = new JTable(myModel); ImageIcon icon=new ImageIcon("/image/img4.gif"); table.setValueAt(icon,1,1) ; table.setPreferredScrollableViewportSize(new Dimension(500, 70)); //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Set up renderer and editor for the Favorite Color column. setUpColorRenderer(table); //setUpColorEditor(table); //Set up real input validation for integer data. //setUpIntegerEditor(table); //Add the scroll pane to this window. getContentPane().add(scrollPane, BorderLayout.CENTER); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } class ColorRenderer extends JLabel implements TableCellRenderer { Border unselectedBorder = null; Border selectedBorder = null; boolean isBordered = true; public ColorRenderer(boolean isBordered) { super(); this.isBordered = isBordered; setOpaque(true); //MUST do this for background to show up. } public Component getTableCellRendererComponent( JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) { setBackground((Color)color); if (isBordered) { if (isSelected) { if (selectedBorder == null) { selectedBorder = BorderFactory.createMatteBorder(2,5,2,5, table.getSelectionBackground()); } setBorder(selectedBorder); } else { if (unselectedBorder == null) { unselectedBorder = BorderFactory.createMatteBorder(2,5,2,5, table.getBackground()); } setBorder(unselectedBorder); } } return this; } } private void setUpColorRenderer(JTable table) { table.setDefaultRenderer(Color.class, new ColorRenderer(true)); } /* * The editor button that brings up the dialog. * We extend DefaultCellEditor for convenience, * even though it mean we have to create a dummy * check box. Another approach would be to copy * the implementation of TableCellEditor methods * from the source code for DefaultCellEditor. */ class MyTableModel extends AbstractTableModel { final String[] columnNames = {"First Name", "Favorite Color", "Sport", "# of Years", "Vegetarian"}; final Object[][] data = { {"Mary", new ImageIcon("/image/img4.gif"), "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", new Color(51, 51, 153), "Rowing", new Integer(3), new Boolean(true)}, {"Kathy",Color.red, "Chasing toddlers", new Integer(2), new Boolean(false)}, {"Mark", Color.blue, "Speed reading", new Integer(20), new Boolean(true)}, {"Philip", Color.pink, "Pool", new Integer(7), new Boolean(false)} }; public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { return data[row][col]; } /* * JTable uses this method to determine the default renderer/ * editor for each cell. If we didn't implement this method, * then the last column would contain text ("true"/"false"), * rather than a check box. */ public Class getColumnClass(int c) { //return getValueAt(0, c).getClass(); return ImageIcon.class; } } public static void main(String[] args) { TableDialogEditDemo frame = new TableDialogEditDemo(); frame.pack(); frame.setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -