0

I want to make a Windows GUI simulation in Java, but I'm having trouble positioning windows over desktop icons. I tried placing the desktop icons (JButtons) and the windows (made using JInternalFrame) in the same container (JDesktopPane), but the windows push the icons away. If I place 2 containers (1 for desktop, 1 for windows) in a JLayeredPane, I'm guessing I won't be able to click the desktop icons?

Are there other ways to approach this?

Edit: More info:

I want to make a simulation of Windows explorer, with the desktop, desktop icons, and windows. I want the desktop (with icons) to be on a different layer as the windows. How do I do this? If I use JLayeredPane, then I can't click the icons because that layer is covered by the windows layer.

Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
  • what, where, how and why, for better help sooner post an SSCCE, short, runnable, compilable – mKorbel Nov 25 '13 at 19:01
  • I'd have to post my entire program. I just need something to point me in the right direction. I'll update the question with more info. – Leo Jiang Nov 25 '13 at 19:16

1 Answers1

1

You can add buttons to JDesktopPane just don't forget to specify their size and position, as JDesktopPane has no layout manager. Here is an example:

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.*;

public class TestInternalFrame {

    private static void createAndShowUI() {
        JDesktopPane pane = new JDesktopPane() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 200);
            }
        };
        pane.setBackground(Color.WHITE);

        addFrame(pane, new Point(50, 20), "Window1");
        addFrame(pane, new Point(60, 30), "Window2");
        addIcon(pane, new Point(5, 5), "Action", 
                UIManager.getIcon("OptionPane.informationIcon"));

        JFrame frame = new JFrame("Desktop");
        frame.add(pane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private static void addFrame(JDesktopPane pane, Point p, String title) {
        JInternalFrame frame = new JInternalFrame(title);
        frame.setSize(200, 100);
        frame.setLocation(p);
        frame.setResizable(true);
        frame.setMaximizable(true);
        frame.setIconifiable(true);
        frame.setClosable(true);
        frame.setVisible(true);
        pane.add(frame);
    }

    private static void addIcon(JDesktopPane pane, Point p, String text, Icon icon) {
        JButton button = new JButton(text, icon);
        button.setVerticalTextPosition(SwingConstants.BOTTOM);
        button.setHorizontalTextPosition(SwingConstants.CENTER);
        button.setBounds(new Rectangle(p, button.getPreferredSize()));
        pane.add(button);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    createAndShowUI();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
tenorsax
  • 21,123
  • 9
  • 60
  • 107