mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 12:18:00 +00:00
Add standard API for setting default focus on FDialog
This commit is contained in:
@@ -22,8 +22,6 @@ import java.awt.Component;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.event.WindowFocusListener;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@@ -108,16 +106,7 @@ public class ListChooser<T> {
|
||||
this.optionPane.setButtonEnabled(0, minChoices <= 0);
|
||||
|
||||
if (minChoices != -1) {
|
||||
this.optionPane.addWindowFocusListener(new WindowFocusListener() {
|
||||
@Override
|
||||
public void windowGainedFocus(final WindowEvent e) {
|
||||
ListChooser.this.lstChoices.grabFocus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowLostFocus(final WindowEvent e) {
|
||||
}
|
||||
});
|
||||
this.optionPane.setDefaultFocus(this.lstChoices);
|
||||
}
|
||||
|
||||
if (minChoices > 0) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
@@ -93,7 +94,7 @@ public class FOptionPane extends FDialog {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T showInputDialog(String message, String title, SkinImage icon, T initialInput, T[] inputOptions) {
|
||||
final Component inputField;
|
||||
final JComponent inputField;
|
||||
FTextField txtInput = null;
|
||||
FComboBox<T> cbInput = 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);
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
inputField.requestFocusInWindow();
|
||||
}
|
||||
});
|
||||
optionPane.setDefaultFocus(inputField);
|
||||
inputField.addKeyListener(new KeyAdapter() { //hook so pressing Enter on field accepts dialog
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
@@ -234,12 +230,7 @@ public class FOptionPane extends FDialog {
|
||||
}
|
||||
});
|
||||
if (option == defaultOption) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
btn.requestFocusInWindow();
|
||||
}
|
||||
});
|
||||
this.setDefaultFocus(btn);
|
||||
}
|
||||
this.add(btn, "x " + x + ", w " + buttonWidth + ", h " + buttonHeight);
|
||||
x += dx;
|
||||
|
||||
@@ -21,8 +21,10 @@ import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseMotionAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.event.WindowFocusListener;
|
||||
import java.awt.geom.RoundRectangle2D;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.SwingUtilities;
|
||||
@@ -54,6 +56,7 @@ public class FDialog extends JDialog implements ITitleBarOwner, KeyEventDispatch
|
||||
private Point mouseDownLoc;
|
||||
private final FTitleBar titleBar;
|
||||
private final FPanel innerPanel;
|
||||
private JComponent defaultFocus;
|
||||
|
||||
public FDialog() {
|
||||
this(true);
|
||||
@@ -75,6 +78,20 @@ public class FDialog extends JDialog implements ITitleBarOwner, KeyEventDispatch
|
||||
this.titleBar.setVisible(true);
|
||||
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
|
||||
this.addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
@@ -144,6 +161,10 @@ public class FDialog extends JDialog implements ITitleBarOwner, KeyEventDispatch
|
||||
super.setVisible(visible);
|
||||
}
|
||||
|
||||
public void setDefaultFocus(JComponent comp) {
|
||||
this.defaultFocus = comp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTitle(String title) {
|
||||
super.setTitle(title);
|
||||
|
||||
Reference in New Issue
Block a user