0

Is there a way to make an image appear on either side of my desktop in java using JFrame?

I am having an JOptionPane.showInputDialog and an Image display on my desktop at the same time, but I don't want them to be on top of each other. I'd rather have one get displayed to the right and one displayed to the left so I don't have to always move the windows.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    A JOptionPane will either be centered on the frame or the desktop, depending on the parameters you use in the method. If you want full control over the location then you need to either: 1) Read the `JOptionPane` API where you will find a section on "Direct Use" which will give you access to the JDialog used by option pane so you can control the location, or 2) Just use a JDialog so you are in full control of the components and the location of the dialog. – camickr Dec 12 '21 at 23:49

1 Answers1

2

In a multi-screen environment, this can be a little more difficult, as you need to take into a lot of variables. You can think of a multi-screen environment as a "virtual screen", where the default screen may not be at 0x0. Also, add into the mix things like OS menu/task bars and you have another series of things to juggle.

The following example displays two frames, both aligned along the bottom of the "safe viewable area" of the "default" screen and positioned on the left and right side of the "safe viewable area" of the "default" screen

import java.awt.EventQueue;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Test {
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame happyFrame = new JFrame();
                    happyFrame.add(new HappyPane());
                    happyFrame.pack();

                    JFrame sadFrame = new JFrame();
                    sadFrame.add(new SadPane());
                    sadFrame.pack();

                    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                    GraphicsDevice screenDevice = ge.getDefaultScreenDevice();
                    GraphicsConfiguration gc = screenDevice.getDefaultConfiguration();
                    Rectangle bounds = screenDevice.getDefaultConfiguration().getBounds();

                    Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);

                    bounds.x += insets.left;
                    bounds.y += insets.top;
                    bounds.width -= insets.left + insets.right;
                    bounds.height -= insets.top + insets.bottom;

                    happyFrame.setLocation(bounds.x, (bounds.y + bounds.height) - happyFrame.getSize().height);
                    sadFrame.setLocation((bounds.x + bounds.width) - sadFrame.getSize().width, (bounds.y + bounds.height) - sadFrame.getSize().height);

                    happyFrame.setVisible(true);
                    sadFrame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class HappyPane extends JPanel {

        public HappyPane() throws IOException {
            setBorder(new EmptyBorder(16, 16, 16, 16));
            setLayout(new GridBagLayout());
            add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/images/happy.png")))));
        }

    }

    public class SadPane extends JPanel {

        public SadPane() throws IOException {
            setBorder(new EmptyBorder(16, 16, 16, 16));
            setLayout(new GridBagLayout());
            add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/images/sad.png")))));
        }

    }
}

You can also have a look at

for more information then you probably really wanted to know ;)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366