JTable显示为空白

时间:2018-10-22 16:18:30

标签: java jtable jpanel jscrollpane

我只会发布重要的部分。

我创建了一个带有JPanel的JFrame,其中包含一些JTextField,JTextAreas和JList。我知道要添加一个JTable来显示一些结果,但是它将显示为空白。我尝试查看一些帖子,但无法修复。

我手动输入了2行,但没有出现。列名也不会。请帮忙!

import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class GUI_Automata_Ex_1 extends JFrame {

    public static int ScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
    public static int ScreenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

    public static int WindowWidth = ScreenWidth*3/4;
    public static int WindowHeight = WindowWidth*9/16;

    public static int unit = WindowWidth/160;

    // tables used
    static String[] columnNames = {"word", "length", "result"};
    static Object[][] data = {{"abbaa", new Integer (5), "belongs"}, {"baabbb", new Integer (6), "does not belong"}};
    public static JTable table_saved_words = new JTable(data, columnNames);
    public static DefaultTableModel dtm_saved_words = new DefaultTableModel();
    public static JScrollPane sp_saved_words;

    public GUI_Automata_Ex_1 () {

        // this will only run on ultrawide screens (e.g. 21:9 or 32:9) because the window is 16:9 optimized
        if (ScreenWidth/2 > ScreenHeight) {
            WindowHeight = ScreenHeight*3/4;
            WindowWidth = WindowHeight*16/9;
            unit = WindowWidth/160;
        }

        this.setTitle("Automata Theory 1st Excercise");
        this.setBounds(ScreenWidth/2-WindowWidth/2,ScreenHeight/2-WindowHeight/2,WindowWidth,WindowHeight);
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBackground(colorBG);

        Board board = new Board();
        this.setContentPane(board);
        this.setLayout(null);

        // TABLES

        // settings for table_saved_words
        table_saved_words.setBackground(Color.white);
        table_saved_words.setFont(fontM);
        table_saved_words.setModel(dtm_saved_words);
        table_saved_words.setPreferredScrollableViewportSize(new Dimension(unit*86, unit*50));
        table_saved_words.setFillsViewportHeight(true);
        sp_saved_words = new JScrollPane(table_saved_words);
        board.add(sp_saved_words);
        sp_saved_words.setBounds(unit*68, unit*32, unit*86, unit*50);
        sp_saved_words.setWheelScrollingEnabled(true);
        sp_saved_words.setViewportView(table_saved_words);
        sp_saved_words.setVisible(false);

        dtm_saved_words.addRow(new Object[]{"aabbbbaa", 5, "belongs"});

    }

    public class Board extends JPanel {
        public void paintComponent (Graphics g) {

        }
    }

}

}

这是屏幕截图:

https://i.stack.imgur.com/AHtLf.png

JTable位于右下角。我没有包括其他J组件的代码部分。

在获得要显示的列表之后,我想做的只是在窗口运行时添加一些行(用户输入的单词(是否属于词典)及其长度),但是我卡住了一部分就是让JTable显示列和数据。

1 个答案:

答案 0 :(得分:1)

您的表模型没有列,因此它永远不会显示您添加到其中的数据。

documentation for the zero-argument DefaultTableModel constructor

  

构造默认的DefaultTableModel,该表是零列零行的表。

最初,您的表具有一个自动创建的有效表模型:

JTable table_saved_words = new JTable(data, columnNames);

但是随后您创建了一个没有列的新模型:

DefaultTableModel dtm_saved_words = new DefaultTableModel();