2012-11-21 55 views
1
import java.awt.BorderLayout; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.table.AbstractTableModel; 
import javax.swing.table.TableModel; 

class ColorTableModel extends AbstractTableModel { 

    Object rowData[][] = { { "value1", Boolean.FALSE }, 
     { "value1", Boolean.FALSE }, { "value1", Boolean.FALSE }, 
     { "value1", Boolean.FALSE}, { "value1", Boolean.FALSE }, }; 

    String columnNames[] = { "English", "Boolean" }; 

    public int getColumnCount() { 
    return columnNames.length; 
    } 

    public String getColumnName(int column) { 
    return columnNames[column]; 
    } 

    public int getRowCount() { 
    return rowData.length; 
    } 

    public Object getValueAt(int row, int column) { 
    return rowData[row][column]; 
    } 

    public Class getColumnClass(int column) { 
    return (getValueAt(0, column).getClass()); 
    } 

    public void setValueAt(Object value, int row, int column) { 
    rowData[row][column] = value; 
    } 

    public boolean isCellEditable(int row, int column) { 
    return (column != 0); 
    } 
} 

public class EditableColorColumn { 

    public static void main(String args[]) { 
    JFrame frame = new JFrame("Editable Color Table"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    TableModel model = new ColorTableModel(); 
    JTable table = new JTable(model); 
    // TableColumn column = table.getColumnModel().getColumn(3); 
    // column.setCellRenderer(renderer); 
    // column.setCellEditor(editor); 

    JScrollPane scrollPane = new JScrollPane(table); 
    frame.add(scrollPane, BorderLayout.CENTER); 
    frame.setSize(400, 150); 
    frame.setVisible(true); 
    } 

我想獲得第一列的值,如果我勾選相應的行。我已經閱讀了很多關於這方面的內容,但我不能把它寫入代碼。你可以編碼一個嗎?JTable Boolean.class

示例場景,當我選中row1複選框時,它將system.out.println()結果爲value1

回答

3

你在你的AbstractTableModel實施setValueAt()不火,將通知該改變偵聽器的事件:

@Override 
public void setValueAt(Object value, int row, int column) { 
    rowData[row][column] = value; 
    fireTableCellUpdated(row, column); 
} 

一旦更正,TableModelListener將會看到每個更改。嘗試對fireTableCellUpdated()行進行註釋以查看差異。

此外:

代碼:

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.event.TableModelEvent; 
import javax.swing.event.TableModelListener; 
import javax.swing.table.AbstractTableModel; 
import javax.swing.table.TableModel; 

/** 
* @see https://stackoverflow.com/q/13497276/230513 
*/ 
public class EditableColorColumn { 

    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new EditableColorColumn().display(); 
      } 
     }); 
    } 

    private void display() { 
     JFrame frame = new JFrame("Editable Color Table"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     final TableModel model = new ColorTableModel(); 
     JTable table = new JTable(model); 
     table.setPreferredScrollableViewportSize(new Dimension(400, 150)); 
     table.getModel().addTableModelListener(new TableModelListener() { 

      @Override 
      public void tableChanged(TableModelEvent e) { 
       System.out.println(model.getValueAt(e.getFirstRow(), 0) 
        + " " + model.getValueAt(e.getFirstRow(), 1)); 
      } 
     }); 

     frame.add(new JScrollPane(table), BorderLayout.CENTER); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    private static class ColorTableModel extends AbstractTableModel { 

     String columnNames[] = {"English", "Boolean"}; 
     Object rowData[][] = { 
      {"value1", Boolean.FALSE}, 
      {"value2", Boolean.TRUE}, 
      {"value3", Boolean.FALSE}, 
      {"value4", Boolean.TRUE}, 
      {"value5", Boolean.FALSE},}; 

     @Override 
     public int getColumnCount() { 
      return columnNames.length; 
     } 

     @Override 
     public String getColumnName(int column) { 
      return columnNames[column]; 
     } 

     @Override 
     public int getRowCount() { 
      return rowData.length; 
     } 

     @Override 
     public Object getValueAt(int row, int column) { 
      return rowData[row][column]; 
     } 

     @Override 
     public Class getColumnClass(int column) { 
      return (getValueAt(0, column).getClass()); 
     } 

     @Override 
     public void setValueAt(Object value, int row, int column) { 
      rowData[row][column] = value; 
      fireTableCellUpdated(row, column); 
     } 

     @Override 
     public boolean isCellEditable(int row, int column) { 
      return (column != 0); 
     } 
    } 
} 
0

您可以使用getSelectedRow()來獲取所選的行。然後使用getValueAt(row,column),其中row是您從getSelectedRow()獲得的值,column是您想要的列。嘗試以下操作: -

int row=table.getSelectedRow();

String value=table.getValueAt(row, 0).toString();

其中0表示將返回第一列的值。

+0

你有一些代碼片段? – Jong

+0

int row = table.getSelectedRow(); String value = table.getValueAt(row,0).toString(); – Addict

+0

出現錯誤,應在何處放置代碼? – Jong

0

只要你需要得到所選列的值,試試這個

table.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      int row = table.getSelectedRow(); 
      System.out.println("Selected Row ; " + row); 
      System.out.println("Velue : " + model.getValueAt(row, 0)); 
     } 
});