changelistener question

hey all

i am having problems by adding a change listener to jtabbed pane...

i cannot figure out where to add the changelistener, should i add it to the class that extends JFrame or to the classes that extends JPanel, i tried both and in both cases i cant find the method addChangeListener();

so when i make for example the class that extends JPanel , implements ChangeListener, i fires an error when i write addChangeListener(this);

the thing that i want to do is that when i change tabs, i want it to call the method resetFields();

the following is part of my source code...

please help...

thanks in advance

import javax.swing.*;

import javax.swing.event.ChangeListener;

import javax.swing.event.ChangeEvent;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

publicclass GUIextends JFrameimplements ActionListener{

public GUI(){

setDefaultCloseOperation(EXIT_ON_CLOSE);

setTitle("DVD rental center");

setSize(1000, 700);

setLocation(20, 20);

initializeMenuBar();

initializeGUI();

}

publicvoid initializeGUI(){

JTabbedPane tp =new JTabbedPane(JTabbedPane.LEFT);

getContentPane().add(tp);

tp.addTab("New Customer",new NewCustomerGUI());

tp.addTab("New Title",new NewTitleGUI());

}

publicvoid initializeMenuBar(){

JMenuBar menuBar =new JMenuBar();

JMenu fileMenu =new JMenu("File");

JMenuItem exitItem =new JMenuItem("Exit");

exitItem.setActionCommand("exit");

exitItem.addActionListener(this);

fileMenu.add(exitItem);

menuBar.add(fileMenu);

this.setJMenuBar(menuBar);

}

publicstaticvoid main(String[] args){

GUI g =new GUI();

g.setVisible(true);

}

/**

* Invoked when an action occurs.

*/

publicvoid actionPerformed(ActionEvent e){

if (e.getActionCommand().equalsIgnoreCase("exit"))

System.exit(0);

}

}

class NewCustomerGUIextends JPanelimplements ActionListener, GUIConstants{

private JLabel name =new JLabel("Customer Name: ", SwingConstants.LEFT);

private JLabel phone =new JLabel("Phone: ", SwingConstants.LEFT);

private JLabel address =new JLabel("Address: ", SwingConstants.LEFT);

private JLabel note =new JLabel("Note: ", SwingConstants.LEFT);

private JLabel deposit =new JLabel("Deposit: ", SwingConstants.LEFT);

private JTextField nameField =new JTextField(10);

private JTextField phoneField =new JTextField(10);

private JTextArea addressArea =new JTextArea(5, 10);

private JTextArea noteArea =new JTextArea(5, 10);

private JTextField depositField =new JTextField(10);

private JButton createButton =new JButton("Create Customer");

private JButton resetButton =new JButton("reset");

privatefinalint HORIZONTAL_SPACING = 500;

privatefinalint VERTICAL_SPACING = 290;

privatefinalint SMALL_VERICAL_SPACING = 15;

privatefinalint FONT_SIZE = 15;

private Font f =new Font("Times New Roman", Font.BOLD, FONT_SIZE);

private Font borderFont =new Font("Times New Roman", Font.BOLD, FONT_SIZE + 5);

public NewCustomerGUI(){

setupButtons();

setupFonts();

initializeGUI();

}

publicvoid setupButtons(){

createButton.setActionCommand("create");

createButton.addActionListener(this);

resetButton.setActionCommand("reset");

resetButton.addActionListener(this);

}

publicvoid setupFonts(){

name.setFont(f);

phone.setFont(f);

address.setFont(f);

note.setFont(f);

deposit.setFont(f);

nameField.setFont(f);

phoneField.setFont(f);

addressArea.setFont(f);

noteArea.setFont(f);

depositField.setFont(f);

}

publicvoid resetFields(){

nameField.setText("");

phoneField.setText("");

addressArea.setText("");

noteArea.setText("");

depositField.setText("");

}

public Box addComponent(Component c1, Component c2,int spacing){

Box b =new Box(BoxLayout.LINE_AXIS);

b.add(c1);

b.add(c2);

b.add(Box.createHorizontalStrut(spacing));

return b;

}

publicvoid setupComponents(){

Border b = BorderFactory.createLoweredBevelBorder();

this.setBorder(BorderFactory.createTitledBorder(b

,"New Customer"

, TitledBorder.DEFAULT_JUSTIFICATION

, TitledBorder.DEFAULT_POSITION

, borderFont

, Color.blue));

nameField.setBorder(BorderFactory.createLoweredBevelBorder());

phoneField.setBorder(BorderFactory.createLoweredBevelBorder());

addressArea.setBorder(BorderFactory.createLoweredBevelBorder());

noteArea.setBorder(BorderFactory.createLoweredBevelBorder());

depositField.setBorder(BorderFactory.createLoweredBevelBorder());

}

publicvoid setNameField(String name){

nameField.setText(name);

}

publicvoid setPhone(String phone){

phoneField.setText(phone);

}

publicvoid setAddress(String address){

addressArea.setText(address);

}

publicvoid setNote(String note){

noteArea.setText(note);

}

publicvoid setDeposit(String deposit){

depositField.setText(deposit);

}

publicvoid initializeGUI(){

setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

setupComponents();

add(addComponent(name, nameField, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(phone, phoneField, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

JScrollPane sp1 =new JScrollPane(addressArea,

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,

JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

add(addComponent(address, sp1, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

JScrollPane sp2 =new JScrollPane(noteArea,

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,

JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

add(addComponent(note, sp2, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(deposit, depositField, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(createButton, resetButton, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(VERTICAL_SPACING));

}

/**

* Invoked when an action occurs.

*/

publicvoid actionPerformed(ActionEvent e){

if (e.getActionCommand().equalsIgnoreCase("create")){

}

if (e.getActionCommand().equalsIgnoreCase("reset")){

resetFields();

}

}

}

class NewTitleGUIextends JPanelimplements ActionListener, GUIConstants{

private JLabel title =new JLabel("Title: ", SwingConstants.LEFT);

private JLabel number =new JLabel("Number: ", SwingConstants.LEFT);

private JLabel category =new JLabel("Category: ", SwingConstants.LEFT);

private JLabel actor1 =new JLabel("Actor 1: ", SwingConstants.LEFT);

private JLabel actor2 =new JLabel("Actor 2: ", SwingConstants.LEFT);

private JLabel director =new JLabel("Director: ", SwingConstants.LEFT);

private JLabel newr =new JLabel("New Release: ", SwingConstants.LEFT);

private JLabel cost =new JLabel("Cost($): ", SwingConstants.LEFT);

private JLabel date =new JLabel("Date(DD-MM-YYYY): ", SwingConstants.LEFT);

private JLabel barcode =new JLabel("Barcode: ", SwingConstants.LEFT);

String[] categories ={"Select a category","Thriller"};

private JTextField titleField =new JTextField(20);

private JTextField numberField =new JTextField(10);

private JComboBox categoryBox =new JComboBox(categories);

private JTextField actor1Field =new JTextField(15);

private JTextField actor2Field =new JTextField(15);

private JTextField directorField =new JTextField(15);

private JCheckBox newrField =new JCheckBox("",false);

private JTextField costField =new JTextField(10);

private JTextField dateField =new JTextField(10);

private JTextField barcodeField =new JTextField(10);

private JButton addTitle =new JButton("Add Title");

private JButton reset =new JButton("reset");

privatefinalint HORIZONTAL_SPACING = 500;

privatefinalint VERTICAL_SPACING = 300;

privatefinalint SMALL_VERICAL_SPACING = 15;

privatefinalint FONT_SIZE = 15;

private Font f =new Font("Times New Roman", Font.BOLD, FONT_SIZE);

private Font borderFont =new Font("Times New Roman", Font.BOLD, FONT_SIZE + 5);

public NewTitleGUI(){

setupFonts();

setupButtons();

initializeGUI();

}

publicvoid initializeGUI(){

setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

setupComponents();

add(addComponent(title, titleField, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(number, numberField, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(category, categoryBox, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(actor1, actor1Field, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(actor2, actor2Field, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(director, directorField, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(newr, newrField, HORIZONTAL_SPACING + 300));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(cost, costField, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(date, dateField, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(barcode, barcodeField, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));

add(addComponent(addTitle, reset, HORIZONTAL_SPACING));

add(Box.createVerticalStrut(VERTICAL_SPACING));

}

publicvoid setupFonts(){

title.setFont(f);

number.setFont(f);

category.setFont(f);

actor1.setFont(f);

actor2.setFont(f);

director.setFont(f);

newr.setFont(f);

cost.setFont(f);

date.setFont(f);

barcode.setFont(f);

titleField.setFont(f);

numberField.setFont(f);

category.setFont(f);

actor1Field.setFont(f);

actor2Field.setFont(f);

directorField.setFont(f);

newrField.setFont(f);

costField.setFont(f);

dateField.setFont(f);

barcodeField.setFont(f);

}

publicvoid setupComponents(){

Border b = BorderFactory.createLoweredBevelBorder();

this.setBorder(BorderFactory.createTitledBorder(b

,"New Title"

, TitledBorder.DEFAULT_JUSTIFICATION

, TitledBorder.DEFAULT_POSITION

, borderFont

, Color.blue));

titleField.setBorder(BorderFactory.createLoweredBevelBorder());

numberField.setBorder(BorderFactory.createLoweredBevelBorder());

categoryBox.setBorder(BorderFactory.createLoweredBevelBorder());

actor1Field.setBorder(BorderFactory.createLoweredBevelBorder());

actor2Field.setBorder(BorderFactory.createLoweredBevelBorder());

directorField.setBorder(BorderFactory.createLoweredBevelBorder());

newrField.setBorder(BorderFactory.createLoweredBevelBorder());

costField.setBorder(BorderFactory.createLoweredBevelBorder());

dateField.setBorder(BorderFactory.createLoweredBevelBorder());

barcodeField.setBorder(BorderFactory.createLoweredBevelBorder());

}

publicvoid setupButtons(){

addTitle.setActionCommand("add");

addTitle.addActionListener(this);

reset.setActionCommand("reset");

reset.addActionListener(this);

}

publicvoid resetFields(){

titleField.setText("");

numberField.setText("");

categoryBox.setSelectedIndex(0);

actor1Field.setText("");

actor2Field.setText("");

directorField.setText("");

newrField.setSelected(false);

costField.setText("");

dateField.setText("");

barcodeField.setText("");

}

public Box addComponent(Component c1, Component c2,int spacing){

Box b =new Box(BoxLayout.LINE_AXIS);

b.add(c1);

b.add(c2);

b.add(Box.createHorizontalStrut(spacing));

return b;

}

publicvoid setTitle(String title){

titleField.setText(title);

}

publicvoid setNumber(String number){

numberField.setText(number);

}

publicvoid setCategory(String name){

categoryBox.setSelectedItem(name);

}

publicvoid setActor1(String name){

actor1Field.setText(name);

}

publicvoid setActor2(String name){

actor2Field.setText(name);

}

publicvoid setDirector(String name){

directorField.setText(name);

}

publicvoid setNewRelease(boolean value){

newrField.setSelected(value);

}

publicvoid setCost(String cost){

costField.setText(cost);

}

publicvoid setDate(String date){

dateField.setText(date);

}

publicvoid setBarcode(String barcode){

barcodeField.setText(barcode);

}

/**

* Invoked when an action occurs.

*/

publicvoid actionPerformed(ActionEvent e){

if (e.getActionCommand().equalsIgnoreCase("add")){

}

if (e.getActionCommand().equalsIgnoreCase("reset")){

resetFields();

}

}

}

publicinterface GUIConstants{

publicvoid setupFonts();

publicvoid setupComponents();

publicvoid initializeGUI();

publicvoid setupButtons();

publicvoid resetFields();

public Box addComponent(Component c1, Component c2,int spacing);

}

[25902 byte] By [bif_fa] at [2007-11-27 3:53:19]
# 1
Generarlly you would add the ChangeListener to the tabbed pane in the class where you create the tabbed pane.
camickra at 2007-7-12 8:57:23 > top of Java-index,Desktop,Core GUI APIs...
# 2

thank u a lot camick...

u did help a lot in the 3 postings...thanks a lot... i frankly wait for you when no one answers my posts...

thanks a lot again...u were very helpful...

i couldnt solve the change listener thing...its not firing...could you help me based on my code...

thanks in advance

bif_fa at 2007-7-12 8:57:23 > top of Java-index,Desktop,Core GUI APIs...
# 3

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 8:57:23 > top of Java-index,Desktop,Core GUI APIs...