2012-03-31 76 views
5
public class CursorAtStartFocusListener extends FocusAdapter { 

@Override 
public void focusGained(java.awt.event.FocusEvent evt) { 
    Object source = evt.getSource(); 
    if (source instanceof JTextComponent) { 
     JTextComponent comp = (JTextComponent) source; 
     comp.setCaretPosition(0); 
     comp.selectAll(); 
    } 
} } 

jComboBox.getEditor()getEditorComponent()addFocusListener(新 CursorAtStartFocusListener())。;選擇編輯的JComboBox中的所有文本,並設置光標位置

正如您從上面的代碼中看到的,我想選擇可編輯的JComboBox中的所有文本並將光標位置設置爲開頭。 但我有問題,如果我第一次寫comp.setCaretPosition(0),然後comp.selectAll(),文本被選中,但光標是在文本的結尾,否則,如果我先寫comp.selectAll()然後comp.setCaretPosition 0),我將光標放在我想要的位置,但未選中文本。 有什麼想法,我該如何做這件事?

回答

5

CaretselectAll是更好地包裝成invokeLater,但JTextComponent(你可以得到你從JComboBox太),你必須決定是否要使用

  • 選擇所有文字== selectAll()

或使用

  • 選擇Caret(從Document

EDIT

1),用於可編輯JComboBox

Runnable doRun = new Runnable() { 

    @Override 
    public void run() { 
     myComboBox.getEditor().setItem(0); 
     myComboBox.getEditor().selectAll(); 
     myComboBox.requestFocus(); 
    } 
}; 
SwingUtilities.invokeLater(doRun); 

2)或從編輯JComboBox

((JTextField) myComboBox.getEditor().getEditorComponent()) 

導出JTextFieldJFormattedTextField然後添加的FocusListener例如

private FocusListener focsListener = new FocusListener() { 

     @Override 
     public void focusGained(FocusEvent e) { 
      dumpInfo(e); 
     } 

     @Override 
     public void focusLost(FocusEvent e) { 
      //dumpInfo(e); 
     } 

     private void dumpInfo(FocusEvent e) { 
      //System.out.println("Source : " + name(e.getComponent())); 
      //System.out.println("Opposite : " + name(e.getOppositeComponent())); 
      //System.out.println("Temporary: " + e.isTemporary()); 
      final Component c = e.getComponent(); 
      if (c instanceof JFormattedTextField) { 
       EventQueue.invokeLater(new Runnable() { 

        public void run() { 
         ((JFormattedTextField) c).requestFocus(); 
         ((JFormattedTextField) c).setText(((JFormattedTextField) c).getText()); 
         ((JFormattedTextField) c).selectAll(); 
        } 
       }); 
      } else if (c instanceof JTextField) { 
       EventQueue.invokeLater(new Runnable() { 

        public void run() { 
         ((JTextField) c).requestFocus(); 
         ((JTextField) c).setText(((JTextField) c).getText()); 
         ((JTextField) c).selectAll(); 
        } 
       }); 
      } 
     } 

     private String name(Component c) { 
      return (c == null) ? null : c.getName(); 
     } 
    }; 

編輯2:

SSCCE用於編輯的JComboBox,

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import javax.swing.*; 

public class ComboRendererTest { 

    public ComboRendererTest() { 
     JComboBox comboBox = new JComboBox(); 
     comboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXX"); 
     comboBox.addItem(new Double(1)); 
     comboBox.addItem(new Double(2.25)); 
     comboBox.addItem(new Double(3.5)); 
     comboBox.setRenderer(new TwoDecimalRenderer(comboBox.getRenderer())); 
     comboBox.setEditable(true); 

     JComboBox comboBox1 = new JComboBox(); 
     comboBox1.setPrototypeDisplayValue("XXXXXXXXXXXXXXXX"); 
     comboBox1.addItem(new Double(1)); 
     comboBox1.addItem(new Double(2.25)); 
     comboBox1.addItem(new Double(3.5)); 
     comboBox1.setRenderer(new TwoDecimalRenderer(comboBox.getRenderer())); 
     comboBox1.setEditable(true); 

     JFrame frame = new JFrame(); 
     frame.add(comboBox, BorderLayout.NORTH); 
     frame.add(comboBox1, BorderLayout.SOUTH); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) throws Exception { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 
       ComboRendererTest comboRendererTest = new ComboRendererTest(); 
      } 
     }); 
    } 
} 

class TwoDecimalRenderer extends DefaultListCellRenderer { 

    private ListCellRenderer defaultRenderer; 

    public TwoDecimalRenderer(ListCellRenderer defaultRenderer) { 
     this.defaultRenderer = defaultRenderer; 
    } 

    @Override 
    public Component getListCellRendererComponent(JList list, Object value, 
      int index, boolean isSelected, boolean cellHasFocus) { 
     Component c = defaultRenderer.getListCellRendererComponent(
       list, value, index, isSelected, cellHasFocus); 
     if (c instanceof JLabel) { 
      c.setBackground(Color.red); 
     } else { 
      c.setBackground(Color.red); 
      c = super.getListCellRendererComponent(
        list, value, index, isSelected, cellHasFocus); 
     } 
     return c; 
    } 
} 

編輯3.

骯髒的黑客可能是

JTextComponent editor = ((JTextField) myComboBox.getEditor().getEditorComponent()); 
editor.setCaretPosition(getLength()); 
editor.moveCaretPosition(0); 
+0

這代碼不能這麼做,我想要,它只是設置「0」的JComb的內容oBox並選擇它 – Karen 2012-03-31 14:49:26

+0

我不知道你想要什麼,只有兩種可能的方式''setCaret'從'Document'或者使用'selectAll',在這兩種情況下'FocusListener'都非常異步,那麼代碼必須被包裝到' invokeLater' – mKorbel 2012-03-31 14:53:04

+1

我想選擇可編輯JComboBox中的所有文本,並將光標位置設置爲開頭。 – Karen 2012-03-31 14:59:04

相關問題