动态表创建,我怎么知道它是哪个table?
Dyanmic JTable creation, how do I know which table is it?
我有这个代码,我需要 运行 取决于数字。我认为这很容易,是的,但后来这堵砖墙击中了我...
我怎么知道我点击了哪个 table 或要将项目放入哪个 table???
private void createTablesForBuilding() {
int buildingcoutns = 3;
for (int i = 0; i < buildingcoutns; i++) {
JTable jt = new JTable();
tables.add(jt);
tables.get(i).setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String[]{
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
tables.get(i).getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (jt.getSelectedRow() > -1) {
System.out.print("what now???");
}
}
});
JScrollPane js = new JScrollPane();
js.setViewportView(tables.get(i));
JPanel jp = new JPanel();
jp.add(js);
Buildings_Panel.add(jp);
}
}
知道了!我创建了一个单独的 class 并在其中分配了一个变量,我可以在其中为我的 table 添加一个 ID。这就是我所做的。
private void createTablesForBuilding() {
int buildingcoutns = 5;
for (int i = 0; i < buildingcoutns; i++) {
myTables tbls = new myTables();
tbls.setTableID(i);
tables.add(tbls);
JTable thistable = tables.get(i).table;
thistable.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String[]{
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
tables.get(i).table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (thistable.getSelectedRow() > -1) {
System.out.println("tableID is:"+tbls.getTableID());
}
}
});
Buildings_Panel.add(tables.get(i));
}
}
这里是 myTables
public class myTables extends javax.swing.JPanel {
int tableID = 0;
public myTables() {
initComponents();
}
public void setTableID(int i){
tableID = i;
}
public int getTableID(){
return tableID;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
table = new javax.swing.JTable();
setLayout(new java.awt.CardLayout());
table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(table);
add(jScrollPane1, "card2");
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
public javax.swing.JTable table;
// End of variables declaration
}
我有这个代码,我需要 运行 取决于数字。我认为这很容易,是的,但后来这堵砖墙击中了我...
我怎么知道我点击了哪个 table 或要将项目放入哪个 table???
private void createTablesForBuilding() {
int buildingcoutns = 3;
for (int i = 0; i < buildingcoutns; i++) {
JTable jt = new JTable();
tables.add(jt);
tables.get(i).setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String[]{
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
tables.get(i).getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (jt.getSelectedRow() > -1) {
System.out.print("what now???");
}
}
});
JScrollPane js = new JScrollPane();
js.setViewportView(tables.get(i));
JPanel jp = new JPanel();
jp.add(js);
Buildings_Panel.add(jp);
}
}
知道了!我创建了一个单独的 class 并在其中分配了一个变量,我可以在其中为我的 table 添加一个 ID。这就是我所做的。
private void createTablesForBuilding() {
int buildingcoutns = 5;
for (int i = 0; i < buildingcoutns; i++) {
myTables tbls = new myTables();
tbls.setTableID(i);
tables.add(tbls);
JTable thistable = tables.get(i).table;
thistable.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String[]{
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
tables.get(i).table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (thistable.getSelectedRow() > -1) {
System.out.println("tableID is:"+tbls.getTableID());
}
}
});
Buildings_Panel.add(tables.get(i));
}
}
这里是 myTables
public class myTables extends javax.swing.JPanel {
int tableID = 0;
public myTables() {
initComponents();
}
public void setTableID(int i){
tableID = i;
}
public int getTableID(){
return tableID;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
table = new javax.swing.JTable();
setLayout(new java.awt.CardLayout());
table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(table);
add(jScrollPane1, "card2");
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
public javax.swing.JTable table;
// End of variables declaration
}