I have a screen with 2 radio buttons. I have manually selected the second radio button to be in selected mode when the screen in visible. So i have set property saying 'rdb1.setSelected(true);' when the screen activates. The radio button2 is actually selected but i find that there is a border around the text for the first radio button rather than for the second one. How do i hide this border ? I tried making the border to be empty but even then when the screen is loaded, we find a border around the text in the radio button.
Does anybody has any idea as to why this is happening? If so, please help me.
1) at the end of the GUI construction add radioButton2.requestFocus(), but you must have setVisible(true) set prior to requesting focus
2) add these lines radioButton1.setFocusPainted(false); radioButton2.setFocusPainted(false);
Smriti Anchu
Ranch Hand
Joined: Dec 21, 2004
Posts: 40
posted
0
Hi Michel
thanks for your help...
1. I tried adding radioButton2.requestFocus() after adding the radio button to the panel, but even then radio button1 has border.
2. Later i tried adding these lines radioButton1.setFocusPainted(false); radioButton2.setFocusPainted(false); Now the radioButton1 doesnt have border. But when the particular screen is focussed, since the radioButton2 is selected, it should have border, but it doesnt display the border inspite of setting the radioButton2.setFocusPainted(true); Only when they are clicked in mouse listener, i have set the ofcusPainted to true.
Is there any way such that when the screen is focussed, the radioButton2 to have a border around hte text.
>Is there any way such that when the screen is focussed, the radioButton2 to >have a border around hte text.
You mustn't have read line 2 of (1) >1) at the end of the GUI construction add radioButton2.requestFocus(), >but you must have setVisible(true) set prior to requesting focus//<---------
I have a class that extends BasicRadioButtonUI and in that class I have written code to traverse the radiobutton in a button group using arrow keys. Using arrow keys I am successfully able to select the radio buttons. However the problem I am facing is I am not able to see the focus line on the selected radio button. I used requestFocus method but still not able to see the focus line. All my radio buttons are visible. I even tried setFocusPainted method of the AbstractButton to set the focus line but invain.
Can you please help me as to how can I get the focus line on the selected radio button ?
Waiting for your reply. Thanks and Regards Rohit.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
> I used requestFocus method
see if requestFocusInWindow() makes a difference
here's a simple demo of traversing the button group with 'Down' arrow key, (reverse the logic for 'Up'arrow key)
Is there any way such that when the screen is focussed, the radioButton2 to have a border around hte text.
The "border" you are talking about is the focus. Being selected and having focus are two orthogonal things, though - the latter means that the component currently can be operated via the keyboard.
If you want the radioButton two to get the focus every time the screen is focussed, you will have to implement this. What exactly do you mean by "screen"? Is it a JDialog or something?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rohit Bhagwat
Ranch Hand
Joined: Dec 19, 2004
Posts: 205
posted
0
Originally posted by Michael Dunn: > I used requestFocus method
see if requestFocusInWindow() makes a difference
here's a simple demo of traversing the button group with 'Down' arrow key, (reverse the logic for 'Up'arrow key)
I tried requestFocusInWindow too still now working. Here is what I have written. public class MyRadioButtonUI extends BasicRadioButtonUI { @Override public static ComponentUI createUI(JComponent c) { if (null == sInstance) { sInstance = new MyRadioButtonUI(); } return sInstance; } // ///////////// Protected member functions //////// @Override protected void installListeners(AbstractButton button) { super.installListeners(button); // map the correct actions to the arrow keys (Issue 45) button.getActionMap().put(NEXT_ACTION, new NextAction()); button.getActionMap().put(PREVIOUS_ACTION, new PreviousAction()); button.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put( sDownKS, NEXT_ACTION);
@Override protected void uninstallListeners(AbstractButton button) { super.uninstallListeners(button); // unmap the actions from the arrow keys button.getActionMap().remove(NEXT_ACTION); button.getActionMap().remove(PREVIOUS_ACTION); button.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) .remove(sDownKS); button.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) .remove(sUpKS); button.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) .remove(sRightKS); button.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) .remove(sLeftKS); } // /////////////////////// Private member functions////////////////////////
/** * The evt, from which we can get the event source and the operation type. * The operation tyep can be NEXT or PREVIOUS. * * @param evt, * The event object. * * @param operation * The type of the operation performed. */ private void selectRadioButton(ActionEvent evt, int operation) { // Get the source of the event. Object evtSource = evt.getSource(); // Check whether the source is JRadioButton, it so, whether it is // visible if (!(evtSource instanceof JRadioButton && ((JRadioButton) evtSource).isVisible() && ((JRadioButton) evtSource) .isEnabled())) { return; }
// Get the button model from the source. ButtonModel model = ((AbstractButton) evtSource).getModel();
// get the ButtonGroup of the button from the button model ButtonGroup group = bm.getGroup(); if (group == null) return;
// Get all the buttons in the group Enumeration<AbstractButton> e = group.getElements();
if (e == null) return;
// First button and last button are used, so that if next is clicked and // the current selected button is last button, we can move to first // button and so on. JRadioButton firstButton = null;
while (e.hasMoreElements()) { currentElement = e.nextElement(); if (currentElement instanceof JRadioButton && ((JRadioButton) currentElement).isVisible() && ((JRadioButton) currentElement).isEnabled()) { // If firstButton is not set yet, currentElement is that first // button if (null == firstButton) { firstButton = (JRadioButton) currentElement; }
if (evtSource == currentElement) { srcFound = true; } else if (!srcFound) { // if we are here, the source has not been yet found but // it's a valid // button closer to the source that the last previousButton // value previousButton = (JRadioButton) currentElement; } else if (srcFound && nextButton == null) { // if we are here, that's because the source has been found // and that current element // is the next valid button of the list nextButton = (JRadioButton) currentElement; } // if we found a new "valid" JRadioButton, it become the new // last "valid" JRadioButton // of the list lastButton = (JRadioButton) currentElement; } }
if (srcFound) { JRadioButton newSelectedButton = null; if (NEXT == operation) { // Select Next button. // Cycle to the first button if the source button is the // last of the group. newSelectedButton = (null == nextButton) ? firstButton : nextButton; } else { // Select previous button // Cycle to the last button if the source button is the // first button of the group. newSelectedButton = (null == previousButton) ? lastButton : previousButton; } if (newSelectedButton != null) { newSelectedButton.requestFocusInWindow(); newSelectedButton.setSelected(true); } } }
// /////////////////////// Inner Classes //////////////////////// private class NextAction extends AbstractAction { public NextAction() { super(NEXT_ACTION); } public void actionPerformed(ActionEvent e) { MyRadioButtonUI.this.selectRadioButton(e, NEXT); } }
private class PreviousAction extends AbstractAction { public PreviousAction() { super(PREVIOUS_ACTION); } public void actionPerformed(ActionEvent e) { MyRadioButtonUI.this.selectRadioButton(e, PREVIOUS); } } // /////////////////////// Class Data //////////////////////// private static final int NEXT = 1; private static final int PREVIOUS = 2; private static final String NEXT_ACTION = "Next"; private static final String PREVIOUS_ACTION = "Previous"; private static MyRadioButtonUI sInstance; private static KeyStroke sDownKS = KeyStroke.getKeyStroke("pressed DOWN"); private static KeyStroke sUpKS = KeyStroke.getKeyStroke("pressed UP"); private static KeyStroke sRightKS = KeyStroke.getKeyStroke("pressed RIGHT"); private static KeyStroke sLeftKS = KeyStroke.getKeyStroke("pressed LEFT"); }