 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Casper Guest
|
Posted: Mon Sep 12, 2005 9:29 pm Post subject: Subclass JToolBar to prevent text being set from Action's? |
|
|
I am using Action (AbstractAction specialization) for adding handlers to
my JMenuBar and JToolBar widgets. However, it seems impossible to have
name (of the Actions) shown in the menu and not in the toolbar buttons.
Can I subclass JToolBar or something similar, to ensure that the text is
never set?
Thanks in advance,
Casper
|
|
| Back to top |
|
 |
Thomas Fritsch Guest
|
Posted: Mon Sep 12, 2005 11:09 pm Post subject: Re: Subclass JToolBar to prevent text being set from Action' |
|
|
"Casper" <casper (AT) jbr (DOT) dk> wrote:
| Quote: | I am using Action (AbstractAction specialization) for adding handlers to my
JMenuBar and JToolBar widgets. However, it seems impossible to have name
(of the Actions) shown in the menu and not in the toolbar buttons. Can I
subclass JToolBar or something similar, to ensure that the text is never
set?
How exactly do you add your Action to the JToolBar? |
I usually take the following pattern and don't run into the sort of problem
you describe.
// an inner class
class OpenFileAction extends AbstractAction {
OpenFileAction() {
putValue(NAME, "Open");
putValue(SMALL_ICON, new ImageIcon(...));
}
...
}
Action openAction = new OpenFileAction();
JMenu fileMenu = new JMenu("File");
fileMenu.add(openFileAction); // I get a menu item with icon and text
JToolBar toolBar = JToolBar();
toolBar.add(openFileAction); // I get a button with icon and *without* text
--
"TFritsch$t-online:de".replace(':','.').replace('$','@')
|
|
| Back to top |
|
 |
Casper Guest
|
Posted: Tue Sep 13, 2005 12:56 am Post subject: Re: Subclass JToolBar to prevent text being set from Action' |
|
|
Thomas Fritsch skrev:
| Quote: | "Casper" <casper (AT) jbr (DOT) dk> wrote:
I am using Action (AbstractAction specialization) for adding handlers to my
JMenuBar and JToolBar widgets. However, it seems impossible to have name
(of the Actions) shown in the menu and not in the toolbar buttons. Can I
subclass JToolBar or something similar, to ensure that the text is never
set?
How exactly do you add your Action to the JToolBar?
I usually take the following pattern and don't run into the sort of problem
you describe.
// an inner class
class OpenFileAction extends AbstractAction {
OpenFileAction() {
putValue(NAME, "Open");
putValue(SMALL_ICON, new ImageIcon(...));
}
...
}
Action openAction = new OpenFileAction();
JMenu fileMenu = new JMenu("File");
fileMenu.add(openFileAction); // I get a menu item with icon and text
JToolBar toolBar = JToolBar();
toolBar.add(openFileAction); // I get a button with icon and *without* text
|
I do the same thing, except I have to specify .setText(null) on the
action in the toolbar in order to not have text! According to Ivor
Horton's "Beginning Java 2", Chapter 18 - Handling Events:
"The setText() method call for the button will remove the label so the
toolbar buttons will just have icons."
....in my case however, using setText() is not a viable solution because
I have implemented support for several languages, and upon chosing a new
language, my Action objects are told to reload strings from external
ressources. It is during this reload, that text reapear in the toolbar
after I initially removed it with the .setText(null).
Casper
|
|
| Back to top |
|
 |
Thomas Fritsch Guest
|
Posted: Tue Sep 13, 2005 1:39 am Post subject: Re: Subclass JToolBar to prevent text being set from Action' |
|
|
"Casper" <casper (AT) jbr (DOT) dk> schrieb:
| Quote: | Thomas Fritsch skrev:
I usually take the following pattern and don't run into the sort of
problem you describe.
// an inner class
class OpenFileAction extends AbstractAction {
OpenFileAction() {
putValue(NAME, "Open");
putValue(SMALL_ICON, new ImageIcon(...));
}
...
}
Action openAction = new OpenFileAction();
JMenu fileMenu = new JMenu("File");
fileMenu.add(openFileAction); // I get a menu item with icon and text
JToolBar toolBar = JToolBar();
toolBar.add(openFileAction); // I get a button with icon and *without*
text
I do the same thing, except I have to specify .setText(null) on the action
in the toolbar in order to not have text!
You mean, you have to call .setText(null) on the button which was returned |
by toolBar.add(Action), don't you ? Action has no .setText(...) method.
| Quote: | According to Ivor
Horton's "Beginning Java 2", Chapter 18 - Handling Events:
"The setText() method call for the button will remove the label so the
toolbar buttons will just have icons."
...in my case however, using setText() is not a viable solution because I
have implemented support for several languages, and upon chosing a new
language, my Action objects are told to reload strings from external
ressources.
I've never done switching the GUI-language *while* the GUI is displayed. I |
only load the language-dependent strings once during GUI-construction, and
then stay with them. So I'm running out of experience here.
| Quote: | It is during this reload, that text reapear in the toolbar after I
initially removed it with the .setText(null).
May be calling .setText(...) on the button is different to calling |
..putValue(NAME, ...) on the Action.
But I'm totally guessing here, because I don't see your code. I think it
will be best, if you post a short compilable example, just big enough to
reproduce your problem, something like described in
<http://www.physci.org/codes/sscce.jsp>.
--
"TFritsch$t-online:de".replace(':','.').replace('$','@')
|
|
| Back to top |
|
 |
Casper Guest
|
Posted: Tue Sep 13, 2005 2:04 am Post subject: Re: Subclass JToolBar to prevent text being set from Action' |
|
|
Thomas Fritsch:
| Quote: | You mean, you have to call .setText(null) on the button which was returned
by toolBar.add(Action), don't you ? Action has no .setText(...) method.
According to Ivor
Horton's "Beginning Java 2", Chapter 18 - Handling Events:
"The setText() method call for the button will remove the label so the
toolbar buttons will just have icons."
...in my case however, using setText() is not a viable solution because I
have implemented support for several languages, and upon chosing a new
language, my Action objects are told to reload strings from external
ressources.
I've never done switching the GUI-language *while* the GUI is displayed. I
only load the language-dependent strings once during GUI-construction, and
then stay with them. So I'm running out of experience here.
It is during this reload, that text reapear in the toolbar after I
initially removed it with the .setText(null).
May be calling .setText(...) on the button is different to calling
.putValue(NAME, ...) on the Action.
But I'm totally guessing here, because I don't see your code. I think it
will be best, if you post a short compilable example, just big enough to
reproduce your problem, something like described in
http://www.physci.org/codes/sscce.jsp>.
|
It took a lot of code reading, but after a while I found this
interesting thing in AbstractButton:
if (e.getPropertyName().equals(Action.NAME)) {
Boolean hide = (Boolean)button.getClientProperty("hideActionText");
if (hide == null || hide == Boolean.FALSE) {
String text = (String) e.getNewValue();
button.setText(text);
button.repaint();
}
....so basically I solved it (on my way to subclassing JButton) by
setting a hashtable property on the JButton client component itself:
button.setText(null);
button.putClientProperty("hideActionText", Boolean.TRUE);
....and it works great. But boy do I think Java could need a clean-up soon!
Thanks for your feedback Thomas, really appreciate it!
Casper
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|