1

I want to be able to add multiple tabs to my program. The tabs will contain jTable components. I would like to be able to click add a new tab, type in some information, and add some values to the table and then insert it into the tabbedPane. The SSCE that i have below does this but when you try to add a second one the table does not show up in the dialog. Can anyone help me figure out why? (FYI: I'm using Netbeans and the GUI designer)

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;


public class tabtest extends javax.swing.JFrame {

    public tabtest() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        addTabDialog = new javax.swing.JDialog();
        titleTextBox = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        addTabConfirmButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        tabPane = new javax.swing.JTabbedPane();
        addTabButton = new javax.swing.JButton();

        addTabDialog.setBounds(new java.awt.Rectangle(0, 0, 400, 400));

        titleTextBox.setText("New Tab");

        jLabel1.setText("Tab Title");

        addTabConfirmButton.setText("Confirm");
        addTabConfirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTabConfirmButtonActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "assignment", "points", "score"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout addTabDialogLayout = new javax.swing.GroupLayout(addTabDialog.getContentPane());
        addTabDialog.getContentPane().setLayout(addTabDialogLayout);
        addTabDialogLayout.setHorizontalGroup(
            addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addTabDialogLayout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(titleTextBox, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(addTabDialogLayout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(addTabConfirmButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addTabDialogLayout.createSequentialGroup()
                .addContainerGap(43, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32))
        );
        addTabDialogLayout.setVerticalGroup(
            addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addTabDialogLayout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addGroup(addTabDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(titleTextBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(addTabConfirmButton)
                .addGap(212, 212, 212))
        );

        jLabel2.setText("This is a new Tab");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        addTabButton.setText("add new tab");
        addTabButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTabButtonActionPerformed(evt);
            }
        });
        tabPane.addTab("+", addTabButton);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabPane, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(64, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabPane, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(111, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void addTabButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        addTabDialog.setLocationRelativeTo(null);
        addTabDialog.setVisible(true);        
    }                                            

    private void addTabConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        JTable table=jTable1;
        tabPane.insertTab(titleTextBox.getText(),null,new JScrollPane(table),"Hello",tabPane.getTabCount()-1);
        //tabPane.addTab(titleTextBox.getText(),newTab);
        tabPane.setSelectedIndex(tabPane.getTabCount()-1);
        addTabDialog.setVisible(false);
    }                                                   

    public static void main(String args[]) {
       java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new tabtest().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton addTabButton;
    private javax.swing.JButton addTabConfirmButton;
    private javax.swing.JDialog addTabDialog;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTabbedPane tabPane;
    private javax.swing.JTextField titleTextBox;
    // End of variables declaration                   
}

Also... is it better to use javaFX2 then swing? would it make somethings like this easier? and is it a lot different then coding in swing? is there a lot to learn?

1 Answer 1

4

You only create one JTable and then try to re-use it throughout your program, and since a component can only be shown once in a GUI, that means that the JTable will only be visible in one location. Instead you need to create a new JTable when a new one is needed.

As a proof of concept, if you change this line in your addTabConfirmButtonActionPerformed(...) method:

  JTable table = jTable1;

to this:

  // !!!!! changes
  // JTable table = jTable1;
  JTable table = new JTable(); //!!
  table.setModel(jTable1.getModel()); //!!
  // !!!!! end of changes

You will see new JTables. Note that this is not a decent solution for your code since each JTable will now share the same model, something that I don't think that you desire. You should create another class that extends DefaultTableModel or AbstractTableModel and give an instance of this class to each new JTable.


Edit
You ask in comment:

As for the same models im going to create an AbstractTableModel, but couldnt I just apply that model to jTable1 in my code, and then it will be the same as table?

The model that you add to jTable1 will apply for the object that jTable1 references, no more, no less. If you want all JTables displaying exactly the same information in each cell, then they all can share the same model. I doubt that you want this.

(I might be confused about what a table model is, is it when column 2 contains int, column 1 contains strings etc? or is it all of the data in it too?)

The table model holds the table's data as well as its logical structure -- number of columns, rows, etc.

Please have a look at the JTable Tutorial for all the gory details.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I knew it was something like that. As for the same models im going to create an AbstractTableModel, but couldnt I just apply that model to jTable1 in my code, and then it will be the same as table? (I might be confused about what a table model is, is it when column 2 contains int, column 1 contains strings etc? or is it all of the data in it too?)
your awesome. haha great explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.