在面板内安装JTable [英] Fitting a JTable Inside a Panel

查看:50
本文介绍了在面板内安装JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JTable并将其添加到使用gridbaglayout的面板中,如下所示:

I'm using a JTable and adding it to a panel which uses a gridbaglayout like so:

JTable qdbs = new JTable(rowData, columnNamesVector);
qdbs.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

panel.add(qdbs, c);

我不希望表格出现在滚动窗格中,但是我希望表格占据面板的整个宽度.我该怎么办?

I don't want the table to be in a scroll pane, but I do want the table to take up the entire width of the panel. How would I accomplish this?

要求的SSCCE:

import javax.swing.*;
import java.awt.*;

public class Main{

    public static void main(String[] args) {
    new TestFrame();
    }

    public static class TestFrame extends JFrame{
    public TestFrame() {
        this.setTitle("SSCCE");

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0;
        c.gridy = 0;

        c.insets = new Insets(10,10,10,10);
        JTable testTable = new JTable(10,2);
        panel.add(testTable, c);

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

}

我希望此表始终占据面板的整个宽度(插图除外).当前,在调整框架大小时,表格的大小不会更改.

I would like this table to always take up the entire width of the panel (except the insets). Currently the table does not change size when the frame is resized.

推荐答案

您需要添加约束条件以告诉布局如何处理更多空间.在您的SSCCE中添加以下项目:

You need to add constrains to tell the layout what to do with more space. In your SSCCE add these items:

  c.fill = GridBagConstraints.BOTH;
  c.weightx = 1;
  c.weighty = 0;

这篇关于在面板内安装JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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