How to swap the JOptionPane background with an image?

2

Can you use this " ImageIcon " as the background for JOptionPane ?

Here is the code I tried:

public static void main(String args []){

  UIManager UI = new UIManager();
  ImageIcon icon = new ImageIcon("Imagens/Imagem.jpg"); //Declara Icon como Imagem

  UI.put("OptionPane.messageFont", new Font("Times New Roman", Font.PLAIN, 18));
  UI.put("OptionPane.messageForeground", Color.white);
  UI.put("OptionPane.background", Color.black);  
  UI.put("Panel.background", Color.black);
    
asked by anonymous 21.04.2017 / 01:23

1 answer

4

I do not think it's possible. JOptionPane is already a basic implementation of a modal notice window. If you want something more personalized, the best way is to build your own component.

Changing by Look and feel (LAF) settings is much more complex, because in addition to only changing when the LAF nimbus is applied, if you move to another LAF, all these changes will be ignored. As an alternative to this, I built this JDialog , which works as a JOptionpane flat but customizable, relevant excerpts are commented:

class JOptionBackgroundPane extends JDialog {

    BufferedImage imgBackground;
    JLabel label;

    public JOptionBackgroundPane(BufferedImage imgBackgound, String message) {
        this.imgBackground = imgBackgound;
        makeUI(message);
    }

    private void makeUI(String message) {

        JPanel backgroundPane = new JPanel() {

            @Override
            public void paintComponent(Graphics g) {

                super.paintComponents(g);
                //desenha a imagem no componente como fundo
                g.drawImage(imgBackground, 0, 0, this);
            }
        };

        label = new JLabel(message);

        //mude para cor de primeiro plano que quiser
        label.setForeground(Color.BLACK);
        //mude para o formato de fonte que quiser
        label.setFont(new Font("Times New Roman", Font.PLAIN, 18));

        label.setAlignmentX(Component.CENTER_ALIGNMENT);

        JButton btn = new JButton("OK");

        btn.setAlignmentX(Component.CENTER_ALIGNMENT);

        btn.addActionListener(e -> {
            dispose();
        });

        backgroundPane.setLayout(new BoxLayout(backgroundPane, BoxLayout.Y_AXIS));
        //workaround para evitar que o label fique colado no topo
        backgroundPane.add(Box.createRigidArea(new Dimension(0, Math.round(getPreferredSize().height*0.1f))));
        backgroundPane.add(label);
        backgroundPane.add(Box.createVerticalGlue());
        backgroundPane.add(btn);
        //workaround para evitar que o label fique colado na parte de baixo
        backgroundPane.add(Box.createRigidArea(new Dimension(0, Math.round(getPreferredSize().height*0.1f))));

        setModal(true);
        setResizable(false);
        setContentPane(backgroundPane);
        pack();
        setLocationRelativeTo(null);
    }

    @Override
    public Dimension getPreferredSize() {
        //força o container a ter sempre o mesmo tamanho da imagem
        return new Dimension(imgBackground.getWidth(), imgBackground.getHeight());
    }
}

To use, just pass an image through a BufferredImage , something like this:

BufferedImage image = ImageIO.read(SuaClasse.class.getResource("path da imagem"));

JOptionBackgroundPane pane = new JOptionBackgroundPane(image, "mensagem");
pane.setVisible(true);

You can see it working through the example below:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JOptionPaneBackground {
    public static void main(String[] args) throws Exception {

        BufferedImage image = ImageIO.read(
                new URL("https://www.walldevil.com/wallpapers/a64/thumb/background-wallpapers-blue-opera-light-images.jpg"));

        JOptionBackgroundPane pane = new JOptionBackgroundPane(image, "Teste");
        pane.setVisible(true);

    }
}

class JOptionBackgroundPane extends JDialog {

    BufferedImage imgBackground;
    JLabel label;

    public JOptionBackgroundPane(BufferedImage imgBackgound, String message) {
        this.imgBackground = imgBackgound;
        makeUI(message);
    }

    private void makeUI(String message) {

        JPanel backgroundPane = new JPanel() {

            @Override
            public void paintComponent(Graphics g) {

                super.paintComponents(g);
                //desenha a imagem no componente como fundo
                g.drawImage(imgBackground, 0, 0, this);
            }
        };

        label = new JLabel(message);

        //mude para cor de primeiro plano que quiser
        label.setForeground(Color.BLACK);
        //mude para o formato de fonte que quiser
        label.setFont(new Font("Times New Roman", Font.PLAIN, 18));

        label.setAlignmentX(Component.CENTER_ALIGNMENT);

        JButton btn = new JButton("OK");

        btn.setAlignmentX(Component.CENTER_ALIGNMENT);

        btn.addActionListener(e -> {
            dispose();
        });

        backgroundPane.setLayout(new BoxLayout(backgroundPane, BoxLayout.Y_AXIS));
        //workaround para evitar que o label fique colado no topo
        backgroundPane.add(Box.createRigidArea(new Dimension(0, Math.round(getPreferredSize().height*0.1f))));
        backgroundPane.add(label);
        backgroundPane.add(Box.createVerticalGlue());
        backgroundPane.add(btn);
        //workaround para evitar que o label fique colado na parte de baixo
        backgroundPane.add(Box.createRigidArea(new Dimension(0, Math.round(getPreferredSize().height*0.1f))));

        setModal(true);
        setResizable(false);
        setContentPane(backgroundPane);
        pack();
        setLocationRelativeTo(null);
    }

    @Override
    public Dimension getPreferredSize() {
        //força o container a ter sempre o mesmo tamanho da imagem
        return new Dimension(imgBackground.getWidth(), imgBackground.getHeight());
    }
}
    
21.04.2017 / 03:16