回答
解決方法一:通過焦點事件做吧。不是最好的解決方案。
public static void main(final String[] args) {
// simple window preparation
final JFrame f = new JFrame();
f.setBounds(200, 200, 400, 400);
f.setVisible(true);
{ // this sleep part shall simulate a user doing some stuff
try {
Thread.sleep(2345);
} catch (final InterruptedException ignore) {}
}
{ // here's the interesting part for you, this is what you put inside your button listener or whatever
final JTextField t = new JTextField("Hello World!");
t.addFocusListener(new FocusListener() {
@Override public void focusLost(final FocusEvent pE) {}
@Override public void focusGained(final FocusEvent pE) {
t.selectAll();
}
});
f.add(t);
f.validate();
t.requestFocus();
}
}
謝謝你的迴應,它真的幫助我 –
使用Thread.sleep的目的是什麼?這在Swing應用程序中通常被認爲是不好的做法 – MadProgrammer
這只是爲了讓該領域在延遲後彈出,而不是做所有這些點擊按鈕 - 隱藏 - 元素 - 新元素 - 彈出 - 獲取 - 聚焦 - 全部被選中他在原始問題中正在談論。但是我會編輯我的帖子,使其更清晰。 – JayC667
JTextField類包含其API中的方法。
這可以幫助:
http://forums.codeguru.com/showthread.php?308517-How-do-you-highlight-the-text-in-a-JTextfield
JTextField.selectAll()是你所需要的。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SelectAll
{
private int count = 0;
private void displayGUI()
{
JFrame frame = new JFrame("Select All");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final JPanel contentPane = new JPanel();
JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
JTextField tfield = new JTextField(10);
tfield.setText("" + (++count));
contentPane.add(tfield);
tfield.requestFocusInWindow();
tfield.selectAll();
contentPane.revalidate();
contentPane.repaint();
}
});
contentPane.add(addButton);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new SelectAll().displayGUI();
}
});
}
}
似乎您必須將selectAll()與[requestFocusInWindow()](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#requestFocusInWindow())一起使用,以獲得所需的效果。如我的編輯所示。 –
- 1. JTextField在焦點增加時選擇所有內容
- 2. 製作一個視圖,其內容出現在所有故事板場景中
- 3. 選擇除一個div的所有元素及其內容
- 4. 如何在製作其他所有內容時播放Movieclip?
- 5. mod_rewrite選擇所有內容
- 6. 選擇'〜`的所有內容
- 7. 防止Chrome在畫布外繪製時選擇所有內容?
- 8. EntityFramework。選擇其孫子出現在IDS
- 9. 選擇選項將強制選擇其他選項
- 10. 選擇所有焦點在很多jTextField
- 11. 時間選擇器容器犯規包裹其內容
- 12. 所有圖像內容出現在div
- 13. php/sql選擇在48小時內過期的所有內容
- 14. 在清除DataGridView的所有列時是否有其他選擇?
- 15. 禁用選擇jTextfield的所有亮點
- 16. 可可 - 強制退出所有其他應用程序
- 17. 末JavaScript函數及其所有內容
- 18. 縮放視圖及其所有內容
- 19. 選擇表中未出現在其他表的外鍵中的所有項目
- 20. Hibernate命名查詢(選擇所有實體不出現在其他實體中)
- 21. 製作垂直導航欄STRETCH時出超出其內容
- 22. 如何在第一次出現後選擇列表中的所有內容?
- 23. JTextField沒有出現
- 24. 在MySQL中選擇變量時防止出現重複內容
- 25. 寬度爲0的Android Textview強制內容在其下消失
- 26. 強制@@ Servername在SQL Server中返回其他內容
- 27. 強制Google InfoWindow重新繪製自己或刷新其內容
- 28. 如何強制div元素將其內容保留在容器內
- 29. 在主頁上強制HTTPS,但在所有其他上強制HTTP
- 30. android選項卡內容在其選擇前加載
是你在找什麼? http://stackoverflow.com/questions/1178312/how-to-select-all-text-in-a-jformattedtextfield-when-it-gets-focus/1178596#1178596 – SomeJavaGuy
我試過這兩個問題之前,我問這個問題,這個答案的問題是它沒有工作。此答案的另一個問題是文本字段只有在您單擊文本後纔會獲得焦點,而不是在出現時才獲得焦點 –
請參閱http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html關於如何注重Swing組件 –