how to response to user's mouse click on JTabbedPane's tabTitle?

J

James

example like when there are 10 tabs inside the JTabbedPane, when user click
on tab 1, I want it to display the particular tab's title into JTextField.
I try add mouse listener into JTabbedPane but no response when user click on
that tab. (nothing display in JTextfield).
The JTextField only display out the title when user click on other place of
tab except tab title.

Part of the code:

JTabbedPane tabpnPage = new javax.swing.JTabbedPane();
JTextField txtTabTitle=new JTextField();

//assume this tabpane had add into JFrame and there are 10 JPanel added into
JTabbedPane which contain title "1","2",.......and there is also txtTabTitle
added into JFrame.

tabpnPage.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tabpnPageMouseClicked(evt);
}
});

private void tabpnPageMouseClicked(java.awt.event.MouseEvent evt) {

txtTabTitle.setText(tabpnPage.getTitleAt(tabpnPage.getSelectedIndex()));
}
 
A

Andrew Thompson

example like when there are 10 tabs inside the JTabbedPane, ...

A better group..
Part of the code:

Others are sometimes able to decipher code snippets, but they
are beyond me. Further...
//assume this ..

.... I am only prepared to make assumptions about people's code
when they are paying me *lots* of money. So instead I suggest
you prepare an SSCCE. <http://www.physci.org/codes/sscce.jsp>
Note that your example only requires two tabs to display the problem.
 
K

Kleopatra

James said:
example like when there are 10 tabs inside the JTabbedPane, when user click
on tab 1, I want it to display the particular tab's title into JTextField.

is that _really_ your requirement? What should happen if the tab is
selected by a keystroke? My guess is that you want the ui to be behave
similarly, independent of the type of low-level event (at least I hope
you want that :)
I try add mouse listener into JTabbedPane but no response when user click on
that tab. (nothing display in JTextfield).
The JTextField only display out the title when user click on other place of
tab except tab title.

that should (and usually does) work.

allow me two comments on your code:

a) the "type" prefixes (forgot - how are they called, it's been too long
ago <g>) are unusual in java.
b) import the classes you need instead using the full class name

Jeanette


PS: a better group to ask ui related questions is comp.lang.java.gui -
because it's focused on alas... gui issues.

PPS: you want to use a ChangeListener to get notified about selection
changes.
 
C

Chas Douglass

[ship]
allow me two comments on your code:

a) the "type" prefixes (forgot - how are they called, it's been too
long ago <g>) are unusual in java.
[snip]

"Hungarian" notation.

/me shudders

Chas Douglass
 
G

Grant Wagner

Chas said:
[ship]
allow me two comments on your code:

a) the "type" prefixes (forgot - how are they called, it's been too
long ago <g>) are unusual in java.
[snip]

"Hungarian" notation.

/me shudders

He used one type of "Hungarian" notation, one a lot of developers
disagree with:

<url: http://blogs.msdn.com/ericlippert/archive/2003/09/12/52989.aspx />
"What Simonyi is saying here is that the point of Hungarian Notation is
to extend the concept of "type" to encompass semantic information in
addition to storage representation information."

<url: http://blogs.msdn.com/larryosterman/archive/2004/06/22/162629.aspx
/>
"The bottom line is that when you’re criticizing Hungarian, you need to
understand which Hungarian you’re really complaining about. Hungarian as
defined by Simonyi isn’t nearly as bad as some have made it out to be."

<url: http://blogs.msdn.com/rick_schaut/archive/2004/02/14/73108.aspx />
"You see, there are really two distinct naming conventions known as
Hungarian Notation. I like to refer to them as Hungarian Notation and
Anti-Hungarian Notation."

<url: http://blogs.msdn.com/ericlippert/archive/2003/09/16/53015.aspx />
"If you're going to uglify your code with Hungarian Notation, please, at
least do it right. The whole point is to make the code easier to read
and reason about, and that means ensuring that the invariants expressed
by the Hungarian prefixes and suffixes are actually invariant...In other
words, the name of a variable should reflect its meaning throughout its
lifetime, not merely its initialization."

I don't use Hungarian notation, but if I am going to disagree with the
use of a particular syntax style, I figure I should understand what I am
disagreeing with.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top