0

First of all, im kinda new with Java and GUI programming. Im trying to find the right way to build one of my first GUI apps with 'multiple pages/tabs' (sorry im a webdev).

This is the idea behind the app:

  • There is a menu where the user can select a 'page'
  • There is a label representing the app name
  • And finally there is JPanel where I want the content of the 'page'.

http://i.imgur.com/wIyceUh.png
http://i.imgur.com/Oqrk0mA.png

Am I doing this the right way? I feel kinda stuck and I dont know if im on the right track.

To be more clear: Whats the right/best way to make a GUI with mutliple pages/tabs.

Dylan
  • 158
  • 1
  • 2
  • 10
  • Your question is very broad, perhaps overly broad, and so might get closed. But having said that, I suggest 1) ditching the GUI builder and learning to code your GUI by hand, using the tutorials as a guide. This will give you a deeper understanding of the GUI library. 2) Create each major "view" JPanel as its own class, one that you can run and test in isolation. 3) Once these have been debugged, ad them to your larger main program. 4) Consider swapping views using a CardLayout. 5) Keep writing lots of code, and coming here with code when your stuck on specific problems. – Hovercraft Full Of Eels Jul 20 '14 at 19:36
  • For better help on how to use tabbed panes, please see the official [tabbed pane tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html). But again, your question sounds as if you'd be far better off using a CardLayout and not a JTabbedPane. For the CardLayout tutorial, please look [here](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). – Hovercraft Full Of Eels Jul 20 '14 at 19:45
  • Also consider having a look at [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) as an alternative to `JTabbedPane`, might or might not meet your needs, but it's worth a comparision – MadProgrammer Jul 21 '14 at 00:59

3 Answers3

1

Alright, I will give you some example code for making a multiple tabs with Swing

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;

public class GUI extends JFrame {
    private JTabbedPane tabManager = new JTabbedPane(JTabbedPane.TOP); // This will manage the tabs

    // Some GUI code...

    public void addTab(String name, JPanel panel) { // Takes in a name for the tab and 
                                        // a panel for the tab to display when clicked

        getContentPane().add(tabManager);

        tabManager.addTab(name, null, panel, name); // The first name is the name of 
                                                    // the tab and second one is what 
                                                    // is display in the little popup 
                                                    // thing when you hover the mouse 
                                                    // over the tab
    }

    public void removeTab(int index) { // Removes a tab at the index
        tabManager.removeTabAt(index);
    }

    // Some more GUI code...

    public static void main(String[] args) { // In main method or anywhere else
        GUI gui = new GUI(/*constructorParams*/);
        JPanel panel = new JPanel();
        panel.add(new JButton("This is a button!"));
        gui.addTab("This is a tab!", panel); // Adds a tab with the name 
                                   // "This is a tab!" and the panel to display "panel"
    }

}

See this link for more details about the JTabbedPane

Zach
  • 4,652
  • 18
  • 22
  • This code will not compile. Even if it did it wouldn't display anything. For a real example see the link posted above by @Hovercraft Full Of Eels – DavidPostill Jul 20 '14 at 19:57
  • Why doesn't it compile? – Zach Jul 20 '14 at 19:58
  • You posted code that you didn't try to compile? Firstly `panel.add(new JButton("This is a button!");` is missing a `)`. Secondly you might think about a few `import`s ... – DavidPostill Jul 20 '14 at 20:03
  • It compiles for me if `constructorParams` is not there (obviously) as it was put there to indicate that parameters go here since I have not included the constructor, but I will comment it out if you insist. In addition, this is only meant to show the methods used to add tabs and is not meant as a standalone GUI application that displays tabs when you put it into a .java file and run it. Notice the `// Some GUI code...` and `// Some more GUI code...` comments – Zach Jul 20 '14 at 20:13
1

As others have suggested in comments, you probably want to use a CardLayout. From the listeners of menu items, you can just call cardLayout.show(...) to should the panel you want. See more at How to use CardLayout.

Also I noticed that you are using the Netbeans builder tool. You will probably get some help at How to use CardLayout with Netbeans GUI Buidler.

Also you question seems to hint you may want to use tabs. In that case you will want to use the tabbed pane. You can just drag the tabbed pane from the palette then start adding panels to the tabbed pane. Maybe this link will help also for dragging panel class. Note: with dragging more than one panel, you should drag the panel, so that the cursor is next to the actual tab of the first tab panel.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

I would second the advice above from @Hovercraft Full Of Eels to look at Sun's tutorials for Java.

After reading those, however, I'd recommend looking at the Quick Start Guide and Swing demo on http://www.miglayout.com because MigLayout makes hand-coding good-looking layouts much simpler to read, understand, and write than most/all of the layout managers built into Java.

Its Swing demo app (yellow button about halfway down that website, or http://www.migcalendar.com/miglayout/swingdemoapp.jnlp) shows both what it's capable of and example code for how to make use of each feature.

NWinocur
  • 74
  • 5