Hello,

So I've big project (about conserts) going on, and this project, I'll handle all GUI. Since we need a few windows (one for registration artist, hall, concert. one for "Buy ticket", one for reviews etc.), we decided to put all these things on tabs, so we only have one windows, but with a multiple tabs where you can switch.
I was just wondering, I've never programmed with tabs before, and I'm looking to read about it. Still, I've question.

Since we're going to have a few tabs (4-5), do we have to program all code in one class? Or is it possible to divide it in a couple of classes? Like one for registration for artist, one for registration for hall etc.
If we need to have all code in one class, we'll mostly end up with 2000+ lines.

Also, what do you think about this idea? One window, multiple tabs. There will be an option to login for an admin, since the admin is the one who's going to registert artists, halls, etc. So we'll have two windows, one for admin (registrations), and one for the customers (possible to read reviews, buy tickets for conserts etc):

But how should I do it? I don't clearly understand that.

Right now, I have this code:

   public class TabbedWindow extends JFrame {

    private JTextField search;
    private JButton searchButton;
    private JTabbedPane tab;


    public TabbedWindow() {
        super("Name");
        setSize(700,500);
        tab = new JTabbedPane();
        searchButton = new JButton("Search");
        search = new JTextField(33);

        // add tabs
        getContentPane().add(tab);

        // Creating panels so I can add components, also my tabs. 
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();
        JPanel jp4 = new JPanel();

        // Creating labels to put text
        JLabel label1 = new JLabel();
        JLabel label2 = new JLabel();
        JLabel label3 = new JLabel();
        JLabel label4 = new JLabel();

        label1.setText("Search: ");
        label2.setText("This is tab 2");
        label3.setText("This is tab 3");
        label4.setText("This is tab 4");

        // put the components on tabs
        jp1.add(label1);
        jp1.add(search);
        jp1.add(searchButton);
        jp2.add(label2);
        jp3.add(label3);
        jp4.add(label4);

        tab.addTab("Front Page", jp1);
        tab.addTab("Buy tickets", jp2);
        tab.addTab("Information", jp3);
        tab.addTab("Admin", jp4);


        setVisible(true);

    }

This code works, but if I keep doing it this way, I'll end up with 1000+ lines. Also, it's quite messy.
How can I make this easier? What I want is my panels on seperate classes, so I can call them to this "main" class and add them on this way, but I clearly dont understand how I should do this. I can make different GUI's, but it's much harder to let them fit on my tab-class. Simply, I want to create the panels/labels in seperate classes, but they shall be called in this class, and I want them to fit the GUI, the panel here. How should I do it? Could anyone show me an example or something?

Edited 4 Years Ago by Stjerne: spelling mistake

Make each panel a new class that extends JPanel. Put all the code associated with that panel into that class. From your main window class just create instances of your classes and add them to the main GUI.

For example you could make methods the create and populate jp1, jp2 etc and return a reference to the panel that could be added to tab.

This article has been dead for over six months. Start a new discussion instead.