如何声明枚举有 jtable 标题列



嗨,我有一些 Jtable 和一些列,前两列是可排序的,素食列是组合框,现在我想将标题列更改为枚举。所以我想更改代码,使枚举应该是我的标题列......请给我回复,我从昨晚开始尝试这个程序

class MyTable extends JFrame  {
    /*public enum ColumNames {
        FIRSTNAME, LASTNAME, DEPARTMENT, SALARY,
        YEARS, VEGETARIAN, GENDER,AGE;

    }*/


    private JPanel topPanel;
    private JTable table;
    private JScrollPane scrollPane;
    private String[] columnNames;
    private Object[][] dataValues;
    JTextField textBox = new JTextField();
    public MyTable() {
        setTitle("MyTable");
        setSize(300, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        getContentPane().add(topPanel);
        columnNames = new String[] { "FirstName", "LastName", "Department", "Salary", "Years", "Vegetarian", "Gender",
                "Age" };
        dataValues = new Object[][] {
                { "Kathy", "Smith", "Networking", new Integer(50000), new Integer(5), new Boolean(false), "Male",
                        new Integer(35) },
                { "John", "Doe", "Testing", new Integer(65000), new Integer(3), new Boolean(true), "Male",
                        new Integer(45) },
                { "Sue", "Black", "Testing", new Integer(60000), new Integer(2), new Boolean(false), "Male",
                        new Integer(40) },
                { "Jane", "White", "Developer", new Integer(70000), new Integer(20), new Boolean(true), "Male",
                        new Integer(35) },
                { "Joe", "Brown", "Networking", new Integer(60000), new Integer(10), new Boolean(false), "Male",
                        new Integer(30) } };
        myTableModel model = new myTableModel();

        table = new JTable();
        table.setRowHeight(50);
        table.setModel(model);
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel()){

            @Override
            public boolean isSortable(int ColumnNames) {
                if(ColumnNames < (2))
                    return true;
                else 
                    return false;
            }};

        table.setRowSorter(sorter);

        setUpVegColumn(table,table.getColumnModel().getColumn(5));
        setUpSalColumn(table, table.getColumnModel().getColumn(3));

        table.setCellSelectionEnabled(true);
        scrollPane = new JScrollPane(table);
        topPanel.add(scrollPane, BorderLayout.CENTER);
    }


    private void setUpSalColumn(JTable table, TableColumn column) {
        TableColumn salary = table.getColumnModel().getColumn(3);
        salary.setCellEditor(new DefaultCellEditor(textBox));
        String s =textBox.getText();
        Pattern pattern = Pattern.compile("^[0-9]*$");
        Matcher m = pattern.matcher(s);
        if((!m.matches())){
            textBox.setEditable(false);
        JOptionPane.showMessageDialog(null, "enter numbers only");
            }
            else
            {
            textBox.setEditable(true);
            }
            }


    private void setUpVegColumn(JTable table, TableColumn column ) {
        JComboBox comboBox = new JComboBox();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement("True");
        model.addElement("False");
        model.addElement("Both");
        model.addElement("None of the above");
        comboBox.setModel(model);
        column.setCellEditor(new DefaultCellEditor(comboBox));
        model = new DefaultComboBoxModel();
        model.addElement("True");
        model.addElement("False");
        model.addElement("Both");
        model.addElement("None of the above");
        // Set up tool tips for the sport cells.
        ComboBoxTableCellRenderer renderer = new ComboBoxTableCellRenderer();
        renderer.setModel(model);
        column.setCellRenderer(renderer);
    }

    class ComboBoxTableCellRenderer extends JComboBox implements TableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
                int row, int column) {
            setSelectedItem(value);
            return this;
        }
    }
    public class myTableModel extends DefaultTableModel {
        myTableModel() {
            super(dataValues, columnNames);
        }
        @Override
        public boolean isCellEditable(int row, int cols) {
            if(cols==3 || cols==5){
                return true;
            }
            else{
                return false;
            }
        }
    }
    public static void main(String args[]) {
        MyTable x = new MyTable();
        x.setVisible(true);
    }

枚举是该枚举类型的静态实例的集合。因此ColumNames.FIRSTNAME的类型是 ColumNames ,而您需要一个字符串作为标头。您可以使用实例的值,但这可能不会为您提供所需的内容:

public enum ColumNames {
    FIRSTNAME, LASTNAME, DEPARTMENT, SALARY,
    YEARS, VEGETARIAN, GENDER,AGE;
}
final int numberOfColumns = ColumNames.values().length;
columnNames = new String[numberOfColumns ];
for (int i = 0; < numberOfColumns; i++) {
  columnNames[i] = ColumNames.values()[i].name();
}

这将为您提供等效的数组:

columnNames = new String[] { "FIRSTNAME", "LASTNAME", "DEPARTMENT", "SALARY", "YEARS", "VEGETARIAN", "GENDER", "AGE" };

或者,您可以在enum上定义一个显示方法并使用它而不是name()

public enum ColumNames {
    FIRSTNAME("FirstName"), 
    LASTNAME("LastName"),
    DEPARTMENT("Department"),
    SALARY("Salary",
    YEARS("Years"),
    VEGETARIAN("Vegetarian"), 
    GENDER("Gender"),
    AGE("Age");
    private final displayName;
    private ColumNames(String displayName){
      this.displayName = displayName;
    }
    public String getDisplayName() {
      return displayName;
    }
}

但是,如果国际化是一个问题,第一种方法是更好的方法,因为它使用独立于本地的常量字符串,可以在用户界面中替换。

您需要使用操作侦听器,否则应用程序将无法识别是否已执行操作,下面是方法setUpSalColumn的示例:

private void setUpSalColumn(JTable table, TableColumn column) {
        TableColumn salary = table.getColumnModel().getColumn(3);
        textBox.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
                validateSal();
            }
            public void removeUpdate(DocumentEvent e) {
                validateSal();
            }
            public void insertUpdate(DocumentEvent e) {
                validateSal();
            }
            public void validateSal() {
                Pattern pattern = Pattern.compile("^[0-9]*$");
                String s = textBox.getText();
                Matcher m = pattern.matcher(s);
                if ((!m.matches())) {
                    JOptionPane.showMessageDialog(null, "enter numbers only");
                } else {
                    textBox.setEditable(true);
                }
            }
        });
        salary.setCellEditor(new DefaultCellEditor(textBox));
    }

相关内容

  • 没有找到相关文章

最新更新