Updating JTabbedPane when new tab is added

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.

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.

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.

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!

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?
}

Framework

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

Best Regards, Mike

UPDATE:

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.) creating a new JPanel 2.) adding (whatever needs to be displayed) to the jpanel 3.) adding that jpanel to the JTabbedPane

this is done by

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

The javadoc for this says

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

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).

Answers


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

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

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

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

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.

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

Execution result:


call revalidate() on your center, then repaint.


Need Your Help

Can we have an AtomicEnum in Java?

java multithreading enums synchronized volatile

Today I saw this utility class for AtomicEnum but I wonder if it is possible to have an atomic enum why it is not included in Java standard libraries? In fact I have so much doubt if it can be really

How to compare two "Comparable" type variables in Java?

java comparator comparable

I'm currently working on a University assignment. I have a piece of code that looks like the following.