简体   繁体   English

如何在没有JScrollpane的情况下创建JTable

[英]How to make a JTable without a JScrollpane

I have done some pretty decent searching and i cant quite fine what im looking for. 我做了一些相当不错的搜索,我不能正常寻找。 basically i want a JTable in a JPanel and for that JPanel to re-size itself per the size of the JTable. 基本上我想在JPanel中使用JTable,并且JPanel根据JTable的大小重新调整自身大小。 i know the usual way of implementing a JTable is to use the JScrollpane but in this circumstance it is unnecessary and it makes my program look ... messy. 我知道实现JTable的通常方法是使用JScrollpane但在这种情况下它是不必要的,它使我的程序看起来......凌乱。

the reason i cant find the answer i need is everytime someone asks about a JTable in a JPanel someone gives them the answer of putting it in the scrollpane. 我无法找到我需要的答案的原因是,每当有人询问JPanel中的JTable时,有人会给出答案,将它放在滚动窗格中。 im aware of the advantages to using the scrollpane but in this circumstance they aren't advantageous. 我意识到使用滚动板的优点,但在这种情况下它们并不是有利的。

  1. yes is possible add JTable to the JPanel , notice with a few row that fills real screen resolution 是可以将JTable添加到JPanel ,注意几行填充真正的屏幕分辨率

  2. you have to add TableHeader separatelly to the container too, otherwise isn't added, nor to be visible, for JTable into JScrollPane isn't required add or define TableHeader separatelly 您必须将TableHeader单独添加到容器中,否则不会添加,也不可见,因为JTable不需要JScrollPane添加或定义TableHeader

  3. better could be to remove borders from JScrollPane.setBorders(null) and with to disable VERTICAL and HORIZONTAL_SCROLLBAR to NEVER 更好的方法是从JScrollPane.setBorders(null)删除边框,并禁用VERTICALHORIZONTAL_SCROLLBARNEVER

  4. for shrinking the wider JScrollPane to fits to the JTables Dimension is possible to use table.setPreferredScrollableViewportSize(table.getPreferredSize()); 缩小更宽的JScrollPane以适应table.setPreferredScrollableViewportSize(table.getPreferredSize()); JTables Dimension可以使用table.setPreferredScrollableViewportSize(table.getPreferredSize());

Try to override the getPreferredSize() method of your JPanel and return the value of your JTable's getPreferredSize() : 尝试覆盖JPanel的getPreferredSize()方法并返回JTable的getPreferredSize()

final JTable table = new JTable();

JPanel panel = new JPanel() {
    public Dimension getPreferredSize() {
        return table.getPreferredSize();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM