Page 1 of 1

Java Tabbed Pane GUI Create a Swing GUI with tabs Rate Topic: ****- 1 Votes

#1 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Posted 19 February 2010 - 08:01 PM

*
POPULAR

I have created a tutorial that will show a programmer how to create a tabbed windowed application. It will show all the components that are necessary to put into the JFrame and then how to put tabs in the Pane and components within each tab.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.*;



These are all the imports that are needed. You could probably put a splat after swing, but I was adding them as I needed them.

public class TabbedPane extends JFrame {

   public TabbedPane()   //constructor for the tabbed frame you will be creating
   {
	//This will create the title you see in the upper left of the window	
       setTitle("Tabbed Pane");  
 
	//Here we are creating the object
       JTabbedPane jtp = new JTabbedPane();

 	//This creates the template on the windowed application that we will be using
       getContentPane().add(jtp);

       JPanel jp1 = new JPanel();//This will create the first tab

       JPanel jp2 = new JPanel();//This will create the second tab


Make note that I am using all Swing components, and that each component must be declared and given value like all data types(i.e int, char, double, String, Object)
	//This creates a non-editable label, sets what the label will read
	//and adds the label to the first tab
       JLabel label1 = new JLabel();
       label1.setText("This is Tab 1");/
       jp1.add(label1);

       //This adds the first and second tab to our tabbed pane object and names it
       jtp.addTab("Tab1", jp1);
       jtp.addTab("Tab2", jp2);

	//This creates a new button called "Press" and adds it to the second tab
       JButton test = new JButton("Press");
       jp2.add(test);


Those are all the components that will be added for this tutorial. Adding components in a GUI for Java is like creating A blank piece of paper, then you create several sheets that lay on top of each other, similar to the overhead that teachers used in school, where they would have a clear plastic sheet they would lay on top of another sheet, so you could see the extra information they added along with the original paper.
	//This is an Action Listener which reacts to clicking on 
	//the test button called "Press"
       ButtonHandler phandler = new ButtonHandler();
       test.addActionListener(phandler);
   }


This section of code creates a handler that we haven't defined in the code yet. This creates a handler and adds the handler to the button we created earlier, so now the button can do something when it's clicked. this is also called registering the handler.
	
	//This is the internal class that defines what the above Action Listener
	//will do when the test button is pressed.
       class ButtonHandler implements ActionListener
       {
               public void actionPerformed(ActionEvent e)
               {
                       JOptionPane.showMessageDialog(null, "I've been pressed", "What happened?", JOptionPane. INFORMATION_MESSAGE);
               }
       }



Here we have created a small internal class known as an action listener. All Action Listeners must note implements ActionListener so that Java will know that it is listening for an action.

    public static void main(String[] args)
    {
    TabbedPane tabbedPane = new TabbedPane();
    }
}


This will create an instance of the TabbedPane class




Full code for a quick and easy test:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.*;

public class TabbedPane extends JFrame {
    
    public TabbedPane() {
        
         //This will create the title you see in the upper left of the window    
        setTitle("Tabbed Pane");  
        setSize(300,300); //set size so the user can "see" it
        //Here we are creating the object
        JTabbedPane jtp = new JTabbedPane();

        //This creates the template on the windowed application that we will be using
       getContentPane().add(jtp);

       JPanel jp1 = new JPanel();//This will create the first tab

       JPanel jp2 = new JPanel();//This will create the second tab
        
         //This creates a non-editable label, sets what the label will read
        //and adds the label to the first tab
       JLabel label1 = new JLabel();
       label1.setText("This is Tab 1");
       jp1.add(label1);

       //This adds the first and second tab to our tabbed pane object and names it
       jtp.addTab("Tab1", jp1);
       jtp.addTab("Tab2", jp2);

        //This creates a new button called "Press" and adds it to the second tab
       JButton test = new JButton("Press");
       jp2.add(test);

        //This is an Action Listener which reacts to clicking on 
        //the test button called "Press"
        ButtonHandler phandler = new ButtonHandler();
        test.addActionListener(phandler);
        setVisible(true); //otherwise you won't "see" it 
    }
    
    //This is the internal class that defines what the above Action Listener
    //will do when the test button is pressed.
    class ButtonHandler implements ActionListener{
           public void actionPerformed(ActionEvent e){
                   JOptionPane.showMessageDialog(null, "I've been pressed", "What happened?", JOptionPane. INFORMATION_MESSAGE);
           }
    }

    //example usage
     public static void main (String []args){
        TabbedPane tab = new TabbedPane();
    }

}



Is This A Good Question/Topic? 5
  • +

Replies To: Java Tabbed Pane GUI

#2 toshiro  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 137
  • Joined: 27-June 09

Posted 22 February 2010 - 03:25 PM

Much easier then doing panel swaps with class Objects. Good stuff. I never knew a JTabbedPane existed. This will make my GUI career just that much easier.
Was This Post Helpful? 0
  • +
  • -

#3 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Posted 24 February 2010 - 12:36 PM

Cool, I'm glad you liked it!
Was This Post Helpful? 0
  • +
  • -

#4 Theomi  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 88
  • Joined: 17-January 10

Posted 24 June 2010 - 04:00 PM

Nice little tut, in school we only learned to use the netbeans gui creator..
Altho it's nifty it's funnier to do it "your self"...
Was This Post Helpful? 0
  • +
  • -

#5 harmy01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 179
  • Joined: 15-June 10

Posted 25 June 2010 - 10:59 PM

Thanks man, very helpful
Was This Post Helpful? 0
  • +
  • -

#6 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 145
  • Joined: 15-December 08

Posted 26 June 2010 - 06:31 AM

a good tutorial, helps clear up a bit on Tabbed Pane.
Was This Post Helpful? 0
  • +
  • -

#7 BlakeRey  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 23
  • Joined: 08-February 10

Posted 27 October 2010 - 10:39 PM

Thanks alot for this, I have been reading about Java Tabbed Panes and yours is the best example I have found!

Thanks,
Blake.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_ml*


Reputation:

Posted 07 December 2010 - 10:37 PM

Thank you. This was easy to follow...
ml
Was This Post Helpful? 0

#9 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Posted 20 April 2011 - 06:31 PM

Hi

I have a little question. Might even seem sill too. Can I just ask why would you import the J Extensions seperately if you're going to import the entire swing lib?

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.*;




Is there a reason behind it? Thanks.
Was This Post Helpful? 0
  • +
  • -

#10 socks1825  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-August 12

Posted 01 August 2012 - 06:43 PM

View Postsas1ni69, on 20 April 2011 - 06:31 PM, said:

Hi

I have a little question. Might even seem sill too. Can I just ask why would you import the J Extensions seperately if you're going to import the entire swing lib?

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.*;




Is there a reason behind it? Thanks.



I have a question about the tabbed panes for GUI. I looked at this tutorial and it makes sense how to set up the tabs. What I am confused about it where do I put my program for each tab?? I have the code working but I don't know where to place it to get it in a tab. If anyone could just help direct me on where to insert my code that would be much appreciated.
Was This Post Helpful? 0
  • +
  • -

#11 hanzechin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 12

Posted 11 October 2012 - 11:22 AM

View Postsocks1825, on 01 August 2012 - 06:43 PM, said:

View Postsas1ni69, on 20 April 2011 - 06:31 PM, said:

Hi

I have a little question. Might even seem sill too. Can I just ask why would you import the J Extensions seperately if you're going to import the entire swing lib?

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.*;




Is there a reason behind it? Thanks.



I have a question about the tabbed panes for GUI. I looked at this tutorial and it makes sense how to set up the tabs. What I am confused about it where do I put my program for each tab?? I have the code working but I don't know where to place it to get it in a tab. If anyone could just help direct me on where to insert my code that would be much appreciated.

I'm sorry if i'm bring up an old thread but...
I've the same problem as this...Please if someone would be willing to explain i'm all eyes :P
Was This Post Helpful? 0
  • +
  • -

#12 hanzechin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 12

Posted 15 October 2012 - 10:49 AM

Bump, can anyone help?
Was This Post Helpful? 0
  • +
  • -

#13 Brainpower49  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 04-February 13

Posted 07 March 2013 - 07:20 PM

well done!
Thanks much for this tutorial!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1