Add standard API for setting default focus on FDialog

This commit is contained in:
drdev
2014-01-05 03:30:24 +00:00
parent b3bec906ef
commit d9e5d93265
3 changed files with 26 additions and 25 deletions

View File

@@ -22,8 +22,6 @@ import java.awt.Component;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@@ -108,16 +106,7 @@ public class ListChooser<T> {
this.optionPane.setButtonEnabled(0, minChoices <= 0); this.optionPane.setButtonEnabled(0, minChoices <= 0);
if (minChoices != -1) { if (minChoices != -1) {
this.optionPane.addWindowFocusListener(new WindowFocusListener() { this.optionPane.setDefaultFocus(this.lstChoices);
@Override
public void windowGainedFocus(final WindowEvent e) {
ListChooser.this.lstChoices.grabFocus();
}
@Override
public void windowLostFocus(final WindowEvent e) {
}
});
} }
if (minChoices > 0) { if (minChoices > 0) {

View File

@@ -8,6 +8,7 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
@@ -93,7 +94,7 @@ public class FOptionPane extends FDialog {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T showInputDialog(String message, String title, SkinImage icon, T initialInput, T[] inputOptions) { public static <T> T showInputDialog(String message, String title, SkinImage icon, T initialInput, T[] inputOptions) {
final Component inputField; final JComponent inputField;
FTextField txtInput = null; FTextField txtInput = null;
FComboBox<T> cbInput = null; FComboBox<T> cbInput = null;
if (inputOptions == null) { if (inputOptions == null) {
@@ -107,12 +108,7 @@ public class FOptionPane extends FDialog {
} }
final FOptionPane optionPane = new FOptionPane(message, title, icon, inputField, new String[] {"OK", "Cancel"}, -1); final FOptionPane optionPane = new FOptionPane(message, title, icon, inputField, new String[] {"OK", "Cancel"}, -1);
SwingUtilities.invokeLater(new Runnable() { optionPane.setDefaultFocus(inputField);
@Override
public void run() {
inputField.requestFocusInWindow();
}
});
inputField.addKeyListener(new KeyAdapter() { //hook so pressing Enter on field accepts dialog inputField.addKeyListener(new KeyAdapter() { //hook so pressing Enter on field accepts dialog
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
@@ -234,12 +230,7 @@ public class FOptionPane extends FDialog {
} }
}); });
if (option == defaultOption) { if (option == defaultOption) {
SwingUtilities.invokeLater(new Runnable() { this.setDefaultFocus(btn);
@Override
public void run() {
btn.requestFocusInWindow();
}
});
} }
this.add(btn, "x " + x + ", w " + buttonWidth + ", h " + buttonHeight); this.add(btn, "x " + x + ", w " + buttonWidth + ", h " + buttonHeight);
x += dx; x += dx;

View File

@@ -21,8 +21,10 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.geom.RoundRectangle2D; import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
@@ -54,6 +56,7 @@ public class FDialog extends JDialog implements ITitleBarOwner, KeyEventDispatch
private Point mouseDownLoc; private Point mouseDownLoc;
private final FTitleBar titleBar; private final FTitleBar titleBar;
private final FPanel innerPanel; private final FPanel innerPanel;
private JComponent defaultFocus;
public FDialog() { public FDialog() {
this(true); this(true);
@@ -75,6 +78,20 @@ public class FDialog extends JDialog implements ITitleBarOwner, KeyEventDispatch
this.titleBar.setVisible(true); this.titleBar.setVisible(true);
addMoveSupport(); addMoveSupport();
this.addWindowFocusListener(new WindowFocusListener() {
@Override
public void windowGainedFocus(final WindowEvent e) {
if (FDialog.this.defaultFocus != null) {
FDialog.this.defaultFocus.grabFocus();
FDialog.this.defaultFocus = null; //reset default focused component so it doesn't receive focus if the dialog later loses then regains focus
}
}
@Override
public void windowLostFocus(final WindowEvent e) {
}
});
if (isSetShapeSupported) { //if possible, set rounded rectangle shape for dialog if (isSetShapeSupported) { //if possible, set rounded rectangle shape for dialog
this.addComponentListener(new ComponentAdapter() { this.addComponentListener(new ComponentAdapter() {
@Override @Override
@@ -144,6 +161,10 @@ public class FDialog extends JDialog implements ITitleBarOwner, KeyEventDispatch
super.setVisible(visible); super.setVisible(visible);
} }
public void setDefaultFocus(JComponent comp) {
this.defaultFocus = comp;
}
@Override @Override
public void setTitle(String title) { public void setTitle(String title) {
super.setTitle(title); super.setTitle(title);