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



You're right, you're doing something silly- setting the button's icons to be null, which as you suspect actually replaces the button's icon with the null, much as it would replace the button's icon with some other icon 'foo' if you called button.setIcon( foo ).

Your setIcon*View() methods need to test

if ( null == button.getIcon() ){
button.setIcon( new ImageIcon( "images/" + imageFiles[ i ] ) );
}

or something similar, you may want to create the ImageIcons just once and keep those in an array or something. The only reason you don't have the same problem with the text is you're keeping the string in the ToolTip as well as in the label of the button, and although that's a bit redundant ( you might want more descriptive or helpful tooltips ? ) it works.

On Tuesday, July 8, 2003, at 10:44 AM, 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.

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. How can I hide my icons in "Text Only" view without losing my reference to my array of icons?

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);. Is it because button.setToolTipText( toolbarLabels [i] ); and because I am calling my text with button.setText(button.getToolTipText());.

Or am I all wrong about loosing my reference.

I am just learning so I suspect I am doing something silly as usual.

Much thanks to any help you may have for me.

Here is my code that I am working on.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- public class BrowserToolBar extends JToolBar implements ActionListener {

/**
** create two arrays one with the icons and one with the lables
** Then dynamly genrates each icon and corespeconding label via a loop
*/
public BrowserToolBar() {

String imageFiles[] = { "account.png", "compose.png", "customize.png", "delete.png" };
String toolbarLabels[] = { "Account", "Compose", "Customize", "Delete" };

for (int i=0; i<toolbarLabels.length; i++ ) {
ToolBarButton button = new ToolBarButton( "images/" + imageFiles[ i ] , toolbarLabels[ i ] );
button.setToolTipText( toolbarLabels [i] );
button.addActionListener(this); // adds action listener to the buttons

// removes the button border why and how is unknown
// must be set to a minum of 1 pixel wide
button.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(1,1,1,1), button.getBorder() ));
button.setOpaque(false); // set button bg transperint so the jpanel bg becomes visible

/*
** Since Apple L&F has a very small gap between icons and button text
** need to set space to 0 not the default wich is 4.
*/
int gapSize = button.getIconTextGap(); // Get gap size; default is 4
button.setIconTextGap(0); // Set gap size
button.setMargin( new Insets(5, 5, 5, 5) ); // sets margins between icons and text labels
add(button);

}
}

/**
** this class test to see if the tick button has been clicked and
** the user wishes to have lables with their text. if yes
** then the button.setText(button.getToolTipText()); is
** invoked displaying the button labels.
*/
public void setIconAndTextView(boolean iconAndTextView) {
Component c;
int i = 0;

while((c = getComponentAtIndex(i++)) != null) {
ToolBarButton button = (ToolBarButton)c;
button.setIcon(button.getIcon());
button.setText(button.getToolTipText());
}
}

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

while((c = getComponentAtIndex(i++)) != null) {
ToolBarButton button = (ToolBarButton)c;
button.setText(null);
button.setIcon(button.getIcon());
}
}

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

while((c = getComponentAtIndex(i++)) != null) {
ToolBarButton button = (ToolBarButton)c;
button.setIcon(null);
button.setText(button.getToolTipText());
}
}


}
_______________________________________________
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.