⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplejxtabledemo.java

📁 java实现浏览器等本地桌面的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * SimpleJXTableDemo.java is a 1.4 application that requires no other files. It is derived from * SimpleTableDemo in the Swing tutorial. */package org.jdesktop.demo.sample;import java.awt.BorderLayout;import java.awt.Color;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.io.InputStreamReader;import java.io.LineNumberReader;import java.net.URL;import java.util.Comparator;import java.util.Enumeration;import java.util.Iterator;import java.util.regex.Pattern;import javax.swing.AbstractAction;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JTextField;import javax.swing.ListSelectionModel;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableColumn;import org.jdesktop.swingx.JXTable;import org.jdesktop.swingx.action.AbstractActionExt;import org.jdesktop.swingx.decorator.AlternateRowHighlighter;import org.jdesktop.swingx.decorator.Filter;import org.jdesktop.swingx.decorator.FilterPipeline;import org.jdesktop.swingx.decorator.Highlighter;import org.jdesktop.swingx.decorator.HighlighterPipeline;import org.jdesktop.swingx.decorator.PatternFilter;import org.jdesktop.swingx.decorator.RolloverHighlighter;import org.jdesktop.swingx.decorator.Sorter;import org.jdesktop.swingx.table.TableColumnExt;/** * This SimpleJXTableDemo is a very simple example of how to use the extended features of the * JXTable in the SwingX project. The major features are covered, step-by-step. You can run * this demo from the command-line without arguments * java org.jdesktop.demo.sample.SimpleJXTableDemo * * If looking at the source, the interesting code is in configureJXTable(). * * This is derived from the SimpleTableDemo in the Swing tutorial. * * @author Patrick Wright (with help from the Swing tutorial :)) */public class SimpleJXTableDemo extends JPanel {    public SimpleJXTableDemo() {        super(new BorderLayout());        initUI();    }        private void initUI() {        JXTable jxTable = initTable();        configureJXTable(jxTable);                //Create the scroll pane and add the table to it.        JScrollPane scrollPane = new JScrollPane(jxTable);                //Add the scroll pane to this panel.        add(scrollPane, BorderLayout.CENTER);                // add our search panel        // TODO: not ready yet        // add(initSearchPanel(jxTable), BorderLayout.NORTH);        add(initConfigPanel(jxTable), BorderLayout.NORTH);    }        /** Initialize our JXTable; this is standard stuff, just as with JTable */    private JXTable initTable() {        // boilerplate table-setup; this would be the same for a JTable        SampleTableModel model = new SampleTableModel();        JXTable table = new JXTable(model);        model.loadData();        table.setPreferredScrollableViewportSize(new Dimension(500, 70));                table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);        return table;    }        /** For demo purposes, the special features of the JXTable are configured here. There is     * otherwise no reason not to do this in initTable().     */    private void configureJXTable(JXTable jxTable) {        // This shows the column control on the right-hand of the header.        // All there is to it--users can now select which columns to view        jxTable.setColumnControlVisible(true);                // our data is pulling in too many columns by default, so let's hide some of them        // column visibility is a property of the TableColumnExt class; we can look up a        // TCE using a column's display name or its index        TableColumnExt latitude = jxTable.getColumnExt("LATITUDE");        latitude.setVisible(false);        jxTable.getColumnExt("LONGITUDE").setVisible(false);        jxTable.getColumnExt("DEWPOINT").setVisible(false);        jxTable.getColumnExt("VISIBILITY").setVisible(false);        jxTable.getColumnExt("WIND_SPEED").setVisible(false);        jxTable.getColumnExt("GUST_SPEED").setVisible(false);        jxTable.getColumnExt("VISIBILITY_QUAL").setVisible(false);        jxTable.getColumnExt("WIND_DIR").setVisible(false);        jxTable.getColumnExt("WIND_DEG").setVisible(false);        jxTable.getColumnExt("REGION").setVisible(false);                        // note that now, getColumnExt() will fail for any of these columns we have hidden        // JW: changed to api to not fail        try {            jxTable.getColumnExt("LATITUDE");        } catch (Exception e) {            // we expect this to happen            // ignore        }                // however, if we still have the TableColumnExt we can change our mind        latitude.setVisible(true);                // now we can change the remaining column titles. This is a convenience for calling        // setHeaderValue() on a TableColumn.        //        // We'll do a trick, though, and that is to set the identifiers of each column        // to their current header value, so we can still use the same names for identifiers        // in the rest of our code                // First, a trick: by default, the "identifier" for a TableColumn is actually null        // unless we specifically set it; the header value is used instead. By doing this get,        // we're pulling the header value, and setting that as the identifier; then we can        // change the header value independently. This could also be done in creating the        // TableModel, of course.//        Enumeration en = jxTable.getColumnModel().getColumns();//        while ( en.hasMoreElements() ) {//            TableColumn tc = (TableColumn)en.nextElement();//            tc.setIdentifier(tc.getIdentifier());//        }//        ////        // ...and now change the titles//        int c = jxTable.getColumnCount();//        for ( int i=0; i < c; i++ ) {//            TableColumnExt tce = jxTable.getColumnExt(i);//            String title = tce.getTitle();//            title = title.substring(0,1).toUpperCase() + title.substring(1).toLowerCase();//            title.replace('_', ' ');//            tce.setTitle(title);//        }        //JW: use new api to access visible and invisible columns        // there's a slight catch: the getColumns does not (cannot) guarantee        // that the elements are of type TableColumnExt        for (Iterator iter = jxTable.getColumns(true).iterator(); iter.hasNext();) {            TableColumnExt column = (TableColumnExt) iter.next();            column.setIdentifier(column.getIdentifier());            // ...and now change the title            String title = column.getTitle();            title = title.substring(0,1).toUpperCase() + title.substring(1).toLowerCase();            title.replace('_', ' ');            column.setTitle(title);        }        // Sorting by clicking on column headers is on by default. However, the comparison        // between rows uses a default compare on the column's type, and elevations        // are not sorting how we want.        //        // We will override the Comparator assigned to the Sorter instance assigned        // to the elevation column. Every column has a Sorter using a default Comparator.        // By using a custom Comparator we can control how sorting in any column takes place        Comparator numberComparator = new Comparator() {            public int compare(Object o1, Object o2) {                Double d1 = Double.valueOf(o1 == null ? "0" : (String)o1);                Double d2 = Double.valueOf(o2 == null ? "0" : (String)o2);                return d1.compareTo(d2);            }        };        // First, the long way to assign--just to show there's a Sorter involved        Sorter sorter = jxTable.getColumnExt("ELEVATION").getSorter();        sorter.setComparator(numberComparator);                // and, shorthand        jxTable.getColumnExt("TEMPERATURE").getSorter().setComparator(numberComparator);                // comparators are good for special situations where the default comparator doesn't        // understand our data.                // This turns horizontal scrolling on or off. If the table is too large for the scrollpane,        // and horizontal scrolling is off, columns will be resized to fit within the pane, which can        // cause them to be unreadable. Setting this flag causes the table to be scrollable right to left.        jxTable.setHorizontalScrollEnabled(false);                // We'll add a highlighter to offset different row numbers        // Note the setHighlighters() takes an array parameter; you can chain these together.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -