我正在创建一个包含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时,由于所有数据都包含在名为Model的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();
}
}