Я пытаюсь получить выбранную ячейку во внутренней 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);
}
}