-1

I'm experimenting with desktop panes so I can use them in my work projects. The problem here is that I want to use an JInternalFrame within a JDesktopPane, in a normal JPanel it shows normally but cannot move it, using the desktop pane doesn't display any component added.

Here is the code of the last try, is simple just for learning how it works:

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

public class Internal_FrameShowtst extends JFrame{

    Internal_FrameShowtst(){
        BorderLayout bl = new BorderLayout();
        JDesktopPane p = new JDesktopPane();
        JPanel p1 = new JPanel();
        JButton b = new JButton("click");
        JInternalFrame in = new JInternalFrame("Test");
        Internal_Frametst ift = new Internal_Frametst();
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //p1.add(new JLabel("hola"));
                //in.add(p1);
                //in.setVisible(true);
                ift.setVisible(true);
            }
        });
        p1.add(b);
        bl.addLayoutComponent(p,BorderLayout.CENTER);
        //p.add(in);
        p.add(ift);
        p.repaint();
        setLayout(bl);
        add(p);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Internal_FrameShowtst().setVisible(true);
            }
        });
    }
}

custom internal frame class:

import javax.swing.*;

public class Internal_Frametst extends JInternalFrame {

    Internal_Frametst(){
        JPanel p = new JPanel();
        JLabel label = new JLabel("Halo");
        setIconifiable(true);
        //setIcon(true);
        setClosable(true);
        p.add(label);
        p.setSize(300,300);
        add(p);
        //setVisible(true);
    }
}

I've read and tried the following: Components inside JDesktopPane not showing JDesktopPane not displaying components when adding JInternalFrame

I've tried adding the components directly, adding a JPanel, adding the internal frame, trying without it, creating the internal frame in the main class, creating my own internal frame in its own class, using layout managers with both panels (normal and desktop), all with the same result.

  • 2
    Oracle has a helpful tutorial, [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the [How to Use Internal Frames](https://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html) section. – Gilbert Le Blanc Jan 27 '23 at 16:16
  • @GilbertLeBlanc I'm not using any GUI, writing all the code by myself in intellij. This is working for me, since the project is being updated once in a while and the GUI version needed to be graphically improved too. – Alfredo Aparicio Jan 27 '23 at 16:51
  • The code in your question is not a [mcve] since it is missing code for class `Internal_Frametst`. – Abra Jan 27 '23 at 16:56
  • 1
    *This is working for me,* - then why are you asking a question??? The code posted here is wrong. There is no need for a panel or layout managers or manually invoking repaint(). You add the desktop pane to the frame and the internal frame to the desktop pane. Read the tutorial and download the working examples to understand how to use a desktop pane and internal frames. Because a desktop pane doesn't use a layout manager (so you can drag internal frames) you need to set the size of the internal frames manually. – camickr Jan 27 '23 at 16:58
  • @Abra I didn't add the custom internal code because the problem is not about the internal frame. (Added in the edit) – Alfredo Aparicio Jan 27 '23 at 17:06
  • @camickr the "this is working for me" was about working without GUIs, the problem is that my JDesktopPane is not displaying any component I add to it. – Alfredo Aparicio Jan 27 '23 at 17:08
  • *my JDesktopPane is not displaying any component I add to it.* - I gave you the reason why. And you were given a link to the Swing tutorial with working examples, so I assume the problem is now solved? – camickr Jan 27 '23 at 20:15
  • *and look for working examples* - not sure what you mean by "look". The tutorial already contains working examples, so you don't have to look to far. – camickr Jan 29 '23 at 00:30
  • unrelated: stick to java naming conventions, please (no underscores for separating words) – kleopatra Jan 30 '23 at 16:57

1 Answers1

1

Your code creates several components that are never added to the visible UI at all. In the version you have posted, the internal frame is invisible and the button to make it visible is not part of the frame. But there are also problems with the initial sizes of the components.

I strongly recommend to keep the creation of a component, the setting of its initial properties, and the code to add it to a parent component close together in your source code.

Further, consider the points discussed in Prefer composition over inheritance? Your subclasses are entirely unnecessary.

Here is a revised version of your code that will open the internal frame when the button is clicked:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;

public class UiExample {
    public static void main(String[] args) {
        EventQueue.invokeLater(UiExample::initializeUI);
    }

    static void initializeUI() {
        JPanel p1 = new JPanel();
        JButton b = new JButton("Show Internal Frame");
        p1.add(b);

        JInternalFrame ift = initializeInternalFrame();
        b.addActionListener(e -> {
            ift.pack();
            ift.setVisible(true);
        });
        JDesktopPane p = new JDesktopPane();
        p.add(ift);

        JFrame mainFrame = new JFrame();
        mainFrame.setSize(300, 200);
        mainFrame.getContentPane().add(p, BorderLayout.CENTER);
        mainFrame.getContentPane().add(p1, BorderLayout.PAGE_START);
        mainFrame.setVisible(true);
    }

    static JInternalFrame initializeInternalFrame() {
        JInternalFrame iFrame = new JInternalFrame();
        iFrame.setIconifiable(true);
        // setIcon(true);
        iFrame.setClosable(true);
        iFrame.setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
        JPanel p = new JPanel();
        p.add(new JLabel("Hello"));
        iFrame.add(p);
        return iFrame;
    }
}

Note that setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE) is necessary for being able to show the frame again via setVisible(true) after the internal frame has been closed.

Holger
  • 285,553
  • 42
  • 434
  • 765