繁体   English   中英

添加新标签时更新JTabbedPane

[英]Updating JTabbedPane when new tab is added

我正在做一个项目,并且遇到了一些逻辑错误,希望你们中的一个可以解决这个问题。

我正在构建一个将显示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);
}

最好的问候,迈克

更新:

框架具有这些用于构建“框架”的变量。框架是边界布局(东,西,北,南,中)

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(这就是为什么center和container是受保护的而不是私有的)。

在构造函数中添加标签的代码如下:

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指出的内容,没有“ foregroud”属性,也没有“ forground”(根据您的评论),而是“ foreground”属性。

现在您只需要执行以下操作:

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);
    }
}

执行结果:

例

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

暂无
暂无

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

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