1

I want to know how to show an internal frame in swing. That means,when a JFrame is needed, normally what I do is,

new MyJFrame().setVisible(true);

Let's say the previous form should be displayed as well. And when this new frame is displayed,another new icon is displayed on the task bar.(it sounds like two separate applications run in one application) I want to avoid showing that icon and display both frames as they are in one application. Thank you

Nate W.
  • 9,141
  • 6
  • 43
  • 65
Débora
  • 5,816
  • 28
  • 99
  • 171
  • 3
    please post here short runnable code that shows non-asked question, and basic rulles for short runnable code here http://sscce.org/ – mKorbel Aug 31 '11 at 19:34
  • 3
    There are recent example [here](http://stackoverflow.com/questions/7229226/avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swing/7229662#7229662) and [here](http://stackoverflow.com/questions/7218971/java-method-works-on-windows-but-not-macintosh/7220544#7220544). – trashgod Aug 31 '11 at 19:41

2 Answers2

3

..want to avoid showing that icon and display both frames as they are in one application.

Another solution is to put the 2nd and subsequent free floating elements in a JDialog.

E.G. of using both a frame and dialog to hold extra content.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class FrameTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                initGui();
            }
        });
    }

    public static void initGui() {
        final JFrame f = new JFrame("Frame Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel gui = new JPanel(new GridLayout(0,1,5,5));
        final Content c = new Content();
        JButton frame = new JButton("Frame");
        frame.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                JFrame f2 = new JFrame("Content");
                f2.add(c.getContent());
                f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f2.pack();
                f2.setLocationByPlatform(true);
                f2.setVisible(true);
            }
        });
        gui.add(frame);

        JButton dialog = new JButton("Dialog");
        dialog.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                JDialog d = new JDialog(f);
                d.add(new Content().getContent());
                d.pack();
                d.setLocationByPlatform(true);
                d.setVisible(true);
            }
        });
        gui.add(dialog);

        f.add(gui);
        f.pack();
        f.setVisible(true);
    }
}

class Content {

    public Component getContent() {
        JPanel p = new JPanel();
        p.add(new JLabel("Hello World!"));
        return p;
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You're welcome. Glad I could provide an alternate strategy that suits the use-case. :-) – Andrew Thompson Sep 02 '11 at 03:59
  • I tried to use JDialog as following. Here frame is a JFrame and I used Netbeans and just created a seperate JFrame(right click -> new -> JFrame form.. also I put some buttons and textfields) and created an instance of that JFrame. Then put that instance into the JDialog. MyJFrame frame=new MyJFrame(); JDialog d=new JDialog(frame); d.setVisible(true); but the dialog was visible without the frame. I cannot see any button or textfield I put inside my JFrame. I referred [this](http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html) – Débora Sep 02 '11 at 05:13
  • Don't put the dialog into the frame. That won't work. Instead put the into the dialog - the components that you had put into the frame. – Andrew Thompson Sep 02 '11 at 05:17
  • I did as you mentioned. but the result is as for the JDialg. Dialog d=new Dialog(new frame()); //frame is a JFrame d.setVisible(true); Would you let me know any sample code working or any link please. – Débora Sep 02 '11 at 06:21
  • Thank you very much... Andrew.. Here is the point I got JDialog d = new JDialog(f); d.add(new Content().getContent()); // Here is the thing I missed. I put my Panel consting other components d.pack(); d.setLocationByPlatform(true); d.setVisible(true); It worked and my problem completly solved .. I appreciate your help .. EXCELLENT :) :) :) – Débora Sep 02 '11 at 07:17
  • only one more thing. I want to close the Dialog when a button in the JPanel is pressed. How I did it was, I used the single pattern and wrote a method to dispose the dialog in parent JFrame. Then from the JPanel, when the button is pressed, get an instance of Parent JFrame and then called the closing method. I dont know how far my solution is good, but if u suppose there is another best solution,please let me know. Once again I appreciate your help.Thanks – Débora Sep 02 '11 at 07:48
  • I am not one to answer questions on 'best programming styles' (it is outside my area of expertise). I suggest you ask a separate question, I'm sure there are many who would love to pick out the stylistic errors in my code. ;) – Andrew Thompson Sep 02 '11 at 08:59
2

You have one JFrame for an application.

You can display multiple JPanels within a JFrame.

Or, as trashgod pointed out, you can have multiple JInternalFrames within a JDesktopFrame.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Thank you Mr. Gilbert. but thing is, if I use JInternalFrames nad JDesktop pane, internal frame will be limited into to the applcation window(can move only inside the desktoppane :( ).Then application's looking may not be as I wished.But I thing JDialg is best for my problem. If you can,please let me know how to use a JDialog. – Débora Sep 02 '11 at 05:01
  • @Aash Maharoon: See Andrew Thompson's answer for a JDialog explanation. – Gilbert Le Blanc Sep 02 '11 at 18:36