JTable 未出现在 Window 中
JTable not appearing in Window
我正在创建一个 table 包含来自 Neo4j 数据库的数据。 java 代码连接到数据库,我的代码都写对了。 table 下方还会有一个进度条。进度条在 GUI 上显示正常,但 JTable 区域只是空白。这是 class.
中的一些代码
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 class。
当您创建 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();
}
}
我正在创建一个 table 包含来自 Neo4j 数据库的数据。 java 代码连接到数据库,我的代码都写对了。 table 下方还会有一个进度条。进度条在 GUI 上显示正常,但 JTable 区域只是空白。这是 class.
中的一些代码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 class。
当您创建 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();
}
}