jtable的左行和上行是不可见的

vm0i2vca  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(369)

我试图展示一些关于jtables的数据:

private JTable table1 = new JTable() {

    private static final long serialVersionUID = 1L;

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

};

private JScrollPane secTabFirst() {

    Object [][] data = {{"90"},{"1700"},{"60"},{"0.7"}, {"Globular"}, {"Sinusoidal"}};

    String[] columnNames = {"Parameters"};

    DefaultTableModel model = new DefaultTableModel(data, columnNames);

    table1.setModel(model);

    table1.setTableHeader(null);
    table1.setShowHorizontalLines(true);
    table1.setShowVerticalLines(true);
    //table1.setPreferredSize(new Dimension(150,240));
    //table1.setMaximumSize(new Dimension(150,240));

    JScrollPane tb1 = new JScrollPane(table1);

    tb1.setVisible(true);

    return tb1;
}

public static void main(String args[]){

 JPanel panel = new JPanel();

 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

 panel.add(secTabFirst());

}

JTable如下所示:

但是table的左上两行是看不见的。另外,在jscrollpane中放置表之后,它会显示2个黑框。
我怎样才能让线条清晰可见而暗箱消失呢?
谢谢,
mb

0md85ypi

0md85ypi1#

我有一个不同的解决方案,对我来说似乎是可行的,我决定保持代码简单(我已经将所有代码包含在一个构造函数中):

import javax.swing.*;

public class GUI {
  conn conn = new conn();
    // frame
    JFrame f;
    // Table
    JTable j;

    // Constructor
    GUI()
    {
        // Frame initiallization
        f = new JFrame();

        // Frame Title
        f.setTitle("JTable Example");

        // Data to be displayed in the JTable
        String[][] data = {
                { "data", "data", "data" },
                { "data", "data", "data" }
        };

        // Column Names
        String[] columnNames = { "data", "data", "data" };

        // Initializing the JTable
        j = new JTable(data, columnNames);
        j.setBounds(30, 40, 200, 300);

        // adding it to JScrollPane
        JScrollPane sp = new JScrollPane(j);
        f.add(sp);
        // Frame Size
        f.setSize(500, 200);
        // Frame Visible = true
        f.setVisible(true);
    }

    // Main
    public static void main(String[] args)
    {
        new GUI();
    }
}

以下是输出:https://photos.app.goo.gl/itzwx8kxwnkbzsjt8
如果有任何问题,请回复这个答案

tnkciper

tnkciper2#

我试图复制你的问题但没有成功 JTable 在默认情况下,将永远不会显示顶部和左侧边框,但这不是一个问题,因为它通常会在 JScrollPane . 正如@hovercraftfullofeels提到的,请尝试给我们一个简短、自包含、正确(可编译)的示例

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class TestApp {

    public TestApp() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TestApp::new);
    }

    private void initComponents() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(new EmptyBorder(10, 10, 10, 10));

        JTable table = new JTable() {
            private static final long serialVersionUID = 1L;

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        Object[][] data = {{"90"}, {"1700"}, {"60"}, {"0.7"}, {"Globular"}, {"Sinusoidal"}};
        String[] columnNames = {"Parameters"};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table.setModel(model);
        table.setTableHeader(null);

        JScrollPane pane = new JScrollPane(table); // borders for top and left will be rendered thanks to JScrollPanes borders
        panel.add(pane);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

}

更新:
不过,您可以设置 Border 确保绘制顶部和左侧边界线:

table.setBorder(new LineBorder(Color.GRAY, 1));

产生( JTable 不是在一个 JScrollPane 在下面的屏幕截图中显示无干扰的边界):

相关问题