0
package javaapplication1;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        final NewJPanel p = new NewJPanel();
        frame.setTitle("Frame");
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        device.setFullScreenWindow(frame);
        device.setDisplayMode(new DisplayMode(800, 600, 32, 60));


        JButton btn = new JButton();
        btn.setText("Button");
        JPanel panel = new JPanel();

        panel.add(btn);
        frame.add(panel);

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame f = new JFrame();
                JPanel p = new JPanel();
                f.setSize(300, 300);
                p.setBackground(Color.red);

                f.add(p);
                f.setLocationRelativeTo(null);
                f.setAlwaysOnTop(true);
                f.setVisible(true);
            }
        });
    }
}

I wanted the f.setVisible(true); pops out inside the full screened JFrame which is set to be modal when I clicked the button. How can I do that? Because in that code, when I click the button, the f.setVisible(true); shows outside the full screen JFrame. Looking forward for your answers.

Jong
  • 131
  • 1
  • 2
  • 12

1 Answers1

3

So you want something like JInternalFrame inside of your main JFrame:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
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.SwingUtilities;

public class JavaApplication1 {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final JFrame frame = new JFrame();
                final JDesktopPane desktopPane = new JDesktopPane();
                frame.setTitle("Frame");
                frame.setSize(300, 300);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                final GraphicsDevice device = GraphicsEnvironment
                    .getLocalGraphicsEnvironment().getDefaultScreenDevice();
                device.setFullScreenWindow(frame);
                device.setDisplayMode(new DisplayMode(800, 600, 32, 60));

                JButton btn = new JButton();
                btn.setText("Button");
                JPanel panel = new JPanel();

                panel.add(btn);
                frame.add(panel, BorderLayout.NORTH);
                frame.add(desktopPane, BorderLayout.CENTER);

                btn.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JInternalFrame f = new JInternalFrame();
                        JPanel p = new JPanel();
                        p.setBackground(Color.red);
                        f.setSize(300, 300);
                        f.setResizable(true);
                        f.add(p);
                        f.setVisible(true);
                        desktopPane.add(f);
                    }
                });
            }
        });

    }
}

enter image description here

More about JInternalFrame you can find here

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
  • 1
    +1, output he can generate :-) i mean its not necessary to attach images – vels4j Dec 25 '12 at 19:14
  • Are the border layout required? Because in my application it has already fixed design and layout. – Jong Dec 25 '12 at 19:17
  • I just removed the border layout. The components are not showing. :( – Jong Dec 25 '12 at 19:25
  • 1
    You will have to add that `JDesktopPane` somewhere. – Branislav Lazic Dec 25 '12 at 19:28
  • Did you take a look the one I have commented in my other question? – Jong Dec 25 '12 at 19:32
  • 1
    Huh? Whats the problem? Btw...don't use "modal JFrame" as you stated in your last comment. If you want to display some kind of dialog INSIDE of your main frame, you can use `JOptionPane.showInternalMessageDialog()` as shown here: http://stackoverflow.com/questions/13859258/java-swing-internal-frame-as-dialog – Branislav Lazic Dec 25 '12 at 20:38