将 JTable 安装在面板中

发布于 2024-11-08 18:35:32 字数 1007 浏览 0 评论 0原文

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

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

panel.add(qdbs, c);

我不希望表格位于滚动窗格中,但我确实希望表格占据面板的整个宽度。我将如何实现这个目标?

根据要求提供 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'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?

An SSCCE as requested:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

日裸衫吸 2024-11-15 18:35:32

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

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

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;
优雅的叶子 2024-11-15 18:35:32
import java.awt.*;
import javax.swing.*;

public class Test
{
    public static void main(String args[]) throws Exception
    {
        JPanel panel = new JPanel( new GridBagLayout() );
        JTable table = new JTable(5, 5);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(table, gbc);

        JFrame frame = new JFrame();
        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}

如果您需要更多帮助,请发布说明问题的 SSCCE

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

public class Test
{
    public static void main(String args[]) throws Exception
    {
        JPanel panel = new JPanel( new GridBagLayout() );
        JTable table = new JTable(5, 5);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(table, gbc);

        JFrame frame = new JFrame();
        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}

If you need more help post a SSCCE that demonstrates the problem.

呆橘 2024-11-15 18:35:32

我想 c 是一个 GridBagConstraint 或者类似的东西?最简单的方法是将 JPanelLayoutManager 设置为 BorderLayout,然后添加约束 BorderLayout .CENTER .

c is a GridBagConstraint or something along those lines, I imagine? The very simplest thing to do would be to set the LayoutManager of the JPanel to a BorderLayout, then just add with the constraint BorderLayout.CENTER .

辞慾 2024-11-15 18:35:32

嗯,

Alex Bliskovsky 写了 panel.add(qdbs, c);

这是错误的,不,永远不要这样做,你忘了包裹你 JTableScrollPane 然后你就可以使用一些 LayoutManagers,有关 LayoutManagers 的相关示例,请检查 GridBagConstraints for GrigBagLayout

hmmm

Alex Bliskovsky wrote panel.add(qdbs, c);

that's wrong, not, never do that, you are forgot wrap you JTable to the ScrollPane and then you can play with some of LayoutManagers, for related examples for LayoutManagers check GridBagConstraints for GrigBagLayout

聆听风音 2024-11-15 18:35:32

当调整框架大小时,以下框架将正确调整表格大小。

public class Sandbox {

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);
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        JTable testTable = new JTable(10, 2);
        panel.add(testTable, c);

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

}

The following frame will correctly resize the table when the frame is resized.

public class Sandbox {

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);
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        JTable testTable = new JTable(10, 2);
        panel.add(testTable, c);

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

}

栩栩如生 2024-11-15 18:35:32

也许与:

GridBagConstraints.fill = GridBagContraints.BOTH;

Perhaps with:

GridBagConstraints.fill = GridBagContraints.BOTH;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文