Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simple enough but... calling a method from another class

805576Oct 14 2010 — edited Oct 14 2010
Hi all,
I know it's simple enough, although I can't see where I'm going wrong for some reason. Basically I've got a class which extends JPanel but when I try to call one of its methods from my main class I get an error. The relevant code (I hope) is:

Main.java
import java.awt.BorderLayout;
// ...
import javax.swing.UIManager;

public class Main extends JFrame implements ActionListener {

	JFrame frame;
	public static JPanel statusBar;

	public Main() {

		// ...

		// Add a status bar
		statusBar = new StatusBar();
		mainPanel.add( statusBar, BorderLayout.SOUTH );

		setContentPane( mainPanel );
		// ...
	}

	// ...

	public static void setStatusBarText( String s ) {
		statusBar.setStatusText("Test");
	}

	// ...
}
StatusBar.java
import java.awt.Color;
// ...
import javax.swing.SwingConstants;

public class StatusBar extends JPanel {

	private int barWidth;
	private static JLabel status;

	public StatusBar() {
		super();
		//barWidth = Main.getProgramWidth();
		//setPreferredSize( new Dimension(barWidth,22) );
		setLayout( new FlowLayout( FlowLayout.LEADING ) );
		setBorder( BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(160,160,160) ) );

		status = new JLabel("Test", SwingConstants.LEFT);
		//status.setVerticalAlignment( SwingConstants.CENTER );
		add( status );
	}

	protected void setStatusText( String s ) {
		status.setText( s );
		revalidate();
	}
}
The "statusBar.setStatusText("Test");" call in the main class file doesn't work and I get the error:

>
cannot find symbol
symbol : method setStatusText(java.lang.String)
location: class javax.swing.JPanel
statusBar.setStatusText("Test");
>

Any ideas what I'm doing wrong? I created an instance of the StatusBar class called statusBar, then I use this to call the setStatusText() method (via statusBar.setStatusText()) which I thought was all correct, then it doesn't work? Even if there's an easier way to do what I want to achieve (i.e. update a JLabel from the 'central' main class file instead of setting the JLabel to static and having every class call it directly), I'd appreciate knowing where I'm going wrong here nonetheless.

Many thanks,
Tristan
This post has been answered by jduprez on Oct 14 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 11 2010
Added on Oct 14 2010
3 comments
422 views