java - JTable 没有出现在窗口中
问题描述
我正在创建一个包含来自 Neo4j 数据库的数据的表。将java代码连接到数据库并且代码全部正确编写imo。表格下方还会有一个进度条。进度条出现在 GUI 上,但 JTable 区域只是空白。这是该课程的一些代码。
public class BiogramTable extends JFrame
{
//*********************************************************************************
//* CONNECTION TO NEO4J DATABASE BLOCK *
//*********************************************************************************
static Driver driver1 = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "****", "*******" ) );
static Session session1 = driver1.session();
static StatementResult resultVariable1;
static Record recordVariable1;
//Establish the POSTer
static MaxentTagger tagger1 = new MaxentTagger ("taggers/english-caseless-left3words-distsim.tagger");
//Create a query object
static Query neoQuery1 = new Query();
static String resultString1 = new String();
static String POSTedText1 = new String();
private JProgressBar bar;
private JTable selectionTable;
public static void main( String[] arg ) {
new BiogramTable();
}
public BiogramTable() {
selectionTable = new JTable( new TableModel() );
ListSelectionModel selec = selectionTable.getSelectionModel();
selec.addListSelectionListener( new TableSelectionListener() );
JScrollPane scroll = new JScrollPane( selectionTable );
getContentPane().add( scroll );
bar = new JProgressBar( 0, 10 );
bar.setStringPainted( true );
getContentPane().add( bar, BorderLayout.SOUTH );
setDefaultCloseOperation( EXIT_ON_CLOSE );
setSize( 300, 300 );
setVisible( true );
}
private class TableModel extends DefaultTableModel {
DefaultTableModel model=new DefaultTableModel()
{
public Class<?> getColumnClass(int column)
{
switch(column)
{
case 0: // |This is the first column
return Boolean.class; // |First column is set to Boolean as it will contain check boxes
case 1: // |This is the second column
return String.class; // |Second column set to String as it will contain strings
default:
return String.class; // |The table is set to String as default
}
}
};
public TableModel(){
//Create and run the query in the table
neoQuery1= Q1();
resultVariable1 = session1.run(neoQuery1.get());
//resultVariable = session.run(neoQuery.get());
//ASSIGN THE MODEL TO TABLE
//selectionTable.setModel(model);
model.addColumn("Select"); // |Column for Check boxes
model.addColumn("Bigrams"); // |Column for Bigrams
String values = new String ("");
while(resultVariable1.hasNext())
{
recordVariable1 = resultVariable1.next();
values = recordVariable1.get("w4").asString(); // |This is to add the data from database into table as string variables
model.addRow(new Object[]{false, values}); // |Put the data in the table as values
// |Notice that false in the value of the check box column
} //ENDWHILE
}
当我运行代码时,这是我得到的结果。
在屏幕截图中,您可以看到进度条正在显示,但应该在上面区域中的 JTable 没有显示。
我试过这个没有运气:
getContentPane().add(selectionTable, BorderLayout.NORTH);
解决方案
问题在于您的 TableModel 类。
当您创建 TableModel 的实例时,您应该将数据添加到您的 TableModel。
相反,您正在做的是将数据添加到TableModel 内的不同模型中:
DefaultTableModel model=new DefaultTableModel()
因此,当您使用 TableModel 创建新的 JTable 时,什么都不会显示,因为所有数据都包含在 DefaultTableModel 命名模型中,而不是您的 TableModel 实例中。
您不需要以下代码:
DefaultTableModel model=new DefaultTableModel()
{
public Class<?> getColumnClass(int column)
{
switch(column)
{
case 0: // |This is the first column
return Boolean.class; // |First column is set to Boolean as it will contain check boxes
case 1: // |This is the second column
return String.class; // |Second column set to String as it will contain strings
default:
return String.class; // |The table is set to String as default
}
}
};
而不是
model.addColumn("Select"); // |Column for Check boxes
model.addColumn("Bigrams"); // |Column for Bigrams
您应该只将数据添加到您的 TableModel 实例中:
addColumn("Select"); // |Column for Check boxes
addColumn("Bigrams"); // |Column for Bigrams
我完全不明白为什么需要扩展 DefaultTableModel,以下代码可以正常工作,而且更简单:
import javax.swing.*;
import java.awt.*;
public class BiogramTable extends JFrame {
public BiogramTable() {
JTable selectionTable = new JTable(
new Object[][]{
{ true, "Foo" },
{ false, "Bar"}
},
new Object[]{"Select", "Biagrams"}
);
JScrollPane scroll = new JScrollPane(selectionTable);
getContentPane().add(scroll, BorderLayout.CENTER);
JProgressBar bar = new JProgressBar(0, 10);
bar.setStringPainted(true);
getContentPane().add(bar, BorderLayout.SOUTH);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] arg) {
new BiogramTable();
}
}
推荐阅读
- javascript - 尝试使用 JS 和 Handlebars 添加用户并渲染新页面
- node.js - admin.auth(...).createSessionCookie 不是函数
- sql - 如何撤销列权限
- python - 在 IPython 中安装新模块
- sql - 动态将列转换为行,然后与其他表连接
- mysql - 带有通知的 MySQL 备份脚本
- java - Selenium 从下拉列表中选择值
- javascript - 我的firebase函数的错误没有记录到控制台
- javascript - 如何使用 NestJS 创建带参数的嵌套路由
- node.js - aws api网关客户端未执行。然后,它只是退出apigClient功能