I have created a JFrame contaning a JTabbedPane. What I am trying to accomplish is the changing of tabs within my 'addCustomer()' class via an ActionListener. What I was trying was to call the method 'setTab(int c)' located in my 'BoatLog()' class, from my 'addCustomer()' class to set the index value of the private variable 'tabbedPane'. I want to be able to press the 'addBoat' button to switch to the tab of index value of 1.


The following are my two classes.

package boatlog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 


public class BoatLog extends JFrame
{ //Opens BoatLog class

	private JTabbedPane tabbedPane;  

	public BoatLog()
	{      
		setTitle("Penn State Marina");

		// Closes from title bar and from menu
		addWindowListener(new WindowAdapter() 
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});

		// Tabbed pane with panels for Jcomponents
		tabbedPane = new JTabbedPane(SwingConstants.LEFT);
		tabbedPane.setBackground(Color.blue);
		tabbedPane.setForeground(Color.white);

		// Calls a method that adds individual tabs to the
		//tabbedpane object.
		populateTabbedPane();

		//Calls the method that builds the menu 
		buildMenu();

		getContentPane().add(tabbedPane);
	}

	//Creates tabs with titles
	private void populateTabbedPane()
	{
		tabbedPane.addTab("1  Add Customer",
				null,
				new addCustomer(),
				"Click here to add a customer");

		tabbedPane.addTab("2  Add Boat",
				null,
				new addBoat(),
				"Click here to add a boat");

		tabbedPane.addTab("Confirmation",
				null,
				new confirmation(),
				"See confirmation");
	}

	//Builds menu bar
	private void buildMenu()
	{
		JMenuBar mb = new JMenuBar();
		JMenu menu = new JMenu("File");
		JMenuItem item = new JMenuItem("Exit");

		//Closes the application from the Exit menu item.
		item.addActionListener(new ActionListener()
		{ 
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});

		menu.add(item);
		mb.add(menu);
		setJMenuBar(mb);
	}

	public void setTab(int c)
	{
		tabbedPane.setSelectedIndex(c);
	}

	// main method and entry point for app    
	public static void main(String[] args)
	{
		BoatLog bl = new BoatLog();
		bl.setSize(765,690);
		bl.setBackground(Color.white);
		bl.setVisible(true);
	}
} //Ends class DiveLog
package boatlog;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;

import java.awt.*;


public class addCustomer extends JPanel
						 implements ActionListener
{
	private JPanel labelPanel = new JPanel(),
				   textFieldPanel = new JPanel(),
				   buttonPanel = new JPanel();
	
	private JLabel nameLabel = new JLabel("Name");
	private JTextField name = new JTextField(20);
	private JLabel addressLabel = new JLabel("Address");
	private JTextField address = new JTextField(20);
	private JLabel cityLabel = new JLabel("City");
	private JTextField city = new JTextField(20);
	private JLabel phoneLabel = new JLabel("Phone");
	private JTextField phone = new JTextField(20);
	
	private JButton addBoat = new JButton("Add Boat");
	private JButton clear = new JButton("Clear");
	private JButton close = new JButton("Close");
	
	public addCustomer()
	{
		setLayout(new FlowLayout());
		setBackground(Color.white);
		
		add(labelPanel);
		
		labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.Y_AXIS));
		
		labelPanel.add(nameLabel);
		labelPanel.add(addressLabel);
		labelPanel.add(cityLabel);
		labelPanel.add(phoneLabel);
		
		add(textFieldPanel);
		
		textFieldPanel.setLayout(new BoxLayout(textFieldPanel, BoxLayout.Y_AXIS));
		
		textFieldPanel.add(name);
		textFieldPanel.add(address);
		textFieldPanel.add(city);
		textFieldPanel.add(phone);
		
		add(buttonPanel);
		
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		
		buttonPanel.add(addBoat);
		addBoat.addActionListener(this);
		buttonPanel.add(clear);
		buttonPanel.add(close);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == addBoat)
		{
			setTab(1);
		}
		
	}
}

Calling a method from another class you need to append the name of the object to which the method belongs - or the class, if it's a static method.

Since your addCustomer doesn't have a BoatLog that it knows about, you'll either need to give it a reference to the BoatLog object that's doing the work, or make the setTab method static. Or (and this might make more sense) you could make the BoatLog the ActionListener. Your addCustomer would still need a reference to a BoatLog in that case, but it might make more sense.

Calling a method from another class you need to append the name of the object to which the method belongs - or the class, if it's a static method.

Since your addCustomer doesn't have a BoatLog that it knows about, you'll either need to give it a reference to the BoatLog object that's doing the work, or make the setTab method static. Or (and this might make more sense) you could make the BoatLog the ActionListener. Your addCustomer would still need a reference to a BoatLog in that case, but it might make more sense.

Hi, thanks for replying. Sorry, I am still a beginner at java and I don't know if I really understand your suggestions. I tried implementing BoatLog as the ActionListener but now I need to be able to reference my addBoat button which is located in my addCustomer class. I tried making a getAddBoat method to access it, but again I was unable to reference that from BoatLog class.

Calling a method from another class you need to append the name of the object to which the method belongs - or the class, if it's a static method.

Since your addCustomer doesn't have a BoatLog that it knows about, you'll either need to give it a reference to the BoatLog object that's doing the work, or make the setTab method static. Or (and this might make more sense) you could make the BoatLog the ActionListener. Your addCustomer would still need a reference to a BoatLog in that case, but it might make more sense.

Thanks, I figured it out! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.