TableModel model = new DefaultTableModel(rows, columns); JTable table = new JTable(model); RowSorter sorter = new TableRowSorter(model); table.setRowSorter(sorter); |
import javax.swing.*; import javax.swing.table.*; import java.awt.*; public class TestSortedTable { public static void main(String args[]) { JFrame frame = new JFrame("JTable的排序测试"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 表格中显示的数据 Object rows[][] = { { "王明", "中国", 44 }, { "姚明", "中国", 25 }, { "赵子龙", "西蜀", 1234 }, { "曹操", "北魏", 2112 }, { "Bill Gates", "美国", 45 }, { "Mike", "英国", 33 } }; String columns[] = { "姓名", "国籍", "年龄" }; TableModel model = new DefaultTableModel(rows, columns); JTable table = new JTable(model); RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); table.setRowSorter(sorter); JScrollPane pane = new JScrollPane(table); frame.add(pane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); } } |
出现这种情况是因为在默认情况下DefaultTableModal的列是Object类型。而要想使JTable按数值进行排序,必须要覆盖DefaultTableModal的getColumnClass方法。
TableModel model = new DefaultTableModel(rows, columns) { public Class getColumnClass(int column) { Class returnValue; if ((column >= 0) && (column < getColumnCount())) { returnValue = getValueAt(0, column).getClass(); } else { returnValue = Object.class; } return returnValue; } }; |
button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = filterText.getText(); if (text.length() == 0) { sorter.setRowFilter(null); } else { sorter.setRowFilter(RowFilter.regexFilter(text)); } } }); |
import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; public class TestFilter { public static void main(String args[]) { JFrame frame = new JFrame("JTable的过滤测试"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Object rows[][] = { { "王明", "中国", 44 }, { "姚明", "中国", 25 }, { "赵子龙", "西蜀", 1234 }, { "曹操", "北魏", 2112 }, { "Bill Gates", "美国", 45 }, { "Mike", "英国", 33 } }; String columns[] = { "姓名", "国籍", "年龄" }; TableModel model = new DefaultTableModel(rows, columns) { public Class getColumnClass(int column) { Class returnValue; if ((column >= 0) && (column < getColumnCount())) { returnValue = getValueAt(0, column).getClass(); } else { returnValue = Object.class; } return returnValue; } }; final JTable table = new JTable(model); final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); table.setRowSorter(sorter); JScrollPane pane = new JScrollPane(table); frame.add(pane, BorderLayout.CENTER); JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel("过滤"); panel.add(label, BorderLayout.WEST); final JTextField filterText = new JTextField(""); panel.add(filterText, BorderLayout.CENTER); frame.add(panel, BorderLayout.NORTH); JButton button = new JButton("过滤"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = filterText.getText(); if (text.length() == 0) { sorter.setRowFilter(null); } else { sorter.setRowFilter(RowFilter.regexFilter(text)); } } }); frame.add(button, BorderLayout.SOUTH); frame.setSize(300, 250); frame.setVisible(true); } } |
增强的JTabbedPane组件
JTabbedPane组件为我们提供了一种非常好的方法在窗体上显示很多的控件。我们可以将不同类别的控件放到不同的Tab页上,然后通过需要点击相应的Tab页。在传统的Tab页上只能防止文本的图标。而在Java SE 6中使我们可以直接将控件放到Tab上。我们可以通过setTabComponentAt方法将控件放到Tab上。这个方法有两个参数,一个是Tab的索引,另一个是要放置的对象。
JTabbedPane pane = new JTabbedPane(); pane.setTabComponentAt(1, component); |
import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; public class TestTabbedPane { static void addIt(JTabbedPane tabbedPane, String text) { JLabel label = new JLabel(text); JButton button = new JButton(text); JPanel panel = new JPanel(); panel.add(label); panel.add(button); tabbedPane.addTab(text, panel); if(text.equals("tab4")) tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, new JTextField("插入了文本控件")); else tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1,button); } public static void main(String args[]) { JFrame f = new JFrame("JTabbedPane演示"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTabbedPane tabbedPane = new JTabbedPane(); addIt(tabbedPane, "tab1"); addIt(tabbedPane, "tab2"); addIt(tabbedPane, "tab3"); addIt(tabbedPane, "tab4"); addIt(tabbedPane, "tab5"); f.add(tabbedPane, BorderLayout.CENTER); f.setSize(400, 200); f.setVisible(true); } } |
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.print.*; public class TextPrint { public static void main(final String args[]) { JFrame frame = new JFrame("打印测试"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextArea textArea = new JTextArea(); JScrollPane pane = new JScrollPane(textArea); frame.add(pane, BorderLayout.CENTER); textArea.setText("打印内容...\r\n可以分页!" ); JButton button = new JButton("打印"); frame.add(button, BorderLayout.SOUTH); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { try { textArea.print(); } catch (PrinterException pe) { System.err.println("打印失败..."); } } }; button.addActionListener(listener); frame.setSize(250, 150); frame.setVisible(true); } } |
虽然提供了打印设置对话框,但我们并无法设置如页眉(角)等信息,幸运的是print的一个重载为我们提供了这个功能。下面是这个方法的参数。
public boolean print(MessageFormat headerFormat, MessageFormat footerFormat, boolean showPrintDialog, PrintService service, PrintRequestAttributeSet attributes, boolean interactive) |
import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.tree.*; public class TestDrapDrop { public static void main(String args[]) { JFrame f = new JFrame("拖放测试"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel top = new JPanel(new BorderLayout()); JLabel dragLabel = new JLabel("拖我:"); JTextField text = new JTextField(); text.setDragEnabled(true); top.add(dragLabel, BorderLayout.WEST); top.add(text, BorderLayout.CENTER); f.add(top, BorderLayout.NORTH); final JTree tree = new JTree(); final DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); tree.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) suppor.getDropLocation(); return dropLocation.getPath() != null; } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); TreePath path = dropLocation.getPath(); Transferable transferable = support.getTransferable(); String transferData; try { transferData = (String) transferable .getTransferData(DataFlavor.stringFlavor); } catch (IOException e) { return false; } catch (UnsupportedFlavorException e) { return false; } int childIndex = dropLocation.getChildIndex(); if (childIndex == -1) { childIndex = model.getChildCount(path.getLastPathComponent()); } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode( transferData); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent(); model.insertNodeInto(newNode, parentNode, childIndex); TreePath newPath = path.pathByAddingChild(newNode); tree.makeVisible(newPath); tree.scrollRectToVisible(tree.getPathBounds(newPath)); return true; } }); JScrollPane pane = new JScrollPane(tree); f.add(pane, BorderLayout.CENTER); JPanel bottom = new JPanel(); JLabel comboLabel = new JLabel("DropMode"); String options[] ={ "USE_SELECTION", "ON", "INSERT", "ON_OR_INSERT" }; final DropMode mode[] = {DropMode.USE_SELECTION, DropMode.ON, DropMode.INSERT, DropMode.ON_OR_INSERT }; final JComboBox combo = new JComboBox(options); combo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int selectedIndex = combo.getSelectedIndex(); tree.setDropMode(mode[selectedIndex]); } }); bottom.add(comboLabel); bottom.add(combo); f.add(bottom, BorderLayout.SOUTH); f.setSize(300, 400); f.setVisible(true); } |