0

I am adding an icon to a JOptionPane which also has a JPanel added. When the default cup of coffee icon is there, the JPanel is fully visible. Replacing that with my own icon pushes the JPanel over to the right so far some fields are not visible. I have tried scaling the icon but whether I scale it by 1/2, 1/3, 1/5, or 1/7, though the image itself changes in size, the amount of room the JOptionPane allocates for it remains uneffected pushing my poor JPanel out into the cold. Before I give up and write a custom dialog, I am curious: Am I overlooking some setting to have my JPanel begin where the width of my icon ends?

public void displayDialog(){
    Image img = icon.getImage();
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics g = bi.createGraphics();
    g.drawImage(img, 0, 0, img.getWidth(null)/7, img.getHeight(null)/7, null);
    ImageIcon newIcon = new ImageIcon(bi);

    Object[] options = {"Cancel", "Done"};// Cancel returns 0; Done returns 1
    int n = JOptionPane.showOptionDialog(null, mainPanel,"New Certification Information",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

    if(n == 1){
        //done option:  expand later
    }
}

BTW: Don't recall asking a question here before and don't know when I will again so, while I'm here....I use this site quite often. You all have been a great help to me and I am very grateful.

Beircheart
  • 11
  • 1
  • 1
    `BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);` is going to create an icon of the same size as the original.... – MadProgrammer Feb 17 '16 at 02:13
  • *"I have tried scaling the icon but whether I scale it by 1/2, 1/3, 1/5, or 1/7, though the image itself changes in size, the amount of room the JOptionPane allocates for it remains uneffected"* That is because the code snippet is not scaling the size of `bi` in the same way it is scaling the drawing of `img` when it is drawn. On other matters: 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Feb 17 '16 at 03:41
  • Thanks MadProgrammer and Andrew Thompson. Your observations were spot on! I scaled BOTH places where I called img.getWidth(null) and img.getHeight(null) and the resulting icon rested beautifully in my JOptionPane. BufferedImage bi = new BufferedImage(img.getWidth(null)/7, img.getHeight(null)/7, BufferedImage.TYPE_INT_ARGB); Graphics g = bi.createGraphics(); g.drawImage(img, 0, 0, img.getWidth(null)/7, img.getHeight(null)/7, null); ImageIcon newIcon = new ImageIcon(bi); – Beircheart Feb 17 '16 at 22:50
  • 1) *"Thanks MadProgrammer and Andrew Thompson."* Tip: Add @MadProgrammer (or whoever, the `@` is important) to *notify* the person of a new comment. 2) Glad you worked it out. :) Now you might either delete the question, or report the solution in an answer. – Andrew Thompson Feb 18 '16 at 04:33

0 Answers0