Reputation: 12726
I have an application that uses a JDialog to get input from the user, and then searching for files, not a browse dialog, but a more specialized one using metadata.
All this works fine. The only problem is I would like to be able to let the user input the search values, press Ok, and receive these values to do the search and some other operations (from the calling class that opened the dialog) without closing the dialog?
It's necessary to do these operations from the calling class, because that's part of a plugin in an editor.
Basically, in short it's sort of like the way a Find dialog works in any editor - the find dialog stays open while you skip from one found item to the next...
It seems like I'm missing something simple, but I cannot see how to do this.
EDIT:
I tried this in a simple test application according to the tutorial suggested by Nick Rippe, but I think I'm misunderstanding it somehow, because I cannot get it to work. I added a field with getters and setters, and then try to get it:
Main class:
public class TestJFrames {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
TestForm frame = new TestForm();
frame.setDefaultCloseOperation(frame.DO_NOTHING_ON_CLOSE);
frame.addPropertyChangeListener("fileSelected", new FileSelectedListener());
frame.setVisible(true);
}
}
class FileSelectedListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("TEST");
}
}
From the form class:
private String fileSelected;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.setFileSelected("Test");
}
public String getFileSelected() {
return fileSelected;
}
public void setFileSelected(String fileSelected) {
this.fileSelected = fileSelected;
}
I ended up finding a different solution. Posting it here if it could help someone else with a similar difficulty:
It hit me that I could listen to the button event from the calling class, by registering it as a listener to the dialog class. I pretty much followed this example: Create a custom event in Java
Upvotes: 0
Views: 285
Reputation: 6465
The Java Tutorials have a section specifically devoted to this. I'd suggest checking that out.
Combine that with the Getting User Input section, and you have exactly what you wanted.
EDIT
Here's an example from messing with the tutorials a little:
import java.awt.event.*;
import javax.swing.*;
public class Temp extends Box{
JFrame frame;
JTextArea text;
public Temp(JFrame frame){
super(BoxLayout.Y_AXIS);
this.frame = frame;
text = new JTextArea("Clickity Clack, down't the track.\nspam");
add(text);
JButton button = new JButton("Click Me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
launchDialog();
}
});
add(button);
}
public void launchDialog(){
//What you want the find button to do
JButton findButton = new JButton("Find");
findButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int start = text.getText().indexOf("spam");
int end = start + "spam".length();
if(start != -1){
text.requestFocus();
text.select(start, end);
}
}
});
//Cancel button hides the dialog
JButton cancelButton = new JButton("Cancel");
// Create the options displayed in the dialog
final JOptionPane optionPane = new JOptionPane(
"Find \"spam\"?\n"
+ "Do you understand?",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION, null, new Object[]{findButton, cancelButton});
// Build the dialog window
final JDialog dialog = new JDialog(frame,
"Click a button",
false);
//Add action to close button
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});
//Finish up and make it visible
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.setLocation(100, 100);
dialog.pack();
dialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Temp(frame));
frame.pack();
frame.setVisible(true);
}});
}
}
Upvotes: 5