Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get reference for JFrame

1,030 views
Skip to first unread message

Coolm@x

unread,
Apr 9, 2009, 3:46:03 AM4/9/09
to
Hello,
I want to ask for an example. I want to run JDialog from JMenuItem. I
have JFrame in one file and JDialog in another. As far as I know I must
pass frame-parent as argument to JDialog. Examples which I found were
similar to:

JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame (...)

Code is in one class, so there is no problem. I don't know how to pass
argument in this:

Main.java:
(...)
public static void main(String[] args) {
Main ste = new Main();
ste.setVisible(true); }

public Main() {
super();
initialize(); }

private void initialize() {
this.setSize(700, 500);
this.setJMenuBar(pasekMenu());
this.setContentPane(oknoGlowne()); }
(...)
private JMenuItem getHelp() {
if (HelpAbout == null) {
HelpAbout = new JMenuItem();
HelpAbout.setText("About");

HelpAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
???
}});
}
return HelpAbout; }
(...)


JDabout.java:

public JDabout(Frame owner) {
super(owner);
initialize();
}

Is there any method to get jFrame parent? Also, is this code correct:
JDabout about = new JDabout(?);
--
Best regards, Matthew
[ e-mail: matp dot sa a-t gmail dot com ]
[ JID 1: coo...@jabberpl.org ]
[ 2: / same as e-mail / ]

Michael Rauscher

unread,
Apr 9, 2009, 7:05:53 AM4/9/09
to
Coolm@x wrote:
> Hello,
> I want to ask for an example. I want to run JDialog from JMenuItem. I
> have JFrame in one file and JDialog in another. As far as I know I must
> pass frame-parent as argument to JDialog. Examples which I found were
> similar to:
>
> JFrame frame = new JFrame();
> JDialog dialog = new JDialog(frame (...)
>
> Code is in one class, so there is no problem. I don't know how to pass
> argument in this:

...

> Is there any method to get jFrame parent? Also, is this code correct:
> JDabout about = new JDabout(?);

If you're able to obtain the component where the ActionEvent originated
from, you can obtain the frame where the component lives in, too.

class Util {
// ...
public static Frame getFrameAncestor(Component c) {
Frame frame = null;
Window w = SwingUtilities.getWindowAncestor(c);
if ( w instanceof Frame )
frame = (Frame) w;
return frame;
}
}

(Of course, you can rewrite the above method to be more Frame specific
by walking up the component hierarchy)

Fortunately, the ActionEvent provides it's source which often is-a
component. Therefore you can do something like this:

public void actionPerformed(ActionEvent e) {
Frame frame = null;
Object source = e.getSource();
if ( source != null && source instanceof Component ) {
frame = Util.getFrameAncestor((Component) source);
}

// it's perfectly legal to pass the null-reference to JDialog.
JDabout dlg = new JDabout(frame);
}

HTH
Michael

Lew

unread,
Apr 9, 2009, 8:43:30 AM4/9/09
to
Coolm@x wrote:
> Hello,
> I want to ask for an example. I want to run JDialog from JMenuItem. I
> have JFrame in one file and JDialog in another. As far as I know I must
> pass frame-parent as argument to JDialog. Examples which I found were
> similar to:
>
> JFrame frame = new JFrame();
> JDialog dialog = new JDialog(frame (...)
>
> Code is in one class, so there is no problem. I don't know how to pass
> argument in this:
>
> Main.java:
> (...)
> public static void main(String[] args) {
> Main ste = new Main();
> ste.setVisible(true); }
>
> public Main() {
> super();

Redundant.

> initialize(); }
> ...

> JDabout.java:
>
> public JDabout(Frame owner) {
> super(owner);
> initialize();
> }

Aside from your primary question, your code is not thread safe.

Wrap the construction and execution of your GUI in a call to
<http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)>

Read the Swing tutorial on java.sun.com.

--
Lew

Daniel Pitts

unread,
Apr 9, 2009, 11:45:39 AM4/9/09
to

the JFrame parameter is an optional parameter, meaning you can do:
new JDialog((JFrame)null);

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Kalessin

unread,
Apr 11, 2009, 5:08:48 AM4/11/09
to
I Personally prefer SwingUtilities.getRoot(Component)
so for a JMenuItem you would use;

++SNIP++
import java.awt.Component;
import java.awt.Desktop.Action;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Owner
*/

public class MyMenu extends JMenuBar{

public static void main(String args[]) {


JFrame frame = new JFrame();

MyMenu menu = new MyMenu();
frame.setJMenuBar(menu);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}


AbstractAction show_dialog_box = new AbstractAction("Show Dialog Box") {
public void actionPerformed(ActionEvent e) {
JComponent source = (JComponent)e.getSource();
Component root = SwingUtilities.getRoot(source);
JFrame frame = (JFrame)root;
// Substitute in JOptionPane, JDialog, or JFileDialog as needed
String name = JOptionPane.showInputDialog(root, "Please Enter Your
Name","Enter Name",JOptionPane.OK_CANCEL_OPTION);
if( name == null ) {
System.out.println("You Pressed Cancel!");
return;}
System.out.println("Your Name Is "+name);
}
};
JMenuItem menu = new JMenuItem(show_dialog_box);

public MyMenu() {
add(menu);
}
}
++SNIP++

you can if you wanted to, place complex objects on your JFrame object, and
use casting to cast it to your special JFrame class
(ie - Instead of
JFrame frame = (JFrame)root;
you would do
MyJFrame frame = (MyJFrame)root;
then
frame.someSpecialMethodIMade();
which would enable you to access all the methods on your own JFrame class,
rather than treating it like a standard JFrame)
, but i think it looks really messy :( i'm not sure what other people think
about how they pass references from one class to another..
i prefer to create a specific class for the express purpose of holding all
my model classes, and passing a reference to into the constructor of each of
my view classes... but i digress... for simple learning things it's probably
not required of you to separate your code like that.
Best Regards
James Gardner


SwingUtilties.getRoot(
"Daniel Pitts" <newsgroup....@virtualinfinity.net> wrote in message
news:49de18db$0$3632$7836...@newsrazor.net...

0 new messages