JTable网格线意外消失 [英] JTable grid lines disappear unexpectedly

查看:82
本文介绍了JTable网格线意外消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个S(非常S)SCCE,它说明了这个问题:在具有BorderLayout的JPanel中包含JTable和JTree. JTable中的大多数网格线均显示,但左侧未显示,顶部也未显示.尝试过将边框放在JTable自己专用的JPanel上的尝试.

I have an S (very S) SCCE which illustrates the problem: there is a JTable and a JTree both contained in a JPanel with a BorderLayout. Most of the grid lines in the JTable show, but not the one at the left, and not the one at the top. Have tried experimenting with borders, with placing the JTable on its own dedicated JPanel, etc.

我从专家那里得知,调用任何setXXXSize方法都是一个坏主意,因此,例如,请不要诉诸于setPreferredSize.在SSCCE中,您可以通过取消注释"EAST"行来交换树和表的位置.

I have gleaned from the experts that it is a bad idea to call any of the setXXXSize methods, so have not resorted to setPreferredSize, for example. In the SSCCE you can swap the tree and table positions round by uncommenting the "EAST" line)

如何使这些模糊/缺失的网格线重新出现?

How do I make these obscured/missing gridlines reappear?

(注意,最终的想法是使表随着JTree动态增长/缩小,因此表和树中总是有相同数量的行...因此希望将两个组件都包含在同一行中JPanel).

(NB the idea is eventually for the table to grow/shrink dynamically with the JTree, so that there are always the same number of rows in the table and the tree... hence the desire to contain both components on the same JPanel).

SSCCE:

import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
import javax.swing.border.LineBorder;


public class TableGridProb {
    public static void main( String[] args ) throws InterruptedException, InvocationTargetException{

        EventQueue.invokeAndWait( new Runnable(){

            @Override
      public void run() {
          JFrame frame = new JFrame( "Table Grid Prob" );
          frame.setSize( 800, 300 );
          frame.setVisible( true );
          JSplitPane split_pane = new JSplitPane();
          JScrollPane split_jsp = new JScrollPane( split_pane );
          JPanel left_panel = new JPanel();
          left_panel.setLayout( new BorderLayout() );
          JTree jt = new JTree();
          jt.setRowHeight( 20 );
          left_panel.add( jt, BorderLayout.WEST );
//        left_panel.add( jt, BorderLayout.EAST );

          JTable jtable = new JTable( 4, 1  );

//        jtable.setBorder( new LineBorder( Color.green, 3 ));

          jtable.setRowHeight( 20 );
          left_panel.add( jtable, BorderLayout.CENTER );
          split_pane.setLeftComponent( left_panel );
          frame.getContentPane().add( split_jsp );
          split_pane.setDividerLocation( 0.5d );

      }
        });
    }
}

以后

later

为回答我要做的就是将这个JTable封装在JScrollPane中……至少在标准的Java L&F中,这似乎无法解决问题.我还尝试了将JTable放在JPanel上的JScrollPane上,将JTable放在自己的JPanel上并为后者提供LineBorder等各种排列方式.问题似乎有些棘手.我的主要嫌疑人是Insets和布局管理器...但是 1)在此代码过程中均未调用JTable的getInsets方法 2)也许BorderLayout并不理想.我确实尝试了GridLayout和BoxLayout,但是据我所记得,这些都不能解决问题...

In answer to the suggestion that all I have to do is wrap this JTable in a JScrollPane... this doesn't appear to solve the problem, at least with the standard Java L&F. I have also tried various permutations of sitting the JTable on a JScrollPane on a JPanel, sitting the JTable on its own JPanel and giving the latter a LineBorder, etc. The problem appears to be a bit trickier. My chief suspects are Insets and the layout managers... but 1) neither of the getInsets methods of the JTable is called in the course of this code however 2) maybe a BorderLayout is not ideal. I did try GridLayout and BoxLayout but, as I recall, neither of these solved the problem...

以后

later

甚至更简单的SSCCE可以说明问题(看不到左侧的垂直网格线)

even simpler SSCCE which illustrates the prob (cannot see left-hand vertical gridline)

import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;


public class TableGridProb2 {
  public static void main( String[] args ) throws InterruptedException, InvocationTargetException{
    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 ));
        frame.getContentPane().add( sp );
        frame.pack();
        frame.setVisible( true );
      }
    });
  }
}

*稍后(9月6日)*

两个SSCCE的屏幕截图:

screenshots for the two SSCCEs:

希望您能看到问题:在两种情况下都缺少垂直的左侧网格线(以及顶部网格线出现的情况)...

hope you can see the problem: missing vertical left-hand gridline (and top gridline it appears) in both cases...

推荐答案

一些注意事项供参考:

  • 我已经结合了@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);
            }
        });
    }
}

这篇关于JTable网格线意外消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆