Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing the displayed tabbed pane using a JButton

0 views
Skip to first unread message

But I Haven't Eaten Any Sweetcorn!

unread,
Feb 23, 2004, 8:25:18 AM2/23/04
to
Hi all,
I'm trying to include a button on a tabbed page that will enable you to
switch to
another page.

I'm using the following code:

FlyProGen.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{

tabbedpane.setSelectedComponent( container ); // container is the page I
want to
// display rather than the top level page.


}
} );

However, I get the following error: "Cannot refer to a non-final variable
tabbed pane inside
an inner class defined in a different method".

Help!

best wishes

Paul
--
http://www.paullee.com

xarax

unread,
Feb 23, 2004, 10:04:34 AM2/23/04
to
"But I Haven't Eaten Any Sweetcorn!" <paul....@baesystems.com> wrote in
message news:4039fe49$1...@baen1673807.greenlnk.net...
One or more of the variables referenced in the
anonymous inner class is not declared final. It
could be "tabbedPane" or "container". Make them
final variables.


--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!


But I Haven't Eaten Any Sweetcorn!

unread,
Feb 23, 2004, 10:29:22 AM2/23/04
to
I've tried that, and the compiler doesn't like them.

By the way, if I declare something as being "final" in the variables
declaration
section, does this not mean I can't change them later on? I'm actually
setting
up the tabbed panes etc. in a SetUpGui() function called within the
constructor.
Within the function is the part where I set up the addActionListener for
the button.


Cheers
Paul
--
http://www.paullee.com


"xarax" <xa...@email.com> wrote in message
news:6Eo_b.4945$yZ1...@newsread2.news.pas.earthlink.net...

Andrew Harker

unread,
Feb 23, 2004, 3:36:48 PM2/23/04
to
... snipped & moved from top post to bottom - easier to
read in newsgroups

But I Haven't Eaten Any Sweetcorn! wrote:
> "xarax" <xa...@email.com> wrote:


> > "But I Haven't Eaten Any Sweetcorn!" wrote:
> > > Hi all,
> > > I'm trying to include a button on a tabbed page that will
> > > enable you to switch to another page. I'm using the following
> > > code:
> > > FlyProGen.addActionListener( new ActionListener() {
> > > public void actionPerformed( ActionEvent e ) {

> > > tabbedpane.setSelectedComponent( container );}});


> > >
> > > However, I get the following error: "Cannot refer to a
> > > non-final variable tabbed pane inside
> > > an inner class defined in a different method".
> > >
> > One or more of the variables referenced in the
> > anonymous inner class is not declared final. It
> > could be "tabbedPane" or "container". Make them
> > final variables.
> >

> I've tried that, and the compiler doesn't like them.
>
> By the way, if I declare something as being "final" in the
> variables declaration section, does this not mean I can't
> change them later on? I'm actually setting up the tabbed

In simple terms it means you cannot change what they point/refer
to but you can alter the object
<http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#10931>

> panes etc. in a SetUpGui() function called within
> the constructor.
> Within the function is the part where I set up the
> addActionListener for the button.
>

As xarax/Jeffrey D. Smith says: If the "tabbedpane" is
declared in the "SetUpGui()" function (ie a local
variable) then it should also be declared "final", eg
I'm guessing you have something like:

private void setUpGui() {
final JTabbedPane tabbedPanel = new JTabbedPane();
final JPanel firstPanel = new JPanel();
final JPanel secondPanel = new JPanel();
JButton firstButton = new JButton("Goto 2nd tab");
JButton secondButton = new JButton("Goto 1st tab");

firstButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
tabbedPanel.setSelectedComponent(secondPanel);
}
});
...

Why don't you post a Short, Self Contained, Correct
(Compilable), Example?
<http://www.physci.org/codes/sscce.jsp>

cheers
--
Andrew

0 new messages