Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Enable and Disable Tab in Java 
 

In this section you will learn how to enable and disable any tab in a TabbedPane component of Java Swing.

 

Enable and Disable Tab in Java

                         

In this section you will learn how to enable and disable any tab in a TabbedPane component of Java Swing. The tabbed pane container can have one or more tables. All the tabs have a specific area in same place. Whenever you will click on any tab, the components added to the tab will be displayed. Sometimes it is necessary to disable some of the tabs. Following figure shows a frame in which some tabs are disabled:

Swing JTabbedPane component with some enabled and disabled tabs

This program shows the multiple tabs on the frame. These  tabs have a suitable name and index number. To enable or disable any tab you can use the setEnabledAt(int index, Boolean value) function. Tabs are by default enabled. Following are some methods and APIs that you can use in your program to enable or disable the tabs.

addTab():
This is the method of JTabbedPane class, adds the tabs to the panel or other other container component. The parameter description of the function are as follows:

  • First is the title of the tab of string data type
  • Second is the icon for the tab
  • Third is the component which is displayed on the corresponding tab
  • And last is the tool tip text for the tab

setEnabledAt(int index, Boolean value):
This is the method of JTabbedPane class, enables or disables the tab. This method takes two argument in which, first is the index number of the tab which has to be enabled or disabled and another argument is the boolean value. If the boolean value is TRUE then tab get enabled other wise tab gets disabled.

Here is the code of program:

import javax.swing.*;

public class EnableAndDisableTab{
  public static void main(String[] args) {
    EnableAndDisableTab t = new EnableAndDisableTab();
  }

  public EnableAndDisableTab(){
    JFrame frame = new JFrame("Enabling and Disabling a Tab in a JTabbedPane Container");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JPanel panel4 = new JPanel();
    JTabbedPane tpane = new JTabbedPane();
    tpane.addTab("Index", null, panel, "This is enable.");
    tpane.addTab("Table of contents", null, panel1, "This is disable.");
    tpane.addTab("Types of colors", null, panel2, "This is enable.");
    tpane.addTab("Types of computers", null, panel3, "This is disable.");
    tpane.addTab("Category", null, panel4, "This is enable.");
    tpane.setEnabledAt(1false);
    tpane.setEnabledAt(3,false);
    frame.add(tpane);
    frame.setSize(400,400);
    frame.setVisible(true);
  }
}

Download this example

                         

» View all related tutorials
Related Tags: c com diff vi value return opera id show if example unc values exam mode ws xor sh bot e

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

tabs are numbered just like arrays (they start at 0 NOT 1). so im guessing that you only have one tab because you technically tried to disable the 2nd tab by saying (1,false) and the error says that you are out of bounds. therefore solution should be:

tpane.setEnabledAt(0,false);

Posted by sean j on Thursday, 07.12.07 @ 03:05am | #21114

Hi,

I'm interested in disable some of tabs of my TabbedPane. I have built my interface with netbeans 5.5 and I've tried to use tpane.setEnabledAt(1,false) in Post-Creation Code, but I received an error like:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0"

What can I do?



Posted by Gorka Esnal on Wednesday, 05.2.07 @ 19:53pm | #15177

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.