簡體   English   中英

JTable.setDefaultEditor()不起作用,但設置為特定字段有效。

[英]JTable.setDefaultEditor() not working but setting to a specific field works.

嘗試使用JTable.setDefaultEditor(),但似乎未激活。 將其專門設置為一列即可,但不能將其設置為默認編輯器。 設置為特定列時,不會返回println命令,但該命令可見。

設置默認編輯器時是否需要執行額外的步驟?

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.TableCellEditor;
    import javax.swing.table.TableColumn;

    public class Main {
      public static void main(String[] argv) throws Exception {

        JFrame myFrame = new JFrame();
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String columnNames[] = { "Column 1", "Column 2", "Column 3" };

        String dataValues[][] =
        {
                { "12", "234", "67" },
                { "-123", "43", "853" },
                { "93", "89.2", "109" },
                { "279", "9033", "3092" }
            };
        JTable table = new JTable(dataValues, columnNames);
        myFrame.getContentPane().add(table);

        table.setDefaultEditor(String.class, new MyTableCellEditor());
    //    TableColumn col = table.getColumnModel().getColumn(0);
    //    col.setCellEditor(new MyTableCellEditor());
        myFrame.pack();
        myFrame.setVisible(true);
      }
    }

    class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor, FocusListener
    {

        JComponent component = new JTextField();

        public MyTableCellEditor()
        {
             component.addFocusListener(this);  
        }

      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
          int rowIndex, int vColIndex) {
          System.out.println("Inside getTableCellEditorComponent()");
        ((JTextField) component).setText((String) value);

        return component;
      }

      public Object getCellEditorValue() {
        return ((JTextField) component).getText();
      }

        @Override
        public void focusGained(FocusEvent e) {
            // TODO Auto-generated method stub
        }

        @Override
        public void focusLost(FocusEvent e) {
            System.out.println("Focus Lost");

        }
    }

這是我不喜歡DefaultTableModel的原因之一...

如果更改table.setDefaultEditor(String.class, new MyTableCellEditor()); table.setDefaultEditor(Object.class, new MyTableCellEditor()); ,它將起作用,但是,更好的解決方案是重寫DefaultTableModelgetColumnClass方法

DefaultTableModel model = new DefaultTableModel(dataValues, columnNames){

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        // You really should be checking the columnIndex and
        // returning the appropriate data type for the column,
        // but you get the idea
        return String.class;
    }

};
JTable table = new JTable(model);
myFrame.getContentPane().add(table);

table.setDefaultEditor(String.class, new MyTableCellEditor());

看看如何使用表格了解更多詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM