2012-01-06 103 views
1

我想要获取JTable中的某些单元格中存在的内部JTable中的选定单元格,但无法这样做。请让我知道,我哪里错了?或者如果有其他方法可以做同样的事情。如何从JTable中获取选定的单元格存在于JTable单元格内?

import javax.swing.JTable; 
import javax.swing.ListSelectionModel; 
import javax.swing.event.ListSelectionListener; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.table.TableCellRenderer; 
import javax.swing.table.TableColumnModel; 
import javax.swing.JScrollPane; 
import javax.swing.JFrame; 

import java.awt.*; 
import java.awt.event.*; 

public class SimpleTableSelectionDemo extends JFrame { 
    private boolean DEBUG = true; 
    private boolean ALLOW_COLUMN_SELECTION = true; 
    private boolean ALLOW_ROW_SELECTION = true; 

    public SimpleTableSelectionDemo() { 
    super("SimpleTableSelectionDemo"); 

    Object[][] subTableData= {{1,2,3}, {4,5,6}, {7,8,9}};   
    String[] subColumnNames = {"Col1", "Col2", "Col3"}; 

    final JTable table1 = new JTable(subTableData, subColumnNames); 
    TableColumnModel tcm1 = table1.getColumnModel(); 
    for(int it = 0; it < tcm1.getColumnCount(); it++){ 
     tcm1.getColumn(it).setCellRenderer(new CellRenderer()); 
    } 
    table1.setName("InnerTable"); 
    processTable(table1); 

    Object[][] data = { 
     {"Mary", "Campione", 
     "Snowboarding", new Integer(5), new Boolean(false)}, 
     {"Alison", "Huml", 
     "Rowing", new Integer(3), new Boolean(true)}, 
     {"Kathy", "Walrath", 
     "Chasing toddlers", new Integer(2), new Boolean(false)}, 
     {"Mark", "Andrews", 
     "Speed reading", new Integer(20), new Boolean(true)}, 
     {"Angela", "Lih", 
     "Teaching high school", table1, new Boolean(false)} 
    }; 

    String[] columnNames = {"First Name", 
          "Last Name", 
          "Sport", 
          "# of Years", 
          "Vegetarian"}; 

    final JTable table = new JTable(data, columnNames); 
    table.setRowHeight(60); 
    table.setName("OuterTable"); 
    processTable(table); 

    table.setComponentPopupMenu(new TestPopUpDemo("Add Table")); 
    TableColumnModel tcm = table.getColumnModel(); 
    for(int it = 0; it < tcm.getColumnCount(); it++){ 
     tcm.getColumn(it).setCellRenderer(new CellRenderer()); 
    } 
    //Create the scroll pane and add the table to it. 
    JScrollPane scrollPane = new JScrollPane(table); 

    //Add the scroll pane to this window. 
    getContentPane().add(scrollPane, BorderLayout.CENTER); 

    addWindowListener(new WindowAdapter() { 
     public void windowClosing(WindowEvent e) { 
      System.exit(0); 
     } 
    }); 
} 

private class CellRenderer implements TableCellRenderer{ 

    public Component getTableCellRendererComponent(JTable table, 
      Object value, boolean isSelected, boolean hasFocus, 
      int row, int column) { 
      /* If what we're displaying isn't an array of values we 
      return the normal renderer*/ 
      if(!(value instanceof JTable)){ 
       return table.getDefaultRenderer( 
       value.getClass()).getTableCellRendererComponent(
       table, value, isSelected, hasFocus,row, column); 
      }else { 
       JTable subTable = (JTable)value; 
       return subTable; 
      } 
     } 

} 

private void processTable (final JTable table) { 
    table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    if (ALLOW_ROW_SELECTION) { // true by default 
     ListSelectionModel rowSM = table.getSelectionModel(); 
     rowSM.addListSelectionListener(new ListSelectionListener() { 
      public void valueChanged(ListSelectionEvent e) { 
       ListSelectionModel lsm = (ListSelectionModel)e.getSource(); 
       if (lsm.isSelectionEmpty()) { 
        System.out.println("No rows are selected."); 
       } else { 
        int selectedRow = lsm.getMinSelectionIndex(); 
        System.out.println("Row " + selectedRow 
             + " is now selected."); 
       } 
      } 
     }); 
    } else { 
     table.setRowSelectionAllowed(false); 
    } 

    if (ALLOW_COLUMN_SELECTION) { // false by default 
     if (ALLOW_ROW_SELECTION) { 
      //We allow both row and column selection, which 
      //implies that we *really* want to allow individual 
      //cell selection. 
      table.setCellSelectionEnabled(true); 
     } 
     table.setColumnSelectionAllowed(true); 
     ListSelectionModel colSM = 
      table.getColumnModel().getSelectionModel(); 
     colSM.addListSelectionListener(new ListSelectionListener() { 
      public void valueChanged(ListSelectionEvent e) { 
       ListSelectionModel lsm = (ListSelectionModel)e.getSource(); 
       if (lsm.isSelectionEmpty()) { 
        System.out.println("No columns are selected."); 
       } else { 
        int selectedCol = lsm.getMinSelectionIndex(); 
        System.out.println("Column " + selectedCol 
             + " is now selected."); 
       } 
      } 
     }); 
    } 

    if (DEBUG) { 
     table.addMouseListener(new MouseAdapter() { 
      public void mouseClicked(MouseEvent e) { 
       printDebugData(table, e); 
      } 
     }); 
    } 
} 

private void printDebugData(JTable table, MouseEvent e) { 

    System.out.println("OUTERTABLE:"+table.getSelectedRow()+"*"+table.getSelectedColumn()); 
    JTable innerTable = null; 
    int row = table.getSelectedRow(); 
    int col = table.getSelectedColumn(); 
    Component c = table.getCellRenderer(row, col) 
         .getTableCellRendererComponent(table, table.getValueAt(row,col), true,true, row, col); 
    if(c instanceof JTable){ 
     innerTable = (JTable)c; 
     int rowIndex = innerTable.rowAtPoint(innerTable.getLocation()); 
     int colIndex = innerTable.columnAtPoint(e.getPoint()); 
     System.out.println("INNERTABLE:"+rowIndex+"*"+colIndex); 
    } 

} 

public static void main(String[] args) { 
    SimpleTableSelectionDemo frame = new SimpleTableSelectionDemo(); 
    frame.setSize(500, 500); 
    frame.pack(); 
    frame.setVisible(true); 
} 

}

+1

永远不会将JSomeThing存储在TableModel中。相反,存储数据并将其呈现在自定义渲染器f.i中。 _using_ JTable渲染组件 – kleopatra 2012-01-06 09:32:34

+0

谢谢kleopatra。我会记住这一点。 – 2012-01-06 12:25:47

回答

1

内表的位置,如果你打电话getCellRendererComponent设置不正确。使用以下几行来相应地更正位置:

TableCellRenderer tableCellRenderer = table.getCellRenderer(row, col); 
Component c = table.prepareRenderer(tableCellRenderer, row, col); 

if (c instanceof JTable) { 
    innerTable = (JTable) c; 

    Point pnt = e.getPoint(); 
    Rectangle cellRect = table.getCellRect(row, col, false); 
    pnt.translate(-cellRect.x, -cellRect.y); 

    int rowIndex = innerTable.rowAtPoint(pnt); 
    int colIndex = innerTable.columnAtPoint(pnt); 
    System.out.println("INNERTABLE:" + rowIndex + "*" + colIndex); 
} 
+0

非常感谢!它像一个魅力。 – 2012-01-06 12:24:48