【问题标题】:Updating JTabbedPane when new tab is added添加新选项卡时更新 JTabbedPane
【发布时间】:2014-04-15 10:30:12
【问题描述】:

我正在做一个项目,遇到了一点逻辑错误,希望你们能解决这个问题。

我正在构建一个将显示 SQL 数据库(以及其他内容)的应用程序。目前,按照我的设置方式,我在容器 (BorderLayout.CENTER) 中有一个 JTabbedPane,这并不是说这真的是相关信息。

任何人,一旦用户连接到数据库(并最终选择要查看的“表”),我想添加一个选项卡。但是现在,只有一个表要显示。

因此,当用户点击“连接”时,理想情况下连接会成功,此时 JTable 会填充数据库信息。

一旦此表初始化并准备就绪,请将其添加到新的 JPanel,然后将该面板添加到 JTabbedPane。

这就是错误出现的地方。我“相信”到目前为止我的逻辑是正确的,并且我没有收到任何编译器/运行时错误,只是没有显示新选项卡(如果我点击它应该是)什么都没有发生。

以下是我的一些代码,如果有什么需要澄清的,请不要犹豫!

这是 Table_Builder 类代码(一旦它正常工作我会清理它!)

public class Table_Builder extends Framework
{
    private DefaultTableModel updated_table_model;
    private JTable updated_table;
    private JScrollPane table;

public Table_Builder()
{
    // no implemention needed
}

public Table_Builder(Vector rows, Vector columns)
{
    updated_table_model = new DefaultTableModel(rows, columns);
    updated_table = new JTable(updated_table_model);

    updated_table.setCellSelectionEnabled(true);
    updated_table.setFillsViewportHeight(false);

    table = new JScrollPane(updated_table); 

    JPanel tab2 = new JPanel();
    tab2.add(table);

    tab2.setVisible(true);

    center.add("Table Viewer", tab2);

    // I'm thinking some sort of listener needs to be active, so it knows I'm adding a new
    // tab, but I'm not sure how this actually works.
    center.addPropertyChangeListener("foregroud", null);
    center.repaint();

    // center has already been added to container so i don't think that needs to be done again?
}

框架

 protected void center_panel()
 {
    JPanel tab1 = new JPanel();

    tab1.add(//emitted);

    center.setPreferredSize(new Dimension(1340, 950));
    center.setBackground(new Color(90, 90, 90));
    center.addTab("Tab1", tab1);

    container.add(center, BorderLayout.CENTER);
}

最好的问候, 迈克

更新:

Framework 具有我用来构建“Frame”的这些变量 框架为边框布局(东、西、北、南、中)

protected JTabbedPane center // this is the center panel
protected Container container // this will house all panels to be added

如上所示,我目前正在添加标签

1.) 创建一个新的 JPanel 2.)将(需要显示的内容)添加到jpanel 3.) 将该 jpanel 添加到 JTabbedPane

这是由

center.addTab("Tab name here", panel to be added);

这个的javadoc说

center.addTab("String title", Component component);

这按预期工作,我遇到的问题是这是在服务器连接之前完成的。用户连接到服务器后,我想添加一个新选项卡,该选项卡是从 Table_Builder 完成的,它继承自 Framework(这就是为什么中心和容器受到保护而不是私有的原因)。

【问题讨论】:

  • 没有"foregroud" 属性。
  • 发布一个简短的可运行程序来复制此问题。您的代码缺少一些重要的细节。
  • 您在寻找什么详细信息?而且我相信根据javadoc有一个“前景”属性。不是说这是我需要的,但我只是在测试一些东西。
  • center.add("Table Viewer", tab2); 看起来很不对劲。不能说,因为你的代码缺乏细节......比如什么是中心。遵循 peeskillet 的建议。
  • 已添加更新,添加选项卡的语法正确(当前在应用程序启动时有效)我正在尝试在满足条件(服务器连接)后构建/添加一个,并且应用程序已经启动并运行。

标签: java swing user-interface jpanel jtabbedpane


【解决方案1】:

在您的中心调用 revalidate(),然后重新绘制。

【讨论】:

  • 在我的 table_builder 构造函数下,我调用了 center.revalidate(),然后调用了 center.repaint(),同样的问题没有显示出来
  • 我也在我的容器上尝试了 revalidate() repaint()
【解决方案2】:

您在构造函数中添加选项卡的代码如下:

JPanel tab2 = new JPanel();
tab2.add(table);

tab2.setVisible(true);

center.add("Table Viewer", tab2);

// I'm thinking some sort of listener needs to be active, so it knows I'm adding a new
// tab, but I'm not sure how this actually works.
center.addPropertyChangeListener("foregroud", null);
center.repaint();

有2个错误和很多不必要的行。错误是:

  • center.add("Table Viewer", tab2); 正在使用Container 类的add 函数。当你想使用center.addTab("Table Viewer", tab2);时。

  • 只是为了澄清@peeskillet 指出的内容,没有“前景”属性,也没有“前景”(根据您的评论),而是“前景”属性。

现在您需要做的只是以下几点:

JPanel tab2 = buildTableViewerTab();
center.addTab("Table Viewer", tab2);

buildTableViewerTab()(返回 JPanel)是创建所需 JPanel 所需的代码。只需创建组件并将其正确添加到 tabbedPane。

为了展示这段代码是如何工作的,这里有一个简单的可执行应用程序来演示这个功能。同样,@peeskillet 在他的第二条评论中问你的是做同样的例子,但以你自己的方式和你的代码展示你遇到的错误。虽然这样做你可能会找到它们。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public class AddTabsExample
{
    public static final void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new AddTabsExample();
            }
        });
    }

    public AddTabsExample()
    {
        JFrame frame = new JFrame("Tab adder frame");

        final JTabbedPane tabbedPane = new JTabbedPane();

        frame.add(tabbedPane);

        JButton addButton = new JButton("Add tab");
        addButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                JPanel newTabComponent = new JPanel();
                int tabCount = tabbedPane.getTabCount();
                newTabComponent.add(new JLabel("I'm tab " + tabCount));
                tabbedPane.addTab("Tab " +tabCount, newTabComponent);
            }
        });
        frame.add(addButton, BorderLayout.SOUTH);

        addButton.doClick(); //add the first tab

        frame.setSize(800, 300);//frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}

执行结果:

【讨论】:

  • DSquare,感谢您的回复,我发现了错误,虽然我的前景可能不正确,但您知道我的意思。回到我最初的问题,我的错误是当我扩展框架时,它创建了框架的一个新实例,所以它被添加到其中,只是不可见。我已经实现了我的代码来访问必要的组件,这样当满足所需的“条件”时,就会将一个面板添加到选项卡窗格中。当我有更多时间时,我会查看上面的代码,看看我是否能够更好地实现我自己的代码。
猜你喜欢
  • 2013-07-06
  • 2021-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多