JTable没有出现在Window

时间:2018-05-01 10:10:14

标签: java user-interface jtable

我正在创建一个包含Neo4j数据库数据的表。 java代码连接到数据库,代码都是正确编写的。该表下方还有一个进度条。 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      
    }

当我运行代码时这是我得到的结果。

The Results

在屏幕截图中,您可以看到进度条正在显示,但是不显示应该位于上方区域的JTable。

我试过这个没有运气:

getContentPane().add(selectionTable, BorderLayout.NORTH);

1 个答案:

答案 0 :(得分:1)

问题在于您的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();
    }
}