2014-05-16 110 views
0

我正在使用AbstractTableModel。JTable更新ColumnNames

當JTable到達最後一行時,我已經使用本網站提供的代碼(http://www.javalobby.org/articles/jtable/?source=archives)爲JTable自動生成新行。

它的工作正常,但我無法在用戶單擊按鈕時更新列名稱。所以我必須刪除自動生成的行功能來更新列名。

據我瞭解網站的代碼,它使用隱藏列作爲觸發器來創建新行。 InteractiveTableModelListener禁用了某些我不明白如何更改/編輯它的內容?

我該如何解決這個問題?

感謝您的時間!

+0

考慮提供一個實際的[演示您的問題可運行示例](https://stackoverflow.com/help/mcve)將涉及較少的猜測工作和更好的反應 – MadProgrammer

+0

PS-這不是一個很好的例子,該渲染器不應該修改模型... – MadProgrammer

+0

@MadProgrammer,你可以建議任何其他替代方案來實現該功能(自動生成新行)! –

回答

1

這是它怎麼可能可以提供「自動添加到末尾」功能

基本上是一個簡單的例子,這樣做是什麼地圖,提供量身定製的鍵盤導航自定義鍵綁定,在這個例子中,這包括標籤 + 標籤輸入最多向下它允許確定當按下這些鍵時應該發生什麼。

晴,標籤輸入截至是最相關的,我只是說了別人作爲一個例子,並保持一致性。

一個稍微有用的功能是提供一個「行工廠」,它能夠爲新的空白行生成所需的數據,而不是隻插入一堆null或至少I會覺得很有用。

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import javax.swing.AbstractAction; 
import javax.swing.ActionMap; 
import javax.swing.InputMap; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.KeyStroke; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableCellEditor; 
import javax.swing.table.TableModel; 

public class TestTable { 

    public static void main(String[] args) { 
     new TestTable(); 
    } 

    public TestTable() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       MutableDefaultTableModel model = new MutableDefaultTableModel(4, 4); 
       JTable table = new JTable(model); 

       KeyStroke tabKey = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0); 
       KeyStroke shiftTabKey = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_DOWN_MASK); 
       KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); 
       KeyStroke arrowLeftKey = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0); 
       KeyStroke arrowRightKey = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0); 
       KeyStroke arrowDownKey = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0); 
       KeyStroke arrowUpKey = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0); 

       InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 
       im.put(tabKey, "nextCell"); 
       im.put(enterKey, "nextCell"); 
       im.put(arrowRightKey, "nextCell"); 
       im.put(shiftTabKey, "previousCell"); 
       im.put(arrowLeftKey, "previousCell"); 
       im.put(arrowDownKey, "nextRow"); 
       im.put(arrowUpKey, "previousRow"); 

       ActionMap am = table.getActionMap(); 
       am.put("nextCell", new NextCellAction(table, model)); 
       am.put("previousCell", new PreviousCellAction(table, model)); 
       am.put("nextRow", new NextRowAction(table, model)); 
       am.put("previousRow", new PreviousRowAction(table, model)); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new JScrollPane(table)); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class MutableDefaultTableModel extends DefaultTableModel implements MutableTableModel { 

     public MutableDefaultTableModel(int rows, int cols) { 
      super(rows, cols); 
     } 

     @Override 
     public void insertNewRow(int row) { 
      Object[] rowData = new Object[getColumnCount()]; 
      if (row < getRowCount()) { 
       insertRow(row, rowData); 
      } else { 
       addRow(rowData); 
      } 
     } 

    } 

    public interface MutableTableModel extends TableModel { 

     public void insertNewRow(int row); 

    } 

    public abstract class AbstractTableAction extends AbstractAction { 

     private JTable table; 
     private MutableTableModel model; 

     private boolean forceStopEditing; 

     public AbstractTableAction(JTable table, MutableTableModel model) { 
      this.table = table; 
      this.model = model; 
     } 

     public MutableTableModel getModel() { 
      return model; 
     } 

     public JTable getTable() { 
      return table; 
     } 

     public boolean isForceStopEditing() { 
      return forceStopEditing; 
     } 

     public void setForceStopEditing(boolean forceStopEditing) { 
      this.forceStopEditing = forceStopEditing; 
     } 

     public void stopCellEditing() { 

      TableCellEditor editor = getTable().getCellEditor(); 
      if (editor != null) { 

       if (!editor.stopCellEditing() && isForceStopEditing()) { 

        editor.cancelCellEditing(); 

       } 

      } 

     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 

      JTable table = getTable(); 

      int row = table.getSelectedRow(); 
      int col = table.getSelectedColumn(); 

      int rowCount = table.getRowCount(); 
      int colCount = table.getColumnCount(); 

      stopCellEditing(); 

      int cell[] = updateCellCoordinates(row, col); 
      row = cell[0]; 
      col = cell[1]; 

      if (col < 0) { 
       col = colCount - 1; 
       row--; 
      } else if (col >= colCount) { 
       col = 0; 
       row++; 
      } 

      if (row < 0) { 
       row = 0; 
      } else if (row >= rowCount) { 

       getModel().insertNewRow(row); 

      } 

      table.setRowSelectionInterval(row, row); 
      table.setColumnSelectionInterval(col, col); 

     } 

     protected abstract int[] updateCellCoordinates(int row, int col); 

    } 

    public class NextCellAction extends AbstractTableAction { 

     public NextCellAction(JTable table, MutableTableModel model) { 
      super(table, model); 
     } 

     @Override 
     protected int[] updateCellCoordinates(int row, int col) { 
      return new int[]{row, ++col}; 
     } 

    } 

    public class PreviousCellAction extends AbstractTableAction { 

     public PreviousCellAction(JTable table, MutableTableModel model) { 
      super(table, model); 
     } 

     @Override 
     protected int[] updateCellCoordinates(int row, int col) { 
      return new int[]{row, --col}; 
     } 

    } 

    public class NextRowAction extends AbstractTableAction { 

     public NextRowAction(JTable table, MutableTableModel model) { 
      super(table, model); 
     } 

     @Override 
     protected int[] updateCellCoordinates(int row, int col) { 
      return new int[]{++row, col}; 
     } 

    } 

    public class PreviousRowAction extends AbstractTableAction { 

     public PreviousRowAction(JTable table, MutableTableModel model) { 
      super(table, model); 
     } 

     @Override 
     protected int[] updateCellCoordinates(int row, int col) { 
      return new int[]{--row, col}; 
     } 

    } 

}