user2078802
user2078802

Reputation: 43

JTabbedPane not showing other tabs added

I have been looking around about JTabbedPane's and can't seem to find any solution.

I am using GridBagLayout as the manager as I am more comfortable with this (I am new to Java programming).

I have made the the class, and extended it with JPanel, and adding all the classes on another class, when i run program all i see is blank screen, i will post code so you can see.

(Test Class / GUI Class)

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class test1 extends JPanel {

    JTextField ID, Name, Address1, Address2,
            Post_Code, Landline, Mobile, Card_Name,
            Card_Number, Expiry_Date, Security_Number,
            Identification = new JTextField();
    ResultSet rs1;
    Connection conn;
    PreparedStatement pst;
    //JFrame frame = new JFrame("Test");
    String items[] = {"Yes", "No"};
    JComboBox box = new JComboBox(items);

    public test1() {
        JPanel jp2 = new JPanel(new GridBagLayout());
        GridBagConstraints c1 = new GridBagConstraints();
        c1.insets = new Insets(5, 5, 5, 5);
        c1.gridx = 1;
        c1.gridy = 0;
        c1.anchor = GridBagConstraints.CENTER;
        jp2.add(new JLabel("Customer Registration"), c1);
        c1.gridx = 0;
        c1.gridy = 1;
        c1.anchor = GridBagConstraints.EAST;
        jp2.add(new JLabel("ID"), c1);
        c1.gridx = 1;
        c1.gridy = 1;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(ID = new JTextField(10), c1);
        c1.gridx = 0;
        c1.gridy = 2;
        c1.anchor = GridBagConstraints.EAST;
        jp2.add(new JLabel("Name"), c1);
        c1.gridx = 1;
        c1.gridy = 2;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(Name = new JTextField(10), c1);
        c1.gridx = 0;
        c1.gridy = 3;
        c1.anchor = GridBagConstraints.EAST;
        jp2.add(new JLabel("Address"), c1);
        c1.gridx = 1;
        c1.gridy = 3;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(Address1 = new JTextField(15), c1);
        c1.gridx = 1;
        c1.gridy = 4;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(Address2 = new JTextField(15), c1);
        c1.gridx = 0;
        c1.gridy = 5;
        c1.anchor = GridBagConstraints.EAST;
        jp2.add(new JLabel("Post Code"), c1);
        c1.gridx = 1;
        c1.gridy = 5;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(Post_Code = new JTextField(10), c1);
        c1.gridx = 0;
        c1.gridy = 6;
        c1.anchor = GridBagConstraints.EAST;
        jp2.add(new JLabel("Landline"), c1);
        c1.gridx = 1;
        c1.gridy = 6;
        c1.anchor = GridBagConstraints.WEST;
        jp2.add(Landline = new JTextField(10), c1);
    }
}

The class that combines all JTabbedPane's together:

import javax.swing.JFrame; import javax.swing.JTabbedPane; import javax.swing.SwingUtilities;

public class tabadd extends JFrame {

JTabbedPane tab = new JTabbedPane();
Customer_Registration CR = new Customer_Registration();
test1 g = new test1();

public tabadd() {
    tab.add("Customer Registration", CR);
    tab.add("Equipment Registration", g);
    getContentPane().add(tab);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            tabadd m = new tabadd();
            m.setTitle("Test");
            m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            m.setSize(1280, 720);
            m.setLocationByPlatform(true);
            m.setVisible(true);
        }
    });
}

}

there will be multiple classes hence the Customer_Registration class.

Once again thanks for all your help.

Upvotes: 3

Views: 1190

Answers (1)

Dan D.
Dan D.

Reputation: 32391

You are not adding the jp2 panel anywhere. Add the following line at the end of the test1 class constructor.

add(jp2);

Another choice is to have the GridBagLayout set as the layout manager to the test1 JPanel and add all the components directly to it. This way you avoid using an extra jp2 panel.

And please use the standard Java naming for classes, such as test1 becomes Test1.

Upvotes: 2

Related Questions