📄 simplejxtabledemo.java
字号:
jxTable.setHighlighters(new HighlighterPipeline(new Highlighter[]{ AlternateRowHighlighter.classicLinePrinter })); // ...oops! we forgot one jxTable.getHighlighters().addHighlighter(new RolloverHighlighter(Color.BLACK, Color.WHITE )); jxTable.setRolloverEnabled(true); // add a filter: include countries starting with a only int col = jxTable.getColumn("COUNTRY").getModelIndex(); jxTable.setFilters(new FilterPipeline(new Filter[] { new PatternFilter("^A", 0, col) })); // resize all the columns in the table to fit their contents // this is available as an item in the column control drop down as well, so the user can trigger it. int margin = 5; jxTable.packTable(margin); // we want the country name to always show, so we'll repack just that column // we can set a max size; if -1, the column is forced to be as large as necessary for the // text margin = 10; int max = -1; // JW: don't - all column indices are view coordinates // JW: maybe we need xtable api to take a TableColumn as argument? //jxTable.packColumn(jxTable.getColumnExt("COUNTRY").getModelIndex(), margin, max); int viewIndex = jxTable.convertColumnIndexToView(jxTable.getColumnExt("COUNTRY").getModelIndex()); jxTable.packColumn(viewIndex, margin, max); } /** This shows off some additional JXTable configuration, controlled by checkboxes in a Panel. */ private JPanel initConfigPanel(final JXTable jxTable) { JPanel config = new JPanel(); FlowLayout fll = (FlowLayout)config.getLayout(); fll.setAlignment(FlowLayout.LEFT); fll.setHgap(30); // This shows or hides the column control--note this is possible at runtime final JCheckBox control = new JCheckBox(); control.setSelected(jxTable.isColumnControlVisible()); control.setAction(new AbstractAction("Show column control") { public void actionPerformed(ActionEvent e) { jxTable.setColumnControlVisible(control.isSelected()); } }); // turn sorting by column on or off // bug: there is no API to read the current value! we will assume it is false final JCheckBox sorting = new JCheckBox(); sorting.setSelected(jxTable.isSortable()); sorting.setAction(new AbstractAction("Sortable") { public void actionPerformed(ActionEvent e) { jxTable.setSortable(sorting.isSelected()); } }); // add checkbox for horizontal scrolling. basically, the table has an action for this, // and we need to link the checkbox up in both directions--so that if the property changes // the checkbox is updated, and vice-versa. we use an AbstractActionExt to make this easier. // you aren't supposed to understand this :) and yes, it will be refactored final JCheckBox horiz = new JCheckBox(); AbstractActionExt hA = (AbstractActionExt)jxTable.getActionMap().get(jxTable.HORIZONTALSCROLL_ACTION_COMMAND); hA.addPropertyChangeListener(new PropertyChangeListener(){ public void propertyChange(PropertyChangeEvent evt) { String propertyName = evt.getPropertyName(); if (propertyName.equals("selected")) { Boolean selected = (Boolean)evt.getNewValue(); horiz.setSelected(selected.booleanValue()); } } }); horiz.addItemListener(hA); horiz.setAction(hA); config.add(control); config.add(sorting); config.add(horiz); return config; } /** TODO: this is not working, need to figure out how PatternFilter is applied. */ private JPanel initSearchPanel(final JXTable jxTable) { JPanel search = new JPanel(); final JButton filter = new JButton("Show"); final JButton reset = new JButton("Clear"); final JTextField text = new JTextField(""); text.setColumns(30); filter.setAction(new AbstractAction("Show") { FilterPipeline fp = null; PatternFilter pf = new PatternFilter(); boolean isAdded = false; public void actionPerformed(ActionEvent e) { String searchString = text.getText().trim(); if ( searchString.length() > 0 ) { pf.setPattern(Pattern.compile(searchString)); if ( ! isAdded ) { fp = new FilterPipeline(new Filter[]{ pf }); jxTable.setFilters(fp); isAdded = true; } } } }); search.add(text); search.add(filter); search.add(reset); return search; } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("SimpleTableDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. SimpleJXTableDemo newContentPane = new SimpleJXTableDemo(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setSize(1024, 768); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } class SampleTableModel extends DefaultTableModel { void loadData() { try { URL url = SampleTableModel.class.getResource("/org/jdesktop/demo/sample/resources/weather.txt"); loadDataFromCSV(url); } catch ( Exception e ) { e.printStackTrace(); loadDefaultData(); } } private void loadDataFromCSV(URL url) { try { LineNumberReader lnr = new LineNumberReader(new InputStreamReader(url.openStream())); String line = lnr.readLine(); String[] cols = line.split("\t"); for ( String col : cols ) { addColumn(col); } while (( line = lnr.readLine()) != null ) { addRow(line.split("\t")); } } catch ( Exception e ) { e.printStackTrace(); loadDefaultData(); } } private void loadDefaultData() { int colCnt = 6; int rowCnt = 10; for ( int i=0; i < colCnt; i++ ) { addColumn("Column-" + (i + 1)); } for ( int i=0; i <= rowCnt; i++ ) { String[] row = new String[colCnt]; for ( int j=0; j < colCnt; j++ ) { row[j] = "Row-" + i + "Column-" + (j + 1); } addRow(row); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -