Customized popup window how to?

O

Osiaq

I have created a form and I want to open it from another form as a
dialog

Is there any equivalent to C#:

void OpenMyChidlrenForm()
{
Form f1=new MyChildForm();
f1.ShowDialog();
}

and get dialog response from child form?
 
O

Osiaq

Could you define "form"?  There's no Form class in the Java API, so I
have no idea what you are referring too.

Preferably, with a short source code example.

http://pscode.org/sscce.html

Thanks Mark!
I mean JFrame. I want to open popup (modal) JFrame with 3 buttons,
assign the integers 0,1,2 to the buttons and return them to "parent"
JFrame. During this process, main JFrame should be "frozen" (thread
paused) until the response won't come from this "modal popup"

Basically I want to recreate JOptionPane.showConfirmDialog,
using separate JFrame.
 
E

Eric Sosman

Thanks Mark!
I mean JFrame. I want to open popup (modal) JFrame with 3 buttons,
assign the integers 0,1,2 to the buttons and return them to "parent"
JFrame. During this process, main JFrame should be "frozen" (thread
paused) until the response won't come from this "modal popup"

Basically I want to recreate JOptionPane.showConfirmDialog,
using separate JFrame.

Why? Are you doing things The Hard Way just to show they can
be done? "I will perform this appendectomy using only a can opener
and two ordinary teaspoons," that sort of thing?

The JDialog class exists for a reason, and JOptionPane exists
to help with the commoner uses of JDialog. If there's something about
your situation that prompts you to reject these obvious candidates,
you should explain what makes your case so unusual lest the answers
you receive run up against the same objections.
 
M

markspace

I mean JFrame. I want to open popup (modal) JFrame with 3 buttons,
assign the integers 0,1,2 to the buttons and return them to "parent"
JFrame. During this process, main JFrame should be "frozen" (thread
paused) until the response won't come from this "modal popup"

Basically I want to recreate JOptionPane.showConfirmDialog,
using separate JFrame.


Ah OK. Short answer: no you can't do that.

Longer answer: Use a JPanel instead of a JFrame, that'll work. You can
also use JDialog directly or use some of the other methds in JOptionPane
to create different types of JOptionPanes.

There's an example of putting three buttons on a JOptionPane here:

<http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html>

Here's a quick example of popping a JOptionPane over a JFrame I whipped up.

package test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JOptionPaneTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final MainView frame = new MainView();

frame.addButtonActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] options = {"Yes, please",
"No, thanks",
"No eggs, no ham!"};
int n = JOptionPane.showOptionDialog(frame,
"Would you like some green eggs to go "
+ "with that ham?",
"A Silly Question",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[2]);
frame.setResult( Integer.toString( n ) );
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
class MainView extends JFrame {
private final JLabel resultLabel = new JLabel();
private final JButton button = new JButton("Click me");
public MainView() {
JPanel panel = new JPanel();
panel.add(resultLabel);
panel.add(button);
add(panel);
}
public void addButtonActionListener(ActionListener a) {
button.addActionListener(a);
}
public void setResult( String result ) {
resultLabel.setText(result);
}
}
 
O

Osiaq

I mean JFrame. I want to open popup (modal) JFrame with 3 buttons,
assign the integers 0,1,2 to the buttons and return them to "parent"
JFrame. During this process, main JFrame should be "frozen" (thread
paused) until the response won't come from this "modal popup"
Basically I want to recreate JOptionPane.showConfirmDialog,
using separate JFrame.

Ah OK. Short answer: no you can't do that.

Longer answer: Use a JPanel instead of a JFrame, that'll work.  You can
also use JDialog directly or use some of the other methds in JOptionPane
to create different types of JOptionPanes.

There's an example of putting three buttons on a JOptionPane here:

<http://download.oracle.com/javase/tutorial/uiswing/components/dialog....>

Here's a quick example of popping a JOptionPane over a JFrame I whipped up.

package test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JOptionPaneTest {
    public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
          public void run() {
             final MainView frame = new MainView();

             frame.addButtonActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                   Object[] options = {"Yes, please",
                      "No, thanks",
                      "No eggs, no ham!"};
                   int n = JOptionPane.showOptionDialog(frame,
                           "Would you like some green eggs to go "
                           + "with that ham?",
                           "A Silly Question",
                           JOptionPane.YES_NO_CANCEL_OPTION,
                           JOptionPane.QUESTION_MESSAGE,
                           null,
                           options,
                           options[2]);
                   frame.setResult( Integer.toString( n ) );
                }
             });
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.pack();
             frame.setLocationRelativeTo(null);
             frame.setVisible(true);
          }
       });
    }}

class MainView extends JFrame {
    private final JLabel resultLabel = new JLabel();
    private final JButton button = new JButton("Click me");
    public MainView() {
       JPanel panel = new JPanel();
       panel.add(resultLabel);
       panel.add(button);
       add(panel);
    }
    public void addButtonActionListener(ActionListener a) {
       button.addActionListener(a);
    }
    public void setResult( String result ) {
       resultLabel.setText(result);
    }

}

Thanks again, Mark!

I believe you and Eric can show me correct approach to the problem I
have.
I'm coming from strong C# background, repeating my current knowledge
using Java. That sucks and I know it.
Java is nothing what I have done before. The problem is simple (for
you) and frustrating (for me):

I have to implement popup window with predefined background and nice
buttons (OK and Cancel) designed by our design team.
I cannot use 80-ish default style of the buttons, they have nice, blue
png skins.

This is simple "Do you agree?" popup window.

Im agree im doing it probably hard way but dont judge too fast - as I
said im repeating my .NET/C# experience here and my last approach to
Java was like 3 years ago when I quit before I started.

Again: I see the only one way - create new JPanel or JFrame, add
background image, add skinned buttons (no this.BackgroundImage option
for buttons and panels, I guess) and finally create this popup. I'm
not brave enough to imagine, how will I pass results (OK or Cancel) to
the parent form ;)

Eric: is it really as easy as set the background image of JOptionPane,
skin the buttons with png (and place them in correct locations) and
fit it in reasonable amount of the code?
 
M

markspace

The problem is simple (for you)


Not really. Skinning an entire GUI correctly can be a tough problem.

my last approach to Java was like 3 years ago when I quit before I
started.


Hmm, OK.

Again: I see the only one way - create new JPanel or JFrame, add
background image, add skinned buttons (no this.BackgroundImage
option for buttons and panels, I guess) and finally create this
popup.


That's more or less how I'd do it.

I'm not brave enough to imagine, how will I pass results (OK
or Cancel) to the parent form ;)


The same way I showed you, and I'd guess the same way you'd do it in C#:
use a reference to the parent "form" in the controller for the dialog.

Eric: is it really as easy as set the background image of
JOptionPane, skin the buttons with png (and place them in correct
locations) and fit it in reasonable amount of the code?


Yes and no, depending on other requirements. Do you have any code to
show us? What images and where? Can we get that SSCCE from you?

http://pscode.org/sscce.html

Here's another thought: can you write this code in C#? Can you show us
a working example?
 
O

Osiaq

Not really.  Skinning an entire GUI correctly can be a tough problem.


Hmm, OK.


That's more or less how I'd do it.


The same way I showed you, and I'd guess the same way you'd do it in C#:
use a reference to the parent "form" in the controller for the dialog.


Yes and no, depending on other requirements.  Do you have any code to
show us?  What images and where?  Can we get that SSCCE from you?

http://pscode.org/sscce.html

Here's another thought: can you write this code in C#?  Can you show us
a working example?

OHHHH YES I DID IT !

Finally JDialog was a correct way to do the trick. In the meantime I
started to like Java ;)
Still please remember this is my first Java code, so keep your
rofls ;)
Here's a call from the parent form:
---------------------------------------
mydialog m= new mydialog();
m.setModal(true);
m.setVisible(true);
int a=m.getResAction();
System.out.println(a);
---------------------------------------

And here's the child: (I hope it keeps formatting)


---------------------------------------
package wbsrvc;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class mydialog extends JDialog {

ImageIcon backIcon = new ImageIcon("/home/osiaq/Desktop/
background.png");
ImageIcon btYesIcon = new ImageIcon("/home/osiaq/Desktop/
btYesBack.png");
ImageIcon btCancelIcon = new ImageIcon("/home/osiaq/Desktop/
btCancelBack.png");
private int resAction = -1;

public int getResAction() {
return resAction;
}

public void setResAction(int resAction) {
this.resAction = resAction;
}

public mydialog() {

this.setBounds(1, 1, 432, 250);
this.setSize(new Dimension(432, 250));
this.isModal();
this.setLayout(new BorderLayout());
this.setUndecorated(true);

JButton btYes = new JButton(btYesIcon);
JButton btCancel = new JButton(btCancelIcon);
JLabel label = new JLabel(backIcon);

label.setSize(new Dimension(200, 200));
btYes.setSize(new
Dimension(btYesIcon.getImage().getWidth(null),
btYesIcon.getImage().getHeight(null)));
btCancel.setSize(new
Dimension(btCancelIcon.getImage().getWidth(null),
btCancelIcon.getImage().getHeight(null)));

btYes.setLocation(50, 200);
btCancel.setLocation(250, 200);

btYes.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
btYesActionPerformed(evt);
}
});

btCancel.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
btCancelActionPerformed(evt);
}
});

btYes.setBorder(BorderFactory.createEmptyBorder());
btCancel.setBorder(BorderFactory.createEmptyBorder());

this.add(btYes);
this.add(btCancel);
this.add(label);
}

private void btYesActionPerformed(java.awt.event.ActionEvent evt)
{
setResAction(1);
dispose();
}

private void btCancelActionPerformed(java.awt.event.ActionEvent
evt) {
setResAction(0);
dispose();
}
}
 
E

Eric Sosman

[...]
I have to implement popup window with predefined background and nice
buttons (OK and Cancel) designed by our design team.
I cannot use 80-ish default style of the buttons, they have nice, blue
png skins.
[...]
Eric: is it really as easy as set the background image of JOptionPane,
skin the buttons with png (and place them in correct locations) and
fit it in reasonable amount of the code?

I've never tried anything fancy with JOptionPane or even with
JDialog, but a good place to get started would be the Java Tutorial

http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html

Be warned, though: GUI's implemented in Java/Swing try to adapt
to the "look and feel" of the native windowing system, and thus will
look different on different platforms. If you absolutely need buttons
that look like Klingon hieroglyphs no matter when or where displayed,
and that use a color scheme unrelated to the user's own preference
settings, you may indeed need to roll your own -- maybe from The Word
Go, or maybe from some Word only a little further down the sentence.
(If it looks like huge amounts of work were needed, you should warn
the people setting the requirements: "Is having an Atari-style UI on
a Mac important enough to justify paying for all the time and effort
I and the testers will need to expend on it?" Maybe they'll still say
"Yes," but at least they'll say it with awareness of the expense. As
with many other things, going against the flow is harder and costlier.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top