How can i create a dialog like JOptionPane functionality
I want to show a dialog to the user by calling a method.
1. showWindow() method will create an object of a Dialog.
2. Dialog will return an Integer Data.
3. this Integer data is the return type of showWindow() method.
How can I do that?
For Example,
publicint showWindow(){
int iSelectedOption = JOptionPane.showConfirmDialog(null,"Do you want to save?");
return iSelectedOption;
}
Expected:
publicint showWindow(){
GeneralDialog dialog =new GeneralDialog();
int iSelectedOption = dialog.selectedValue();
return iSelectedOption;
}
# 3
Sorry, "firel" was supposed to be "field." Not sure what happened there.
Um, I'm not sure how much clearer to make it. In your method, you build the content of the modal dialog - whatever that is, you know, I don't. It should include buttons, like any dialog. The result of pressing those buttons should be that (a) a field in your 'GeneralDialog' object (note that GeneralDialog has no need to extend JDialog) is set according to which button was pressed, and (b) the dialog is closed, thus triggering the return from the setVisible() method. Then you just return the value of the field.
Somthing like this - I'll leave you to write the bit which creates the buttons and wires listeners to them.
public final class GeneralDialog
{
public static final int YES = 1;
public static final int NO = 2;
public static final int CANCEL = 4;
private int result = 0;
public int getResult(Component parent, JComponent content, int buttonBits)
{
JComponent panel = new JPanel(new BorderLayout());
panel.add(content, BorderLayout.CENTER);
panel.add(createButtonPanel(buttonBits), BorderLayout.PAGE_END);
JDialog d = new JDialog(parent, true);
d.setVisible(true);
return this.result;
}
}
# 4
thanks for your help,
i did a small demo, just chenk and tell me what did is right?
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SimulateOptionPane extends JFrame implements ActionListener
{
JButtonbtn= new JButton("YES");
JButtonbtn1= new JButton("NO");
public static final int YES= 1;
public static final int NO= 2;
private int result = 0;
public SimulateOptionPane()
{
JPanel jp = new JPanel();
jp.add(new JTextField(10));
JPanel jp1 = new JPanel();
btn.addActionListener(this);
btn1.addActionListener(this);
jp1.add(btn);
jp1.add(btn1);
int i = getResult(this, jp, jp1);
System.out.println("->" + i);
}
public int getResult(JFrame parent, JPanel jp, JPanel jp1)
{
JComponent panel = new JPanel(new BorderLayout());
panel.add(jp, BorderLayout.CENTER);
panel.add(jp1, BorderLayout.PAGE_END);
JDialog d = new JDialog(parent, true);
d.add(panel);
d.pack();
d.setVisible(true);
return this.result;
}
public static void main(String[] args)
{
new SimulateOptionPane();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btn)
{
result = YES;
dispose();
}
else if (e.getSource() == btn1)
{
result = NO;
dispose();
}
}
}
thnaks
daya