| |||
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] |
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());
}
}
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- --
| Home | Archives | FAQ | Terms/Conditions | Contact | RSS | Lists | About |
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.