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



Your

button.setIcon(button.getIcon());

statement isn't doing what you you want - it just sets the icon to what it already is, and so effectively does nothing, leaving the icon null if it's already null. I suggest replacing your "imagesFile" declaration in the constructor with an "images" field (outside the constructor), and replacing the

button.setIcon(button.getIcon());

statements with

button.setIcon(images[i]);

statements. It might be a good idea to do a similar thing with your button text, since you may well want to have different button text and tool tip text in the future.

Hope this helps,

Russ Calvert


On Tuesday, July 8, 2003, at 08:36 PM, email@hidden wrote:

Message: 4
Date: Wed, 9 Jul 2003 03:44:16 +1000
Subject: Newbie: Modifying JButton view in JToolBar
From: Helmut Kurt Burri <email@hidden>
To: email@hidden

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.



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.