gpt4 book ai didi

java - 向 JTable 中插入一行

转载 作者:行者123 更新时间:2023-12-01 09:32:17 25 4
gpt4 key购买 nike

编辑:问题已解决。我不小心在 if 语句中放置了 return 而不是 break。该函数在更改 DefaultTableModel 之前退出。

StockNameArr 是表中所有股票代码的数组(按字母顺序排列),按字母顺序排列。

此类表示具有两个可单击列的 JTable。第一列现在仅打印到 stdout,而且效果很好。第二个删除它所在的行,并在 JComboBox 中按字母顺序排列每只股票的名称。据我所知,这也可以正常工作。

当点击JComboBox中的其中一个项目时,它应该删除该项目,创建一个String[8](与8行的其他列匹配),从单击的项目中插入股票名称,并将数组作为一行插入到之前删除的同一位置。

但实际发生的情况是,该项目从 JComboBox 中删除,而新行从未插入到表中。我试过打电话:

model.insertItemAt(...);
thisTable.setModel(model);

但它没有做任何事情。认为可能 modelthisTable 设置不正确,我将 thisTable 引用为类变量,并尝试插入一行到 TableModel 中,甚至无需在内部类中为其创建新变量。尽管如此,还是没有运气。

有人可以帮助我吗?

import java.awt.event.Action/Event;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JMainTable extends JTable {
JMainTable thisTable = this; //Represents this class in inner classes
final String[] stockNames;
DefaultTableModel model;
JComboBox<String> dropdown;
ArrayList<String> dropdownStocks = new ArrayList<String> ();
int rowChangeOffset = 0;
public JMainTable(DefaultTableModel tableModel, String[] stockNameArr, JComboBox<String> combobox) {
super(tableModel);
// thisTable = this;
stockNames = stockNameArr;
addMouseListener(new mainTableListener());
model = tableModel;

dropdown = combobox;
dropdown.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
//^ prevents ActionListener from firing when scrolling through list with arrow keys

dropdown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!e.getActionCommand().equals(ActionEvent.KEY_EVENT_MASK)){
System.out.println("Clicked");
JComboBox<String> source = (JComboBox<String>) e.getSource();
String stockString = (String) source.getSelectedItem();
int index = dropdownStocks.indexOf(stockString);
try {
source.removeItemAt(index);
dropdownStocks.remove(index);
} catch(ArrayIndexOutOfBoundsException arrEx) {}
int indexInStockNames = 0; //for compiler
for(int i = 0; i<stockNames.length; i++) {
if(stockNames[i].equals(stockString)) {
indexInStockNames = i;
return;
}//end of If
}//End of For
String[] replacementRow = new String[8];
replacementRow[0] = stockString;
//model.insertRow(indexInStockNames, replaceRow);
((DefaultTableModel) thisTable.getModel()).insertRow(indexInStockNames, replacementRow);
if(index>=0) {//Prevents offset from being changed
//when user clicks on empty JComboBox
rowChangeOffset--;
}

}
}
});
}

class mainTableListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent mouseevent) {
int column;
if((column = thisTable.getSelectedColumn()) == 6) {
int row = thisTable.getSelectedRow();
System.out.println("Button for " + stockNames[row + rowChangeOffset] + " working.");//TESTING
//NOIMPLEMEN
} else if((column = thisTable.getSelectedColumn()) == 7) {
int row = thisTable.getSelectedRow();
model.removeRow(row);
try{
dropdownStocks.add(stockNames[row + rowChangeOffset]);
} catch(Exception adsf) {adsf.printStackTrace(); System.out.println(stockNames[row+rowChangeOffset]);}
int index;
try {
Collections.sort(dropdownStocks);
index = dropdownStocks.indexOf(stockNames[row+rowChangeOffset]);
} catch(Exception ex) {/*dropdownStocks may be empty*/
index=0;
}

dropdown.insertItemAt(stockNames[row+rowChangeOffset], index);
rowChangeOffset = rowChangeOffset + 1;
}
}
}
}

最佳答案

试试这个,它将有助于向链接到表的模型添加一行

 model.addRow(new Object[]{objec1,object2}); //param to count of columns to table
jTable1.setModel(modelo);
/*You are not required if in the beginning did something like this
modelo = (DefaultTableModel)jTable1.getModel();*/

关于java - 向 JTable 中插入一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39310185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com