gpt4 book ai didi

java - 从操作监听器调用 fireTableRowsInserted() 时,JTable 不会立即更新

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

我有一个 JTable,我正在使用在互联网上找到的数据插入行。当我运行直接从主函数填充列表的代码时,我可以看到在调用“fireTableRowsInserted”的同时插入每一行 - 这是我想要的行为。但是,当我从 JButton 的 ActionListener 运行相同的代码时(在“ActionPreformed()”期间),表不会更新,直到线程/事件完成。我认为这个问题与线程有关,但我不太熟悉线程如何工作,不知道从哪里开始。这是问题的示例:

public class Main { 
static TestModel myTestModel = new TestModel();

public static void main(String[] args) {
JFrame testFrame = new JFrame();
JTable table = new JTable(myTestModel);
JButton button = new JButton("Load");

testFrame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
testFrame.getContentPane().add(button, BorderLayout.PAGE_END);
testFrame.pack();
testFrame.setVisible(true);
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

button.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e) {
//When openURLStr is called here, the table in the JFrame doesn't add rows until
// the function has exited.
openURLStr("http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Component.html", true);
}
});

//When openURLStr is called here, the table in the JFrame adds rows at the same time the
// list in the table-model adds a row. (the desired behavoir)
openURLStr("http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Component.html", true);
myTestModel.myArrayList.clear();
myTestModel.fireTableDataChanged();
}

static class TestModel extends AbstractTableModel {
ArrayList<String> myArrayList = new ArrayList<String>();

@Override public int getColumnCount() { return(3); }
@Override public int getRowCount() { return(myArrayList.size()); }
@Override public Object getValueAt(int row, int col) {
char c = '*';
String rowStr = myArrayList.get(row);
if ( rowStr.length() > col ) c = rowStr.charAt(col);
return(c);
}

public boolean add(String line) {
int row = myArrayList.size();
if ( row < 100 ) {
myArrayList.add(line);
this.fireTableRowsInserted(row, row);
return(true);
}
else {
return(false);
}
}
}

private static void openURLStr(String urlStr, boolean lookForLinks) {
try {
URL url = new URL(urlStr);
InputStreamReader isr = new InputStreamReader(url.openConnection().getInputStream());
BufferedReader br = new BufferedReader(isr);
ArrayList<String> linksFound = new ArrayList<String>();

if ( br != null ) {
String strLine = br.readLine();
//row added to table-model here:
boolean continueParse = myTestModel.add(strLine);

//this code is mainly to induce more time between each addition of a line.
while ( strLine != null && continueParse ) {
if ( lookForLinks && strLine != null ) {
String ahrefStr = "a href=\"";
int start_i = strLine.indexOf(ahrefStr) + ahrefStr.length();
if ( start_i > -1 ) {
int end_i = strLine.indexOf("\"", start_i+1);
if ( end_i > -1 && end_i > start_i ) {
linksFound.add(strLine.substring(start_i, end_i));
}
}
}
strLine = br.readLine();
}
br.close();
}

for ( String link : linksFound ) {
openURLStr(link, false);
}

} catch (IOException e) {
}
}
}

最佳答案

ActionListener 的 actionPerformedevent dispatch thread 上执行这将被你里面的代码阻止。因此,GUI 此时会卡住,并且根本不会显示任何更新。

尝试分离“长时间运行”的 URL 调用,例如在 SwingWorker .

关于java - 从操作监听器调用 fireTableRowsInserted() 时,JTable 不会立即更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6115435/

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