Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie: Modifying JButton view in JToolBar



Helmut Kurt Burri wrote:
| Hi - I am working on my toolbar to that imitates the behavior of a
| standard Cocoa toolbar using Java Swing. At the moment I am working on
| the Icon View options. - "Icon Only" or "Icon and Text" or "Text Only"
| views for the toolbar.
|
| I have managed to get the JButton - "Icon Only" + "Icon and Text" +
| "Text Only" to work fine for me however as soon as I invoke the "Text
| Only" option I seem to lose all reference to my JButton icons. So that
| my JButtons no longer display their icons only their text even when I
| re-select the "Icon Only" or "Icon and Text" view options using the
| pull down menu.

Why would you expect anything else to happen? After you've called setIcon(null), why expect getIcon() to return anything *but* null? The button had only one reference to the icon, and in calling "setIcon(null)", you tell it to throw that reference away. If you need a reference saved even when the button has no icon, you have to save it yourself.


|
| I think it is because when I set the view to "Text Only" I am deleting
| all reference in memory to the Icon array by setting the array to null
| like this button.setIcon(null). Well that is my guess.

What have you done to determine whether your guess is correct?

|
| Also why am I not loosing my reference to my text when I am setting my
| text to null in my "Icon Only" view using button.setText(null);.

You *are* losing it, exactly the same way you lose the reference to the icon. You get the text back because you've *also* used it as the tool-tip text. If something caused the tool-tip text to change, the button's text would be set to whatever the tool-tip text became. (By the way: the tool-tip is supposed to *describe* what the button does, not just repeat the button's text. [Take a look at the tool tips used by PB for its toolbars.] If you'd done things that way, you'd have seen what was going on.)


The basic problem I see is that you're trying to create a non-class-based solution, and trying to avoid keeping your own references to things you want references to. Much of what you posted should be part of the ToolBarButton class (assuming it does what its name suggests):

public class ToolBarButton
extends JButton // or whatever
{
public static final int TEXT = 0;
public static final int ICON = 1;
public static final int BOTH = 2;

private Icon icon_;
private String text_;
private int style_ = BOTH;

public void setStyle(int style)
{
style_ = style;

// Note the "super" calls. They prevent the
// ToolBarButton method from being called.
if(style==TEXT || style==BOTH)
super.setText(text_);
else
super.setText("");

if(style==ICON || style==BOTH)
super.setIcon(icon_);
else
super.setIcon(null);
}

public void setIcon(Icon icon)
{
icon_ = icon;
setStyle(style_); // Update the displayed icon.
}

public void setText(String text)
{
text__ = text;
setStyle(style_); // Update the displayed text.
}

// etc.
}

The main code would then simply be

public void setIconAndTextView(boolean iconAndTextView) {
Component c;
int i = 0;

while((c = getComponentAtIndex(i++)) != null)
((ToolBarButton) c).setStyle(ToolBarButton.BOTH);
}

public void setIconOnlyView(boolean iconOnlyView) {
Component c;
int i = 0;

while((c = getComponentAtIndex(i++)) != null)
((ToolBarButton) c).setStyle(ToolBarButton.ICON);
}

public void setTextOnlyView(boolean textOnlyView) {
Component c;
int i = 0;

while((c = getComponentAtIndex(i++)) != null)
((ToolBarButton) c).setStyle(ToolBarButton.TEXT);
}

}

The problem with the posted code is precisely that ToolBarButton is *not* responsible for taking care of itself. It relies on the BrowserToolBar to locate the text and icons whenever the state of a button changes, when it could perfectly well keep track of them itself. Once you make it responsible for itself, all the "losing references" problems vanish.

By the way, I'd write the loops using "for" statements:

public void setTextOnlyView(boolean textOnlyView) {
Component c;

for(int i=0; (c = getComponentAtIndex(i)) != null; ++i)
((ToolBarButton) c).setStyle(ToolBarButton.TEXT);
}

which reduces the clutter of variable declarations somewhat. (Yes, declaring the variable within the "for" is perfectly legal.) You could also simplify things (with the revised ToolBarButton) by writing

public void setIconAndTextView(boolean iconAndTextView) {
setToolbarStyle(ToolBarButton.BOTH);
}

public void setIconOnlyView(boolean iconOnlyView) {
setToolbarStyle(ToolBarButton.ICON);
}

public void setTextOnlyView(boolean textOnlyView) {
setToolbarStyle(ToolBarButton.TEXT);
}

public void setToolbarStyle(int style) {
Component c;

while((c = getComponentAtIndex(i++)) != null)
((ToolBarButton) c).setStyle(style);
}
}

which moves all button modification into a single routine, making it harder for bugs to lurk in just one of the methods.

(Is there a reason your methods have boolean parameters? Your original code never uses them.)

Glen Fisher
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Be sure to read the FAQ http://developer.apple.com/java/faq/ before posting
Do not post admin requests to the list. They will be ignored.

References: 
 >Newbie: Modifying JButton view in JToolBar (From: Helmut Kurt Burri <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.