how to interrupt a tab selection?

J

Joe

I have several tabbed panes in a JPanel and I would like to be able to
catch a tab click before the selected tab is actually displayed. A
small dialog will popup, the user makes a selection, then the dialog
goes away and the selected tab will be displayed.

Anyone know how to do something like this? I have a ChangeListener on
my JTabbedPane but, what I think I need is something like a
willDisplayTab() method of sorts.
 
A

Alexander.V.Kasatkin

what I think I need is something like a willDisplayTab() method of sorts.

To switch pages, the JTabbedPane use the SingleSelectionModel instance
(model field).
You can enhance the DefaultSingleSelectionModel class (I mean the
setSelectedIndex() method)
and set set it to tabbed pane using the JTabbedPane.setModel() method.
 
J

Joe

To switch pages, the JTabbedPane use the SingleSelectionModel instance
(model field).
You can enhance the DefaultSingleSelectionModel class (I mean the
setSelectedIndex() method)
and set set it to tabbed pane using the JTabbedPane.setModel() method.

Thanks, but I don't want to change the model. I just want to catch the
changeevent, show a dialog, then let the changeevent continue to
actually displaying the tab the user clicked on.
 
A

Alexander.V.Kasatkin

Thanks, but I don't want to change the model. I just want to catch the
changeevent, show a dialog, then let the changeevent continue to
actually displaying the tab the user clicked on.

the event originally is sent by model, handled by tabbed pane UI and
after that transferred to you.
So you can include your additional code into the model before it fires
a change event:

// DefaultSingleSelectionModel.java
public void setSelectedIndex(int index) {
// show a dialog here
// or create and fire your own additional event
// or do something else you want
// or return without switching pages
if (this.index != index) {
this.index = index;
fireStateChanged();
}
}
 
J

Joe

the event originally is sent by model, handled by tabbed pane UI and
after that transferred to you.
So you can include your additional code into the model before it fires
a change event:


This is working for me, but the only problem I'm having is that I can't
figure out how to get the source of the event before the
setSelectedIndex() method is called. I see it afterwards.

I've implemented ChangeListener on my class extended from
DefaultSingleSelectionModel, but the stateChanged() doesn't get called.

What am I doing wrong there?
 
A

Alexander.V.Kasatkin

This is working for me, but the only problem I'm having is that I can't
figure out how to get the source of the event before the
setSelectedIndex() method is called. I see it afterwards.

I've implemented ChangeListener on my class extended from
DefaultSingleSelectionModel, but the stateChanged() doesn't get called.

What am I doing wrong there?

It too hard for me to understand it without code you wrote,
so I've updoaded my simple example on the Google code:

http://code.google.com/p/javacodesnippets/downloads/list

Try to download these files.

BR, Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top