我试图从https://docs.oracle.com/javase/tutorial/uiswing/components/table.html修改tableDemo.java,以便可以从外部数据源(例如从套接字读取的数据)更新表模型。我能想到的唯一解决方案是将createAndShowGUI()方法从静态更改为非静态。但是,我不知道这是否是这个用例的推荐解决方案。我的工作代码粘贴在这里,但我希望任何改进或替代方法的建议。
package components;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
@SuppressWarnings("serial")
public class DynamicTableDemo extends JPanel {
// Add a MyTableModel member with a getter so you can modify table model data
public final MyTableModel myTableModel;
public DynamicTableDemo() {
super(new GridLayout(1,0));
myTableModel = new MyTableModel();
JTable table = new JTable(myTableModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
public MyTableModel getMyTableModel() {
return myTableModel;
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Stuff"};
private Object[][] data = {
{new Integer(0)},
{new Integer(0)},
{new Integer(0)},
{new Integer(0)},
{new Integer(0)}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* FIXME:
* Multiple markers at this line
* - overrides javax.swing.table.AbstractTableModel.getColumnClass
* - Type safety: The return type Class for getColumnClass(int) from the type DynamicTableDemo.MyTableModel
* needs unchecked conversion to conform to Class<?> from the type AbstractTableModel
* - Class is a raw type. References to generic type Class<T> should be parameterized
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*
* NOTE: Typically this is a static method but I removed static
* so the underlying table model can be modified.
*/
private void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("DynamicTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
this.setOpaque(true); //content panes must be opaque
frame.setContentPane(this);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
DynamicTableDemo dynamicTableDemo = new DynamicTableDemo();
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
dynamicTableDemo.createAndShowGUI();
}
});
int count = 0; // Change table data with an incrementing count
while (true) {
try {
final int count2 = count; // Copy to a final variable for Runnable, feels a bit kludgey...
/*
* In full application, blocking read of a value from a socket here.
* Note that myTableModel is updated from the EDT, but the socket read
* and sleep below will not be from the EDT.
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
dynamicTableDemo.getMyTableModel().setValueAt(new Integer(count2),
count2 % dynamicTableDemo.getMyTableModel().getRowCount(), 0);
}
});
count++;
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
}
}
基于评论的更新版本:
package components;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.AbstractTableModel;
@SuppressWarnings("serial")
public class DynamicTableDemo extends JPanel {
// Make the JTable a field, so inner class TableCellTask can access it
private final JTable table;
public DynamicTableDemo() {
super(new GridLayout(1,0));
table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
new TableCellTask().execute();
}
private static class TableCell {
private final Object value;
private final int row, col;
TableCell(Object value, int row, int col) {
this.value = value;
this.row = row;
this.col = col;
}
}
private class TableCellTask extends SwingWorker<Void, TableCell> {
@Override
protected Void doInBackground() {
int count = 0; // Change table data with an incrementing count
while (!isCancelled()) {
/*
* In full application, blocking read of a value from a socket here.
* Note that myTableModel is updated from the EDT, but the socket read
* and sleep below will not be from the EDT.
*/
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
publish(new TableCell(count, count % table.getModel().getRowCount(), 0));
count++;
}
return null;
}
@Override
protected void process(List<TableCell> tableCells) {
for (TableCell tableCell : tableCells) {
table.getModel().setValueAt(tableCell.value, tableCell.row, tableCell.col);
}
}
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Stuff"};
private Object[][] data = {
{new Integer(0)},
{new Integer(0)},
{new Integer(0)},
{new Integer(0)},
{new Integer(0)}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("DynamicTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
DynamicTableDemo newContentPane = new DynamicTableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
问题内容: 我有一个侦听某些x端口的套接字。 我可以从客户端应用程序将数据发送到套接字,但是无法从服务器套接字获得任何响应。 ..此代码部分从服务器读取数据。 但是,直到关闭服务器上的套接字,我才能从服务器读取任何内容。服务器代码不受我控制,无法对其进行编辑。 如何从客户端代码中克服这个问题。 谢谢 问题答案: 为了在客户端和服务器之间进行通信,必须明确定义协议。 客户端代码将阻塞,直到从服务器收
我正在创建一个简单的服务器/客户端UDP套接字程序,我遇到了一个问题。 问题是recvfrom()函数一直在重新读取上次发送的数据。 因此,如果我从客户端向服务器发送两个数据包,那么recvfrom()将读取第一个数据包并打印其数据,然后它将不断地反复读取第二个数据包。 据我所知,一旦成功执行读取操作,数据包应该从套接字中删除,但这似乎没有发生。 我知道客户端没有重新发送数据,因为每当客户端发送数
我的springboot应用程序尝试从两个数据源(emwbis和backupemwbis)读取数据。我按照下面的链接配置了我的springboot应用程序以从两个不同的数据源读取数据。 http://www.baeldung.com/spring-data-jpa-multiple-databases 我的应用程序当前的问题是,它总是从主数据源(emwbis)读取数据。我已经编写了以下代码。 主数
我试图从Firebase数据库中读取数据,我已经到处阅读和查找,但我已经走到了死胡同。 这就是我所做的一切。 依赖项: 实现'com.google.firebase: Firebase存储: 9.2.1' 实现'com。谷歌。firebase:firebase数据库:9.2。1' 实现'com。谷歌。firebase:firebase授权:9.2。1' 实现'com。谷歌。火基:火基核心:9.2。
我正在读取火基数据库的数据。以下是存储在数据库中的数据的快照。 在以“8SS...”开头的快照字符串中,是用户的 uid。以下是用于从 firebase 数据库中检索数据的代码。 用户类包含getter和setters。 错误是只有结论。 如何评估 从值事件中读取时的错误是什么? 我尝试使用这个: 然后调用<code>ref。addListenerForSingleValueEvent()但仍然没
问题内容: 我们正在使用Oracle作为Web应用程序的数据库。该应用程序在大多数情况下都运行良好,但是出现了“不再需要从套接字读取数据”错误。 我们使用spring,hibernate,并且在我的应用程序上下文文件中有以下数据源。 我不确定这是由于应用程序错误,数据库错误还是网络错误引起的。 我们在oracle日志上看到以下内容 Oracle版本:11.2.0.1.0 问题答案: 对于此类错误,