如何从JTable单元中获取JTable中的选定单元格?

时间:2012-01-06 06:57:16

标签: java swing jtable selection cell

我试图在内部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 个答案:

答案 0 :(得分: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);
}
相关问题