JTabbedPane event for tab change!

How to add tab change event for JTabbedPane in the code.

How can i add event for buttons for both panels .... as well as text field..please help me..i m new to swing!!

Here is the code I have now:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

publicclass MainFrameextends JFrame

{

// Variables declaration

private JTabbedPane jTabbedPane1;

private JPanel contentPane;

//--

private JPanel jPanel1;

//--

private JTextField jTextField2;

private JButton jButton1;

private JPanel jPanel2;

//--

private JTextField jTextField1;

private JButton jButton2;

private JPanel jPanel3;

//--

// End of variables declaration

public MainFrame()

{

super();

initializeComponent();

this.setVisible(true);

}

privatevoid initializeComponent()

{

jTabbedPane1 =new JTabbedPane();

contentPane = (JPanel)this.getContentPane();

//--

jPanel1 =new JPanel();

//--

jTextField2 =new JTextField();

jButton1 =new JButton();

jPanel2 =new JPanel();

//--

jTextField1 =new JTextField();

jButton2 =new JButton();

jPanel3 =new JPanel();

//--

//

// jTabbedPane1

//

jTabbedPane1.addTab("panel1", jPanel1);

jTabbedPane1.addTab("panel2", jPanel2);

jTabbedPane1.addTab("panel3", jPanel3);

//

// contentPane

//

contentPane.setLayout(null);

addComponent(contentPane, jTabbedPane1, -3,0,383,272);

//

// jPanel1

//

jPanel1.setLayout(null);

//

// jTextField2

//

jTextField2.setText("jTextField2");

});

//

// jButton1

//

jButton1.setText("jButton1");

//

// jPanel2

//

jPanel2.setLayout(null);

addComponent(jPanel2, jTextField2, 143,55,61,22);

addComponent(jPanel2, jButton1, 123,121,113,29);

//

// jTextField1

//

jTextField1.setText("jTextField1");

//

// jButton2

//

jButton2.setText("jButton2");

//

// jPanel3

//

jPanel3.setLayout(null);

addComponent(jPanel3, jTextField1, 140,58,57,21);

addComponent(jPanel3, jButton2, 111,121,114,25);

//

// MainFrame

//

this.setTitle("MainFrame");

this.setLocation(new Point(0, 0));

this.setSize(new Dimension(390, 300));

}

privatevoid addComponent(Container container,Component c,int x,int y,int width,int height)

{

c.setBounds(x,y,width,height);

container.add(c);

}

publicstaticvoid main(String[] args)

{

JFrame.setDefaultLookAndFeelDecorated(true);

JDialog.setDefaultLookAndFeelDecorated(true);

try

{ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

}

catch (Exception ex)

{

System.out.println("Failed loading");

System.out.println(ex);

}

new MainFrame();

}

}

[6860 byte] By [richa_sharmaa] at [2007-11-26 22:25:44]
# 1

have you read this tutorial

http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html

to see how to use JTabbedPane ?

The TabbedPane uses a SingleSelectionModel to represent the set of tab indices and the currently selected index. If the tab count is greater than 0, then there will always be a selected index, which by default will be initialized to the first tab. If the tab count is 0, then the selected index will be -1.

from the javadoc so check [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/SingleSelectionModel.html]this[/url] too !!!

Re-edited by sup@reno because he forgot something !!!

suparenoa at 2007-7-10 11:26:48 > top of Java-index,Desktop,Core GUI APIs...
# 2
Neither of those help the OP though, who - if I understand the question correctly - wants to detect tab selection events. In which case the ChangeListener interface should be used.
itchyscratchya at 2007-7-10 11:26:48 > top of Java-index,Desktop,Core GUI APIs...
# 3

> have you read this tutorial

> http://java.sun.com/docs/books/tutorial/uiswing/compon

> ents/tabbedpane.html

> to see how to use JTabbedPane ?

>

> The TabbedPane uses a SingleSelectionModel to

> represent the set of tab indices and the currently

> selected index. If the tab count is greater than 0,

> then there will always be a selected index, which by

> default will be initialized to the first tab. If the

> tab count is 0, then the selected index will be -1.

>

> from the javadoc so check

> [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/swi

> ng/SingleSelectionModel.html]this[/url] too !!!

>

> Re-edited by sup@reno because he forgot something !!!

I already read the tutorial u suggested..i used dis method to add tab

public void addTab(String title,Component component)

now how can i add event of changing tab...

m i going in right direction

hope i m making sense..kindly help!!

richa_sharmaa at 2007-7-10 11:26:48 > top of Java-index,Desktop,Core GUI APIs...
# 4
Is it necessary to add change event in every time we use tabbed panel?
richa_sharmaa at 2007-7-10 11:26:48 > top of Java-index,Desktop,Core GUI APIs...
# 5
No, not at all.I'm lost. What are you trying to do?
itchyscratchya at 2007-7-10 11:26:48 > top of Java-index,Desktop,Core GUI APIs...
# 6
he may try to change tabbed panes (one, two or more) when he presses a button?i'm lost too
suparenoa at 2007-7-10 11:26:48 > top of Java-index,Desktop,Core GUI APIs...
# 7
Oh. Well to do that it's either setSelectedComponent() or setSelectedIndex().
itchyscratchya at 2007-7-10 11:26:48 > top of Java-index,Desktop,Core GUI APIs...
# 8

i made this

jTabbedPaneMain.addChangeListener(new javax.swing.event.ChangeListener() {

public void stateChanged(javax.swing.event.ChangeEvent e) {

System.out.println("stateChanged() "+ jTabbedPaneMain.getSelectedIndex());

switch ( jTabbedPaneMain.getSelectedIndex() ) {

case 0:

//loadViewExcursionsTab();

break;

case 1:

//loadReservationManagement();

break;

case 2:

break;

default:

break;

}//switch

}

});

souldn't focus on the tab itself but on the tabbed pane at all

alexscorpiona at 2007-7-10 11:26:48 > top of Java-index,Desktop,Core GUI APIs...