I am trying to create a custom input dialog window so that I can have a right click event handler which lets me copy/cut/paste text using the mouse. However, after constructing the following code [as a standalone class, how I plan on using it], I lost the ability to finalize my selection with the 'Enter' key:
class CustomInputDialog {
public static javax.swing.JLabel lbl;
public static javax.swing.JTextField txt;
public static javax.swing.JPopupMenu pop;
public static String showInputDialog(String message, String guess) {
lbl = new javax.swing.JLabel(message);
txt = new javax.swing.JTextField(guess, 10);
pop = new javax.swing.JPopupMenu();
javax.swing.Action copyAction = new javax.swing.AbstractAction(javax.swing.text.DefaultEditorKit.copyAction) {
public void actionPerformed(java.awt.event.ActionEvent e) {
txt.copy();
}
};
javax.swing.Action cutAction = new javax.swing.AbstractAction(javax.swing.text.DefaultEditorKit.cutAction) {
public void actionPerformed(java.awt.event.ActionEvent e) {
txt.cut();
}
};
javax.swing.Action pasteAction = new javax.swing.AbstractAction(javax.swing.text.DefaultEditorKit.pasteAction) {
public void actionPerformed(java.awt.event.ActionEvent e) {
txt.paste();
}
};
javax.swing.JMenuItem m1 = new javax.swing.JMenuItem(copyAction);
javax.swing.JMenuItem m2 = new javax.swing.JMenuItem(cutAction);
javax.swing.JMenuItem m3 = new javax.swing.JMenuItem(pasteAction);
m1.setText("Copy"); m2.setText("Cut"); m3.setText("Paste");
pop.add(m1); pop.add(m2); pop.add(m3);
txt.setComponentPopupMenu(pop);
java.awt.Container ct = new java.awt.Container();
ct.setLayout(new java.awt.GridLayout(2,1));
ct.add(lbl); ct.add(txt); txt.selectAll();
Object[] options = {"OK", "Cancel"};
int responce = javax.swing.JOptionPane.showOptionDialog(null, ct, "TITLE.", javax.swing.JOptionPane.OK_CANCEL_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE, null, options, txt);
if (responce == javax.swing.JOptionPane.OK_OPTION) {
return txt.getText();
}
return null;
}
}
In an attempt to get the enter key working, I separated an instance of the JOptionPane class, so I could refer to it in a keyHandler event using the JTextField. In doing the following code adjustments, now not only does the enter key do nothing, but the method seems to always return null, even if the OK button is selected by the user via mouse:
class CustomInputDialog {
public static javax.swing.JLabel lbl;
public static javax.swing.JTextField txt;
public static javax.swing.JPopupMenu pop;
public static javax.swing.JOptionPane op;
public static String showInputDialog(String message, String guess) {
lbl = new javax.swing.JLabel(message);
txt = new javax.swing.JTextField(guess, 10);
pop = new javax.swing.JPopupMenu();
javax.swing.Action copyAction = new javax.swing.AbstractAction(javax.swing.text.DefaultEditorKit.copyAction) {
public void actionPerformed(java.awt.event.ActionEvent e) {
txt.copy();
}
};
javax.swing.Action cutAction = new javax.swing.AbstractAction(javax.swing.text.DefaultEditorKit.cutAction) {
public void actionPerformed(java.awt.event.ActionEvent e) {
txt.cut();
}
};
javax.swing.Action pasteAction = new javax.swing.AbstractAction(javax.swing.text.DefaultEditorKit.pasteAction) {
public void actionPerformed(java.awt.event.ActionEvent e) {
txt.paste();
}
};
javax.swing.JMenuItem m1 = new javax.swing.JMenuItem(copyAction);
javax.swing.JMenuItem m2 = new javax.swing.JMenuItem(cutAction);
javax.swing.JMenuItem m3 = new javax.swing.JMenuItem(pasteAction);
m1.setText("Copy"); m2.setText("Cut"); m3.setText("Paste");
pop.add(m1); pop.add(m2); pop.add(m3);
txt.setComponentPopupMenu(pop);
txt.addKeyListener(new java.awt.event.KeyListener() {
@Override public void keyTyped(java.awt.event.KeyEvent e) {}
@Override public void keyReleased(java.awt.event.KeyEvent e) {}
@Override public void keyPressed(java.awt.event.KeyEvent e) {
if (e.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
op.setInitialValue(javax.swing.JOptionPane.OK_OPTION);
op.selectInitialValue();
}
}
});
java.awt.Container ct = new java.awt.Container();
ct.setLayout(new java.awt.GridLayout(2,1));
ct.add(lbl); ct.add(txt); txt.selectAll();
Object[] options = {"OK", "Cancel"};
op = new javax.swing.JOptionPane(ct, javax.swing.JOptionPane.QUESTION_MESSAGE, javax.swing.JOptionPane.OK_CANCEL_OPTION, null, options, txt);
op.createDialog(null, "TITLE.").setVisible(true);
int returnValue = (op.getValue() instanceof Integer) ? ((Integer)op.getValue()).intValue() : javax.swing.JOptionPane.CANCEL_OPTION;
if (returnValue == javax.swing.JOptionPane.OK_OPTION) {
return txt.getText();
}
return null;
}
}
I've been going through the javax.swing references on the APIs, but I cannot find anything that seems to make sense to me as to a way to work towards the solution, if anyone could give me some help or guidance, it would be much appreciated. Thanks!
Ps: I think the issue may be in "returnValue" getting set to JOptionPane.CANCEL_OPTION as it is being executed before the JOptionPane dialog is finally closed and a value is entered. However, on the examples I was looking at, it didn't seem like there was an onClosed event handler or something similar used.
Pps [note]: I originally posted this in the Java Programming area, and was directed to come here. Original Post: [http://forums.sun.com/thread.jspa?threadID=5445202|http://forums.sun.com/thread.jspa?threadID=5445202].