tabledemo.java
来自「一个小公司要求给写的很简单的任务管理系统。」· Java 代码 · 共 751 行 · 第 1/3 页
JAVA
751 行
final Object[][] data = { {"Mike", "Albers", green, getString("TableDemo.brazil"), new Double(44.0), strawberry}, {"Mark", "Andrews", blue, getString("TableDemo.curse"), new Double(3), grapes}, {"Brian", "Beck", black, getString("TableDemo.bluesbros"), new Double(2.7182818285), raspberry}, {"Lara", "Bunni", red, getString("TableDemo.airplane"), new Double(15), strawberry}, {"Roger", "Brinkley", blue, getString("TableDemo.man"), new Double(13), peach}, {"Brent", "Christian", black, getString("TableDemo.bladerunner"), new Double(23), broccoli}, {"Mark", "Davidson", darkgreen, getString("TableDemo.brazil"), new Double(27), asparagus}, {"Jeff", "Dinkins", blue, getString("TableDemo.ladyvanishes"), new Double(8), kiwi}, {"Ewan", "Dinkins", yellow, getString("TableDemo.bugs"), new Double(2), strawberry}, {"Amy", "Fowler", violet, getString("TableDemo.reservoir"), new Double(3), raspberry}, {"Hania", "Gajewska", purple, getString("TableDemo.jules"), new Double(5), raspberry}, {"David", "Geary", blue, getString("TableDemo.pulpfiction"), new Double(3), watermelon},// {"James", "Gosling", pink, getString("TableDemo.tennis"), new Double(21), donut}, {"Eric", "Hawkes", blue, getString("TableDemo.bladerunner"), new Double(.693), pickle}, {"Shannon", "Hickey", green, getString("TableDemo.shawshank"), new Double(2), grapes}, {"Earl", "Johnson", green, getString("TableDemo.pulpfiction"), new Double(8), carrot}, {"Robi", "Khan", green, getString("TableDemo.goodfellas"), new Double(89), apple}, {"Robert", "Kim", blue, getString("TableDemo.mohicans"), new Double(655321), strawberry}, {"Janet", "Koenig", turquoise, getString("TableDemo.lonestar"), new Double(7), peach}, {"Jeff", "Kesselman", blue, getString("TableDemo.stuntman"), new Double(17), pineapple}, {"Onno", "Kluyt", orange, getString("TableDemo.oncewest"), new Double(8), broccoli}, {"Peter", "Korn", sunpurple, getString("TableDemo.musicman"), new Double(12), sparegrass}, {"Rick", "Levenson", black, getString("TableDemo.harold"), new Double(1327), raspberry}, {"Brian", "Lichtenwalter", jfcblue, getString("TableDemo.fifthelement"), new Double(22), pear}, {"Malini", "Minasandram", beige, getString("TableDemo.joyluck"), new Double(9), corn}, {"Michael", "Martak", green, getString("TableDemo.city"), new Double(3), strawberry}, {"David", "Mendenhall", forestgreen, getString("TableDemo.schindlerslist"), new Double(7), peach}, {"Phil", "Milne", suspectpink, getString("TableDemo.withnail"), new Double(3), banana}, {"Lynn", "Monsanto", cybergreen, getString("TableDemo.dasboot"), new Double(52), peach}, {"Hans", "Muller", rustred, getString("TableDemo.eraserhead"), new Double(0), pineapple}, {"Joshua", "Outwater", blue, getString("TableDemo.labyrinth"), new Double(3), pineapple}, {"Tim", "Prinzing", blue, getString("TableDemo.firstsight"), new Double(69), pepper}, {"Raj", "Premkumar", jfcblue2, getString("TableDemo.none"), new Double(7), broccoli}, {"Howard", "Rosen", green, getString("TableDemo.defending"), new Double(7), strawberry}, {"Ray", "Ryan", black, getString("TableDemo.buckaroo"), new Double(3.141592653589793238462643383279502884197169399375105820974944), banana}, {"Georges", "Saab", aqua, getString("TableDemo.bicycle"), new Double(290), cantaloupe}, {"Tom", "Santos", blue, getString("TableDemo.spinaltap"), new Double(241), pepper}, {"Rich", "Schiavi", blue, getString("TableDemo.repoman"), new Double(0xFF), pepper}, {"Nancy", "Schorr", green, getString("TableDemo.fifthelement"), new Double(47), watermelon}, {"Keith", "Sprochi", darkgreen, getString("TableDemo.2001"), new Double(13), watermelon}, {"Matt", "Tucker", eblue, getString("TableDemo.starwars"), new Double(2), broccoli}, {"Dmitri", "Trembovetski", red, getString("TableDemo.aliens"), new Double(222), tomato}, {"Scott", "Violet", violet, getString("TableDemo.raiders"), new Double(-97), banana}, {"Kathy", "Walrath", darkgreen, getString("TableDemo.thinman"), new Double(8), pear}, {"Nathan", "Walrath", black, getString("TableDemo.chusingura"), new Double(3), grapefruit}, {"Steve", "Wilson", green, getString("TableDemo.raiders"), new Double(7), onion}, {"Kathleen", "Zelony", gray, getString("TableDemo.dog"), new Double(13), grapes} }; // Create a model of the data. TableModel dataModel = new AbstractTableModel() { public int getColumnCount() { return names.length; } public int getRowCount() { return data.length;} public Object getValueAt(int row, int col) {return data[row][col];} public String getColumnName(int column) {return names[column];} public Class getColumnClass(int c) {return getValueAt(0, c).getClass();} public boolean isCellEditable(int row, int col) {return col != 5;} public void setValueAt(Object aValue, int row, int column) { data[row][column] = aValue; } }; // Create the table tableView = new JTable(dataModel); TableRowSorter sorter = new TableRowSorter(dataModel); tableView.setRowSorter(sorter); // Show colors by rendering them in their own color. DefaultTableCellRenderer colorRenderer = new DefaultTableCellRenderer() { public void setValue(Object value) { if (value instanceof NamedColor) { NamedColor c = (NamedColor) value; setBackground(c); setForeground(c.getTextColor()); setText(c.toString()); } else { super.setValue(value); } } }; // Create a combo box to show that you can use one in a table. JComboBox comboBox = new JComboBox(); comboBox.addItem(aqua); comboBox.addItem(beige); comboBox.addItem(black); comboBox.addItem(blue); comboBox.addItem(eblue); comboBox.addItem(jfcblue); comboBox.addItem(jfcblue2); comboBox.addItem(cybergreen); comboBox.addItem(darkgreen); comboBox.addItem(forestgreen); comboBox.addItem(gray); comboBox.addItem(green); comboBox.addItem(orange); comboBox.addItem(purple); comboBox.addItem(red); comboBox.addItem(rustred); comboBox.addItem(sunpurple); comboBox.addItem(suspectpink); comboBox.addItem(turquoise); comboBox.addItem(violet); comboBox.addItem(yellow); TableColumn colorColumn = tableView.getColumn(getString("TableDemo.favorite_color")); // Use the combo box as the editor in the "Favorite Color" column. colorColumn.setCellEditor(new DefaultCellEditor(comboBox)); colorRenderer.setHorizontalAlignment(JLabel.CENTER); colorColumn.setCellRenderer(colorRenderer); tableView.setRowHeight(INITIAL_ROWHEIGHT); scrollpane = new JScrollPane(tableView); return scrollpane; } private void printTable() { MessageFormat headerFmt; MessageFormat footerFmt; JTable.PrintMode printMode = fitWidth.isSelected() ? JTable.PrintMode.FIT_WIDTH : JTable.PrintMode.NORMAL; String text; text = headerTextField.getText(); if (text != null && text.length() > 0) { headerFmt = new MessageFormat(text); } else { headerFmt = null; } text = footerTextField.getText(); if (text != null && text.length() > 0) { footerFmt = new MessageFormat(text); } else { footerFmt = null; } try { boolean status = tableView.print(printMode, headerFmt, footerFmt); if (status) { JOptionPane.showMessageDialog(tableView.getParent(), getString("TableDemo.printingComplete"), getString("TableDemo.printingResult"), JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(tableView.getParent(), getString("TableDemo.printingCancelled"), getString("TableDemo.printingResult"), JOptionPane.INFORMATION_MESSAGE); } } catch (PrinterException pe) { String errorMessage = MessageFormat.format(getString("TableDemo.printingFailed"), new Object[] {pe.getMessage()}); JOptionPane.showMessageDialog(tableView.getParent(), errorMessage, getString("TableDemo.printingResult"), JOptionPane.ERROR_MESSAGE); } catch (SecurityException se) { String errorMessage = MessageFormat.format(getString("TableDemo.printingFailed"), new Object[] {se.getMessage()}); JOptionPane.showMessageDialog(tableView.getParent(), errorMessage, getString("TableDemo.printingResult"), JOptionPane.ERROR_MESSAGE); } } class NamedColor extends Color { String name; public NamedColor(Color color, String name) { super(color.getRGB()); this.name = name; } public Color getTextColor() { int r = getRed(); int g = getGreen(); int b = getBlue(); if(r > 240 || g > 240) { return Color.black; } else { return Color.white; } } public String toString() { return name; } } class ColumnLayout implements LayoutManager { int xInset = 5; int yInset = 5; int yGap = 2; public void addLayoutComponent(String s, Component c) {} public void layoutContainer(Container c) { Insets insets = c.getInsets(); int height = yInset + insets.top; Component[] children = c.getComponents(); Dimension compSize = null; for (int i = 0; i < children.length; i++) { compSize = children[i].getPreferredSize(); children[i].setSize(compSize.width, compSize.height); children[i].setLocation( xInset + insets.left, height); height += compSize.height + yGap; } } public Dimension minimumLayoutSize(Container c) { Insets insets = c.getInsets(); int height = yInset + insets.top; int width = 0 + insets.left + insets.right; Component[] children = c.getComponents(); Dimension compSize = null; for (int i = 0; i < children.length; i++) { compSize = children[i].getPreferredSize(); height += compSize.height + yGap; width = Math.max(width, compSize.width + insets.left + insets.right + xInset*2); } height += insets.bottom; return new Dimension( width, height); } public Dimension preferredLayoutSize(Container c) { return minimumLayoutSize(c); } public void removeLayoutComponent(Component c) {} } void updateDragEnabled(boolean dragEnabled) { tableView.setDragEnabled(dragEnabled); headerTextField.setDragEnabled(dragEnabled); footerTextField.setDragEnabled(dragEnabled); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?