DefaultTableModel  

取出 JTable 的 TableModel  
//get table model
DefaultTableModel tableModel = (DefaultTableModel) TablePanel.getTable().getModel();

新增 Row:
tableModel.addRow(Object[]  object);

移除 Row:
tableModel.removeRow(Index);

(使用迴圈由最後一列開始移除)

新增 Column:
public void addColumn(Object columnName, Object[] columnData)

事件:AddListSelectionListener
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
         @Override
         public void valueChanged(ListSelectionEvent e) {
          // row selection changed
          if (e.getValueIsAdjusting()) {
           //get index and value
           //method 1
           System.out.println(e.getSource());
           int selectRow = ((ListSelectionModel)e.getSource()).getLeadSelectionIndex();
           System.out.println(selectRow);
           System.out.println(table.getValueAt(selectRow, 0));
          
           // method 2
           selectRow =((JTable)  e.getSource()).getSelectedRow();
           ((JTable)  e.getSource()).getValueAt(selectRow, 0);
           }
       }

JList  

事件:AddListSelectionListener
JList list = new JList();
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(new ListSelectionListener(){
    public void valueChanged(ListSelectionEvent e) {
        Object selectedData = ((JList) e.getSource()).getSelectedValue();
    }
});
//list.setModel(model);

AbstractListModel
public class TestAbstractListModel extends AbstractListModel{
 private String[] testArray;
 @Override
 public Object getElementAt(int index) {
  // TODO Auto-generated method stub
  return testArray[index];
 }

 @Override
 public int getSize() {
  // TODO Auto-generated method stub
  return testArray.length;
 }
}



--
如果覺得文章不錯,請幫忙點選廣告,
謝謝!