2016-01-15 47 views
1

我有一些JRadioButtonsButtonGroup。這些都在一個容器內,並且組中還有其他項目。每當我選擇按鈕組時,焦點總是轉到組中的第一項。但是,我寧願去選擇的項目。獲取焦點在JRadioButtons的ButtonGroup中,以轉到當前選定的項目而不是第一個

若要重現該問題,請使用下面的代碼(添加導入,並且現在忽略我懶得將它全部置於SwingUtilities.invokeLater調用中的事實)。向下選擇單選按鈕,然後向下箭頭至其中一個較後的項目,例如藍色。再次點擊4鍵返回到單選按鈕,您將在頂部的「紅色」單選按鈕上找到焦點。我希望重點放在「藍色」單選按鈕上。

public static void main(String[] args) 
{ 
    JFrame f = new JFrame("JRadioButton Test"); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Container parent = f.getContentPane(); 
    parent.setLayout(new GridBagLayout()); 

    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.anchor = GridBagConstraints.NORTHWEST; 
    gbc.gridx = 1; 
    gbc.gridy = 1; 
    parent.add(new JLabel("Start"), gbc); 
    gbc.gridx++; 
    parent.add(new JTextField(10), gbc); 

    gbc.gridx = 1; 
    gbc.gridy++; 
    parent.add(new JLabel("End"), gbc); 
    gbc.gridx++; 
    parent.add(new JTextField(10), gbc); 

    gbc.gridx = 1; 
    gbc.gridy++; 
    parent.add(new JLabel("Colors"), gbc); 

    gbc.gridx++; 
    final Box buttons = Box.createVerticalBox(); 
    parent.add(buttons, gbc); 
    final ButtonGroup bg = new ButtonGroup(); 
    for (String s : "Red,Orange,Yellow,Green,Blue,Indigo,Violet".split(",")) 
    { 
     JRadioButton radioBtn = new JRadioButton(s); 
     buttons.add(radioBtn); 
     radioBtn.addItemListener(new ItemListener() { 
      @Override 
      public void itemStateChanged(ItemEvent e) 
      { 
       System.out.printf("Itemstate changed to %s\n", 
        e.getStateChange() == ItemEvent.SELECTED ? "SELECTED" : "DESELECTED"); 
      } 
     }); 
     radioBtn.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       System.out.println("JRadioButton action listener"); 
      } 
     }); 
     bg.add(radioBtn); 
    } 

    gbc.gridx = 1; 
    gbc.gridy += 2; 
    gbc.gridwidth = 2; 
    final JLabel currentValue = new JLabel("none"); 
    parent.add(currentValue, gbc); 
    gbc.gridy--; 
    JButton btn = new JButton("Show Button Group Value"); 
    btn.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      ButtonModel selection = bg.getSelection(); 
      for (int i = 0; i < buttons.getComponentCount(); i++) 
      { 
       JRadioButton radioBtn = (JRadioButton)buttons.getComponent(i); 
       ButtonModel loopModel = radioBtn.getModel(); 
       if (loopModel == selection) 
       { 
        currentValue.setText(radioBtn.getText()); 
        return; 
       } 
       currentValue.setText("none"); 
      } 
     } 
    }); 
    parent.add(btn, gbc); 

    f.pack(); 
    f.setVisible(true); 
} 

回答

1

你也許能夠使用FocusTraversalPolicy

buttons.setFocusTraversalPolicyProvider(true); 
buttons.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() { 
    @Override public Component getDefaultComponent(Container focusCycleRoot) { 
    ButtonModel selection = bg.getSelection(); 
    for (Component c: focusCycleRoot.getComponents()) { 
     JRadioButton radioBtn = (JRadioButton) c; 
     ButtonModel loopModel = radioBtn.getModel(); 
     if (loopModel == selection) { 
     return radioBtn; 
     } 
    } 
    return super.getDefaultComponent(focusCycleRoot); 
    } 
}); 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class ButtonGroupFocusTraversalTest { 
    public JComponent makeUI() { 
    JPanel parent = new JPanel(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.anchor = GridBagConstraints.NORTHWEST; 
    gbc.gridx = 1; 
    gbc.gridy = 1; 
    parent.add(new JLabel("Start"), gbc); 
    gbc.gridx++; 
    parent.add(new JTextField(10), gbc); 

    gbc.gridx = 1; 
    gbc.gridy++; 
    parent.add(new JLabel("End"), gbc); 
    gbc.gridx++; 
    JTextField textField = new JTextField(10); 
    parent.add(textField, gbc); 

    gbc.gridx = 1; 
    gbc.gridy++; 
    parent.add(new JLabel("Colors"), gbc); 

    gbc.gridx++; 
    final Box buttons = Box.createVerticalBox(); 
    parent.add(buttons, gbc); 
    final ButtonGroup bg = new ButtonGroup(); 
    for (String s : "Red,Orange,Yellow,Green,Blue,Indigo,Violet".split(",")) { 
     JRadioButton radioBtn = new JRadioButton(s); 
     buttons.add(radioBtn); 
     bg.add(radioBtn); 
    } 

    gbc.gridx = 1; 
    gbc.gridy += 2; 
    gbc.gridwidth = 2; 
    final JLabel currentValue = new JLabel("none"); 
    parent.add(currentValue, gbc); 
    gbc.gridy--; 
    JButton btn = new JButton("Show Button Group Value"); 
    parent.add(btn, gbc); 

    buttons.setFocusTraversalPolicyProvider(true); 
    buttons.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() { 
     @Override public Component getDefaultComponent(Container focusCycleRoot) { 
     ButtonModel selection = bg.getSelection(); 
     for (Component c: focusCycleRoot.getComponents()) { 
      JRadioButton radioBtn = (JRadioButton) c; 
      ButtonModel loopModel = radioBtn.getModel(); 
      if (loopModel == selection) { 
      return radioBtn; 
      } 
     } 
     return super.getDefaultComponent(focusCycleRoot); 
     } 
    }); 
    return parent; 
    } 
    public static void main(String... args) { 
    EventQueue.invokeLater(() -> { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     f.getContentPane().add(new ButtonGroupFocusTraversalTest().makeUI()); 
     f.setSize(320, 320); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    }); 
    } 
} 
+0

我可以發誓,我已在嘗試類似的東西有一點,並不能得到它的工作。您的解決方案效果非常好!那謝謝啦! – Pixelstix

相關問題