Setting the parent frame of a JFrame


H

hust6

Is it possible to set the parent compenent of a JFrame to another
JFrame? Here is a sample of my code:

JScrollPane scrollPane = new JScrollPane()

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Title");
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setSize(wid, hgt);
frame.setVisible(true);
frame.toFront();

This is how I want the frame to be, but I can't set the parent frame.
I tried adding:

JOptionPane.showMessageDialog(MyParentJFrame,frame.getContentPane());

as the last line. This set the parent component to the other JFrame,
but I lost the ability to have my message be scrollable.

Any help would be greatly appreciate.

Thanks!
 
Ad

Advertisements

H

hust6

The new frame is an error message as a result of something that
happened in the parent frame, so we would like them to be "tied"
together.
 
V

Vova Reznik

hust6 said:
The new frame is an error message as a result of something that
happened in the parent frame, so we would like them to be "tied"
together.
Use JDialog
 
A

Andrew Thompson

The new frame is an error message as a result of something that
happened in the parent frame, so we would like them to be "tied"
together.

Why not make 'the new frame' a (J)Dialog instead?

It can accept a parent, and also specify modality.
 
T

Thomas Fritsch

hust6 said:
The new frame is an error message as a result of something that
happened in the parent frame, so we would like them to be "tied"
together.
A Frame (or JFrame) has *no* parent, by definition. What you describe,
is called Window, which *must* always have a parent. You probably want
one of its subclasses (JWindow, JDialog).
However, the most convenient way for popping up a simple error message
is JOptionPane.showMessageDialog, which creates a modal JDialog
(including OK-button, error icon) with minimum effort on your side.
 
Ad

Advertisements

T

Thomas Fritsch

hust6 said:
Is it possible to set the parent compenent of a JFrame to another
JFrame? Here is a sample of my code:

JScrollPane scrollPane = new JScrollPane()

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Title");
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setSize(wid, hgt);
frame.setVisible(true);
frame.toFront();

This is how I want the frame to be, but I can't set the parent frame.
I tried adding:

JOptionPane.showMessageDialog(MyParentJFrame,frame.getContentPane());

as the last line. This set the parent component to the other JFrame,
but I lost the ability to have my message be scrollable.
Try
JOptionPane.showMessageDialog(MyParentJFrame,scrollPane);
May be this makes the message scrollable.
 
Ad

Advertisements

Joined
Sep 29, 2015
Messages
1
Reaction score
0
Hi, I had the same problem and this worked: setDefaultCloseOperation(HIDE_ON_CLOSE); (On the "child" JFrame )
I hope it would be usefull.
 

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

Similar Threads


Top