Swing, JFrame and JInternalFrame

C

chris.allport

Greetings, All-

I am having difficulty getting my application configured the way I
would like. This is a really standard thing, so I must be missing
something simple. I am hoping some of you might have a better idea
than I do about this.

The main application should appear in the main frame. I can make this
happen.

The problem is when I popup other windows. They either don't show up,
or they are behind the main window. I have read about mixing AWT and
Swing, but I thought I was using all Swing.

Also, I could not find any guidelines explaining when I should use
JInternalFrames versus JDialog, so I am just using internal frames.

Currently, I am attempting something similar to the following:


class MyApp extends JFrame {
MyApp() {
super("MyApp");

desktopPane = new JDesktopPane();

getContentPane().add(...);
}

...
}

To add a popup:

public class MyPopup extends JPanel
implements InternalFrameListener {

private JInternalFrame frame;
private JDesktopPane desktopPane;

public MyPopup(JDesktopPane dtPane) {

desktopPane = dtPane;

initComponents();
}

public void run(int width, int height) {
createAndShowGUI(width, height);
}


private void initComponents() {
// Add components to GUI
...
}


private void createAndShowGUI(int width,
int height) {

frame = new JInternalFrame("MyPopUp",
false,
false,
false,
false);

frame.setBackground(Color.gray);
this.setBackground(Color.gray);
frame.setSize(width, height);

//Create and set up the content pane.
this.setOpaque(true);
frame.setContentPane(this);
//frame.getContentPane().add(this); // didn't work either

//Display the window.
frame.setVisible(true);
desktopPane.add(frame);
frame.moveToFront();
}
}


The code instantiates this panel from a menu action listener as
follows:

private class MyPopupListener implements ActionListener {

public MyPopupListener() {
}

public void actionPerformed(ActionEvent e) {
MyPopup gui = new MyPopup(desktopPane);

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui.run(150, 220);
}
});

}
}




This never shows up. Any ideas?

Thanks in advance-

Chris
 
V

Vova Reznik

It is not possible even to compile.

But after some changes(fixes) it works fine.
What version of java do you use?

I have problem - with oldest version:
first JInternalFrame should be added to JDesktopPane and only after
setVisible(true)
 
C

chris.allport

My apologies. This is not my exact code, I simply extracted the
windowing stuff out of my application.

I am using Java 1.5(5.0).
 
V

Vova Reznik

I don't see any problem, check it out:

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

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class TestInnerFrame extends JFrame implements ActionListener {
private JDesktopPane desktop = new JDesktopPane();

private JToolBar bar = new JToolBar();

private JButton btnAdd = new JButton("Add");

public TestInnerFrame() {
getContentPane().add(desktop, BorderLayout.CENTER);

btnAdd.addActionListener(this);
bar.add(btnAdd);

getContentPane().add(bar, BorderLayout.NORTH);
}

public static void main(String[] args) {
JFrame f = new TestInnerFrame();
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setSize(300, 300);
f.show();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnAdd) {
addInnerFrame();
}
}

private void addInnerFrame() {
// JInternalFrame f = new JInternalFrame("MyPopup", false, false, false,
// false);
//
// f.setSize(150, 220);
// f.setVisible(true);
// f.setBackground(Color.GRAY);
// desktop.add(f);
// f.moveToFront();

final MyPopup gui = new MyPopup(desktop);

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui.run(150, 220);
}
});
}

public class MyPopup extends JPanel {

private JInternalFrame frame;

private JDesktopPane desktopPane;

public MyPopup(JDesktopPane dtPane) {

desktopPane = dtPane;

initComponents();
}

public void run(int width, int height) {
createAndShowGUI(width, height);
}

private void initComponents() {

}

private void createAndShowGUI(int width, int height) {

frame = new JInternalFrame("MyPopUp", false, false, false, false);

frame.setBackground(Color.gray);
this.setBackground(Color.gray);
frame.setSize(width, height);

//Create and set up the content pane.
this.setOpaque(true);
frame.setContentPane(this);
//frame.getContentPane().add(this); // didn't work either

//Display the window.
frame.setVisible(true);
desktopPane.add(frame);
frame.moveToFront();
}
}
}
 
C

chris.allport

Thanks for the help. I am working out of the office this week, but
will try this as soon as I return on Monday.
 
C

chris.allport

The example works as written, but when I modify it to suit my
application, it breaks. I think I know what I am doing wrong -- I am
trying to add content directly on the desktop. Is this not allowed?

Here are my changes:

Add:
private JLabel label = new JLabel("Label);
to the TestInnerFrame class.

Change TestInnerFrame() to the following:

public TestInnerFrame() {
getContentPane().add(desktop, BorderLayout.CENTER);

btnAdd.addActionListener(this);
bar.add(btnAdd);

getContentPane().add(bar, BorderLayout.NORTH);

getContentPane().add(label, java.awt.BorderLayout.CENTER); //
Can no longer see MyPopup's unless they are JDialogs
//desktop.add(label, java.awt.BorderLayout.CENTER); // Appears
to do nothing
//bar.add(label); // This works, but only puts the icon on the
bar
}


If I further change the example such that MyPopup is a JDialog (and
pass in "this" instead of "desktop"), it works as I describe.

Should I be pursuing the JDialog approach? If so, do I even need a
desktop? If I do not create the desktop, it appears as if I can just
work off of the content pane?

Thanks in advance once more!

Chris
 
A

Andrew Thompson

The example works as written, but when I modify it to suit my
application, it breaks.

That is probably because you have little idea of what
you are doing. Are you programming according to the
JavaDocs and layout tutorials, or relying on The Force?
..I think I know what I am doing wrong --

It's about time your stopped making up theories
and hit the tutorials.
..I am
trying to add content directly on the desktop. Is this not allowed?

Ask the JavaDocs - that is what they are for.
Here are my changes: ....
public TestInnerFrame() {
getContentPane().add(desktop, BorderLayout.CENTER); ....
getContentPane().add(label, java.awt.BorderLayout.CENTER); //

Anything appear familiar here?

The GUI Experts can be found on comp.lang.java.gui
The layout tutorials are here
<http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html>
And the JavaDocs (1.5 'no frames' entry point) are here.
<http://java.sun.com/j2se/1.5.0/docs/api/overview-summary.html>
 
C

chris.allport

Sorry to have offended you. I read many tutorials before turning to
this forum. All of this windowing stuff is extremely new to me, and I
wholly admit that I don't really know what I am doing. The tutorials
that I read explain things in a specific way and do not yield answers
to the questions that remain. The motivations for my questions is as
much to reinforce my understanding of what I am reading as much as to
fill the gaps in what I am not getting from the tutorials.

Thank you for the pointers you have provided, I will review them with
care.
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top