java sub windows

Started by
5 comments, last by Vyper_uk 18 years, 3 months ago
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.
Well I believe in God, and the only thing that scares me is Keyser Soze.
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.
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.
Well I believe in God, and the only thing that scares me is Keyser Soze.
I still need help on this....
Well I believe in God, and the only thing that scares me is Keyser Soze.
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
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 appletthis.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
Well I believe in God, and the only thing that scares me is Keyser Soze.
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);    }

This topic is closed to new replies.

Advertisement