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;

}

[1059 byte] By [dayanandabva] at [2007-11-26 15:15:01]
# 1
You just get the 'showWindow()' method to populate a panel with a UI and some response buttons which are wired to listeners which set the value of an instance firel, pop it up in a modal JDialog, and then return the field value.
itchyscratchya at 2007-7-8 9:06:36 > top of Java-index,Desktop,Core GUI APIs...
# 2
Please can u describe more. Sorry I am not getting the picture clearly.Thanks for your immediate reply
dayanandabva at 2007-7-8 9:06:36 > top of Java-index,Desktop,Core GUI APIs...
# 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;

}

}

itchyscratchya at 2007-7-8 9:06:36 > top of Java-index,Desktop,Core GUI APIs...
# 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

dayanandabva at 2007-7-8 9:06:36 > top of Java-index,Desktop,Core GUI APIs...
# 5

Well it looks like it'd work, as a hacky demo. It's a lot of stuff rolled into one class, and it extends JFrame, which is A Bad Thing, and it doesn't make sense to have the calling code create the buttons and then the getResult() code assign listeners to them - so, it's an ugly mish-mash. But it demonstrates the principle ;o)

itchyscratchya at 2007-7-8 9:06:36 > top of Java-index,Desktop,Core GUI APIs...
# 6

hi,

thanks for kind replay,

my problem is i have static method from which i am calling a class which extends jframe (or jinternalframe)

which have 10 to 15 components and this class should return a value after OK button click. that's what i i tried in the above demo code.

please correct me what else should i do.

thanks

daya

dayanandabva at 2007-7-8 9:06:36 > top of Java-index,Desktop,Core GUI APIs...
# 7
Why aren't you just using JOptionPane.showConfirmDialog() anyway?
itchyscratchya at 2007-7-8 9:06:36 > top of Java-index,Desktop,Core GUI APIs...
# 8
no i am not using any jopationpanethanksdaya
dayanandabva at 2007-7-8 9:06:36 > top of Java-index,Desktop,Core GUI APIs...