添加新标签后更新JTabbedPane [英] Updating JTabbedPane when new tab is added

查看:166
本文介绍了添加新标签后更新JTabbedPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a project, and have run into a little bit of a logic error, hopefully one of you can clear this up.

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

I'm building an application that will display a SQL database (among other things). Currently, the way I have things set up, I have a JTabbedPane inside a Container (BorderLayout.CENTER) not that this is really pertinent information.

任何人,我都想在用户连接到数据库后添加一个选项卡(并最终选择要查看的表".但是,目前仅显示一个表.

Anywho, I would like to add a tab once the user has connected to a database (and eventually selected which 'table' to see. For now however, there is only one table to be displayed.

因此,当用户单击连接"时,理想情况下,连接将成功,此时将使用数据库信息填充JTable.

So, when the user hits 'Connect', ideally the connection will be successful, at which point in time a JTable is populated with the database information.

此表初始化并准备就绪后,可以将其添加到新的JPanel中,并将该面板添加到JTabbedPane中.

Once this table is initialized and ready to go, add it to a new JPanel, and add that panel to the JTabbedPane.

这是错误的出处.到目前为止,我相信"我的逻辑是正确的,并且没有收到任何编译器/运行时错误,只是不显示新选项卡(如果我单击应该在何处显示)是)什么都没发生.

This is where the error comes in. I 'believe' my logic thus far is correct, and I don't get any compiler/runtime errors, the new tab just isn't shown (and if I click where it should be) nothing happens.

下面是我的一些代码,如果有任何需要澄清的地方,请随时询问!

Below is some of my code, if anything needs clarified please don't hesitate to ask!

这是Table_Builder类的代码(一旦其正常工作,我将对其进行清理!)

This is the Table_Builder Class code (I will clean it up once it is working properly!)

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 has these variables I am using to build the 'Frame' Framework is a borderlayout (east, west, north, south, center)

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

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

As seen above, I am currently adding tabs by

1.)创建一个新的JPanel 2.)添加(无论需要显示什么)到jpanel 3.)将jpanel添加到JTabbedPane

1.) creating a new JPanel 2.) adding (whatever needs to be displayed) to the jpanel 3.) adding that jpanel to the JTabbedPane

这是通过

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

对此的Javadoc说

The javadoc for this says

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

这可以按预期工作,我遇到的问题是,这是在服务器连接之前完成的.用户连接到服务器后,我想添加一个新标签,该标签是从Table_Builder完成的,该标签继承自Framework(这就是为什么center和container是受保护的而不是私有的)的原因.

This works as intended, the problem I am encountering, is that this is done prior to server connection. After the user connects to the server, I would like to add a new tab, which is being done from Table_Builder, which inherits from Framework (which is why center and container are protected and not private).

推荐答案

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

Your code for adding a tab in the constructor is the following:

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个错误和很多不必要的行.错误是:

There are 2 errors and a lot of unnecessary lines. The errors are:

  • center.add("Table Viewer", tab2);使用的是Container类的add函数.当您想使用center.addTab("Table Viewer", tab2);.

  • center.add("Table Viewer", tab2); is using the add function of the Container class. When you wanted to use center.addTab("Table Viewer", tab2);.

只是为了清除@peeskillet指出的内容,没有"foregroud"属性,也没有"forground"(根据您的评论),而是"foreground"属性.

Just to clear up what @peeskillet was pointing out, there is not a "foregroud" property, nor a "forground" (as per your comment), but a "foreground" property.

现在您需要做的只是以下事情:

Now what you need to do is just the following:

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

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

Where buildTableViewerTab() (returning a JPanel) is the code necessary to create the JPanel that you desire. Just create the component and add it to the tabbedPane properly.

在这里展示此代码的工作方式是一个演示此功能的简单可执行应用程序.同样,@ peeskillet在他的第二条评论中问您的是同样的示例,但是以您自己的方式进行,并且您的代码演示了您遇到的错误.尽管这样做,您可能会找到它们.

To show how this code works here is a simple executable application demonstrating this functionality. Again, what @peeskillet was asking you in his second comment is to do this same example but in your own way and with your code demonstrating the errors you were encountering. Although doing this you probably would have found them.

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

执行结果:

这篇关于添加新标签后更新JTabbedPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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