且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JTable网格线意外消失

更新时间:2023-12-04 13:32:16

一些注意事项供参考:

  • 我已经结合了@Reimeus的建议,但我不确定自己想要什么.

  • I've incorporated @Reimeus' suggestion, but I'm not really sure what you want.

我确定边界只是说明性的,但请注意

I'm sure the border is merely illustrative, but note that the API authors, "recommend that you put the component in a JPanel and set the border on the JPanel." In particular, the green border obscures part of the first table row.

请注意,网格线属于UI委托,如此处所示;如果这很关键,则计划对每个目标L&F进行测试.

Note that the gridlines belong to the UI delegate, as shown here; if this is critical, plan to test on each target L&F.

除非您有特定的原因要等待初始线程,请使用invokeLater().

Unless you have a specific reason to wait on the initial thread, use invokeLater().

如果JTable占主导地位,则覆盖Scrollable接口方法getPreferredScrollableViewportSize()是对一般准则的合理折衷

If the JTable dominates your initial appearance, overriding the Scrollable interface method getPreferredScrollableViewportSize() is a reasonable compromise of the general guidelines here.

使用pack()来利用组件的首选大小.

Use pack() to leverage a component's preferred size.

最后执行setVisible().

附录:根据您的更新TableGridProb2,我在封闭面板中添加了彩色边框,以查看效果更好.的确,com.apple.laf.AquaTableUI留下了一个像素的左边界,大概是用于选择矩形.您可以使用所选的L& F尝试此操作.

Addendum: Based on your update, TableGridProb2, I added a colored border to an enclosing panel to see better. Indeed, com.apple.laf.AquaTableUI leaves a one-pixel left margin, presumably for the selection rectangle. You might try this with your chosen L&F.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.plaf.ColorUIResource;

public class TableGridProb2 {

    public static void main(String[] args) {
        UIManager.put("Table.gridColor", new ColorUIResource(Color.gray));
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Table Grid Prob2");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JScrollPane sp = new JScrollPane(new JTable(20, 5));
                JPanel p = new JPanel(new GridLayout());
                p.setBorder(new LineBorder(Color.green, 4));
                p.add(sp);
                frame.getContentPane().add(p);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.plaf.ColorUIResource;

public class TableGridProb {

    public static void main(String[] args) {
        UIManager.put("Table.gridColor", new ColorUIResource(Color.gray));
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Table Grid Prob");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JPanel leftPanel = new JPanel(new BorderLayout());
                JTree jt = new JTree();
                jt.expandRow(1);
                leftPanel.add(jt, BorderLayout.WEST);
                JTable table = new JTable(20, 1) {
                    @Override
                    public Dimension getPreferredScrollableViewportSize() {
                        return new Dimension(300, 200);
                    }
                };
                table.setBorder(new LineBorder(Color.green, 4));
                table.setTableHeader(null);
                leftPanel.add(new JScrollPane(table), BorderLayout.CENTER);

                JSplitPane splitPane = new JSplitPane();
                splitPane.setLeftComponent(leftPanel);
                frame.add(splitPane);
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}