• Advertisement
Sign in to follow this  

java sub windows

This topic is 4434 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am about half-way through the Java API docs for using JDialog, and am still very confused. How could I make a JDialog, created/initialized from within a JApplet instance, that has, say, a single JLabel control that reads "Hello, Dialogs!" ? Here's my best shot:
class MyFirstDialog extends JApplet
{
    JDialog myDialog = new Dialog();
    JLabel lbl = new JLabel("Hello, Dialogs!");

    myDialog.add(lbl);
    this.add(myDialog);
}

I don't think this will work though. And what about controls, such as a JButton, that require listeners to process their events....would I have to create an ActionListener for both my JApplet and my JDialog? If so, where would I write the listener for the dialog...Icertainly don't think I could do it within the body definition of the JApplet.

Share this post


Link to post
Share on other sites
Advertisement
A dialog isn't a component that you add onto another component like an applet, it's a full top-level window like a JFrame. Take a look at the Java Tutorial's section on dialogs. A simple message dialog can be displayed in 1-2 lines of code using the utility class JOptionPane.

By the way, in general, I think it's a lot easier to learn how to use the Java libraries by reading through their section on the Java Tutorial rather than reading through the API documentation.

Share this post


Link to post
Share on other sites
Matei,

I guess I asked the wrong question...let me describe what my end objective is first.

I have an applet - an instance of JApplet - that has, say, a single tiny pushbutton. When that pushbutton is "clicked," I want a JDialog to pop up. A dialog with very complex controls such that JOptionPane could never suffice to represent it.

Yes, this example makes no sense, for why would you create a JDialog that is more complex in layout and design than the applet that called it? However, in reality, I have a very complex applet. [smiling]

I just need an example.


class Test01 extends JApplet implements ActionListener
{
JButton jb = new JButton("Push me and I will launch and JDialog!");
this.add(jb);
jb.addActionListener(this);

public void actionPerformed(ActionEvent ae)
{
int source = ae.getSource();

if(source == jb)
{
/*
Here is where I want the JDialog to be
"launched" from. Pretend that the JDialog
has, itself, a single JRadioButton that itself
performs some action when clicked.

How/where do I make both JDialog and its
one child control, the JRadioButton?
*/

}
}
}




And so you see I am not looking to use a JOptionPane here to construct a simple "Yes/No/Cancel" dialog. I need the dialog to have the capability as being every bit as complex as the main applet.

Share this post


Link to post
Share on other sites
I'm not quite sure what the problem is, you just make it as you would any other swing component, eg modifying your example:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class AppTest extends JApplet implements ActionListener
{
private JButton jb;

public AppTest() {
jb = new JButton("Push me and I will launch and JDialog!");
this.add(jb);
jb.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();

if(source == jb)
{
makeDialog();
}
}

public void makeDialog() {
System.out.println("foo");
JDialog g = new JDialog();
JButton button = new JButton("Press me");
button.addActionListener(new DialogListener(button));
g.add(button);
g.setVisible(true);
g.pack();
}

private class DialogListener implements ActionListener {
private JButton b;

public DialogListener(JButton b) {
this.b = b;
}

public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();

if(source == b) {
makeDialog();
}

}

}

}



This is probably not the best way to structure it, I would have the code that makes the JDialog in another class but it gives you the idea

Share this post


Link to post
Share on other sites
Vyper_uk,

Your suggestion worked - thank you! I am now beginning to understanding how dialogs work with their respective applets.

However I am having another predicatble problem now.

I am writing a simple application, and because of external factors, I specifically do not want to use a Layout Manager of any nature. I want to painstakingly-define where the all dialog controls get placed inside the dialog. And as you probably guessed, I am coming up a cropper with every try.

In my main applet class, here is how I would define, say, a JButton, and place it "by hand" without a layout manager:


// inside applet

this.setLayout(null);

JButton jb = new JButton("To heck with layout managers.");
this.add(jb);
jb.addActionListener(this);

Dimension temp = jb.getPreferredSize();
jb.setBound(145, 243, temp.width, temp.height);



This code example works perfectly fine for my JApplet controls, but when I got to define the dialog controls in your makeDialog function, they don't work - they're being ignored completely.

Your code from your last post with my example interwoven:

public void makeDialog()
{
JDialog g = new JDialog();
g.setLayout(null);

JButton button = new JButton("Press me");
button.addActionListener(new DialogListener(button));

Dimension temp = button.getPreferredSize();
button.setBound(145, 243, temp.width, temp.height);

g.add(button);
g.setVisible(true);
g.pack();
}



I suspect the pack() method is interfering, but I'm not quite sure how. Any clues, anybody?

hisDudeness

Share this post


Link to post
Share on other sites
Is that your actual code, because the method is setBounds() rather than setBound(), this could be your problem?

Anyway you are right, just get rid of the pack() to stop the frame sizeing itself and add a setSize() to do it manually and it all works

    public void makeDialog() {
JDialog g = new JDialog();
g.getContentPane().setLayout(null);

JButton button = new JButton("Press me");
button.addActionListener(new DialogListener(button));

Dimension temp = button.getPreferredSize();
g.getContentPane().add(button);
button.setBounds(145, 243, temp.width, temp.height);

g.setSize(new Dimension(400, 400));
g.setVisible(true);

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement