为什么我的JTable没有出现?

2022-06-19 00:00:00 java swing jscrollpane jtable jtableheader

我的JTable没有显示。我不知道为什么。也许你能帮我。我有一个窗口类,它有一个右边的面板,右边的面板应该显示一个带有模型数据的表。我已经使用了虚拟数据开始,例如月和天。

这里是Right Panel类

import model.*;


import java.awt.*;
import java.awt.event.*;
//import java.text.*;
import javax.swing.*;


public class RightPanel extends JPanel implements View
{
    private TablePanel songsnartists = new TablePanel();
    Shop shop;

    public RightPanel(Shop ourShop)
    {   
        shop = ourShop;
        setup();
        build();
        setVisible(true);
    }

    private void setup()
    {   
        setPreferredSize(new Dimension(300, 300));
        setLayout(new GridLayout(3,2));
        setBorder(BorderFactory.createLineBorder(Color.blue));
    }

    private void build()
    { 
        add(songsnartists);
    }

    public void update()
    {}
}

/*到目前为止,只需尝试将表格面板添加到我的右侧面板就足够简单了*/

下面是我创建的TablePanel类。它继承JTable并使用继承AbstractTableModel的私有类MyTableModel,以便我创建不可编辑的表。

import javax.swing.*;
import javax.swing.table.*;


public class TablePanel extends JTable
{
    //private int ROWS = 3;
    //private int COLUMNS = 2;
    private final String [][] data = {{"Jan", "31"}, {"Feb","28"},{"Mar","31"}};
    private final String[] headers = {"Month", "Days"};
    JTable table;


    public TablePanel()
    {
        table = new JTable(new MyTableModel(data, headers));
        JTableHeader header = table.getTableHeader();
        //header.setBackground(Color.blue);
        setVisible(true);
    }

    private class MyTableModel extends AbstractTableModel
    {
        private String[] columnNames = {"Month", "Days"};
        private String[][] data = {{"Jan", "31"}, {"Feb","28"},{"Mar","31"}};

        public MyTableModel(String [][] data, String [] header)
        {
            int rows, cols, rowCounter, colCounter;

            rows = getRowCount();
            cols = getColumnCount();

            for (rowCounter=0; rowCounter < rows; rowCounter++)
                {
                    for (colCounter=0; colCounter < cols; colCounter++)
                        {
                            setValueAt(data[rowCounter][colCounter],rowCounter,colCounter);
                        }
                }           
        }


        public int getColumnCount()
        {
            return columnNames.length;
        }

        public int getRowCount()
        {
            return data.length;
        }

        public String getColumnName(int col)
        {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col)
        {
            return data[row][col];
        }

        public Class getColumnClass(int c) 
        {
            return getValueAt(0, c).getClass();
        }

        public boolean isCellEditable(int row, int col)
        {
            return false;
        }
    }
}

如您所见,抽象方法在私有类中被覆盖。有人能帮我演示一下为什么我的JTable没有显示吗?


解决方案

  • 阅读教程How to Use Tables

  • 将JTable放到JScrollPane

  • 不要忘记使用正确的LayoutManager将JScrollPane放到JPanel中,我看不出您在JPanel中添加了JTable,不确定新的GridLayout(3,2)是否适合LayoutManagerJTable view

相关文章