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?