user3320152
user3320152

Reputation: 113

On using Nimbus look and feel, JButton vanishes totally in a JFrame if there is a background image

I have a JFrame. In that i have used:

  1. JTabbed panes.
  2. JButton.
  3. background images in a Jlabel which is added in JPanel.
  4. Nimbus look and feel.

My problem is that whenever i use Nimbus look and feel the JButton vanishes only if there is a background image added. But this doesn't happen with other look and feel. Can anyone help me in getting the button visible?

Here is my code:

import javax.swing.ImageIcon;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame; 
import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;

import java.awt.*;
import java.awt.event.*;

class ImageTest 
{
JTabbedPane tp;
JLabel lab1;
JPanel  welcome;
JFrame frame;
ImageIcon image2;
JButton b1;
public void createUI()
{
    frame=new JFrame("JTabbedPane Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    b1=new JButton();
    welcome= new JPanel(null);
    welcome.setPreferredSize(new Dimension(1366,786));
    ImageIcon icon = new ImageIcon(ImageTest.class.getResource("icloud.jpg"));

    tp=new JTabbedPane();
            Container pane = frame.getContentPane();
    pane.add(tp);
    tp.addTab("Welcome", welcome);
            lab1=new JLabel();
    lab1.setIcon(icon);

    b1.setBounds(100,100,100,100);
    lab1.setBounds(0,0,1500,700);
            welcome.add(lab1);
            welcome.add(b1);
            b1.setVisible(true);
    frame.setSize(500,500);
    frame.setVisible(true);
    frame.setTitle("I-Cloud");

}   
public static void main(String[] args) 
{

      try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {

    }   
      ImageTest w = new ImageTest();
      w.createUI();   

     }
    }

EDIT:

 import javax.swing.ImageIcon;
 import javax.swing.JTabbedPane;
 import javax.swing.JTextField;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 import javax.swing.UnsupportedLookAndFeelException;
 import javax.swing.UIManager.LookAndFeelInfo;

 import java.awt.*;
 import java.awt.event.*;

 class ImageTest 
  {
 JTabbedPane tp;
 JLabel lab1;
 JPanel  welcome,w;
 JFrame frame;
 ImageIcon image2;
 JButton b1;

public void createUI()
{
    frame=new JFrame("JTabbedPane Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    b1=new JButton();
    welcome= new JPanel(new GridLayout(1,1,15,15));
    w=new JPanel (new GridLayout(1, 1, 15, 15));
    welcome.setPreferredSize(new Dimension(1366,786));

    ImageIcon icon = new ImageIcon(ImageTest.class.getResource("icloud.jpg"));

    tp=new JTabbedPane();
            Container pane = frame.getContentPane();
    pane.add(tp);


    lab1=new JLabel();
    lab1.setIcon(icon);
    w.setOpaque(false);
    w.add(b1);


    b1.setVisible(true);
            welcome.add(lab1);
            welcome.add(w);
            tp.addTab("Welcome", welcome);

    frame.setSize(500,500);
    frame.pack();
    frame.setVisible(true);
    frame.setTitle("I-Cloud");

}   
public static void main(String[] args) 
{
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {

    }

        ImageTest w = new ImageTest();
        w.createUI();   

    }
  }

Thanks.

Upvotes: 4

Views: 589

Answers (2)

sam
sam

Reputation: 83

I think setting a background image in JLabel and then adding buttons even after adding containers is a very complex issue. Believe me tried it and it was a very complex task. you can do it by directly drawing as Hovercraft suggested, but remember you would get not so much good image quality with that method. :-|

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your use of Swing GUI is out of kilter by use of null layouts and then adding one component on top of another, essentially covering up one of the components (the JButton) with another (the JLabel with the image).

  • Don't use null layout like you're doing. While to a newbie using null layout and setBounds seems the best way to create complex GUI's, the more you deal with Swing GUI creation, the more you will find that doing this will put your GUI in a straight-jacket, painting it in a very tight corner and making it very hard to extend or enhance. Just don't do this.
  • Don't add one component on top of another in the same container.
  • Instead give your JLabel with the image a layout manager.
  • Then add the JButton to the JLabel.
  • Then add the JLabel to the JTabbedPane.

Edit
Your latest edit did not use the JLabel as the container as suggested above. So if you use a separate container, then the button and the label will be shown side by side. To solve this, you either need to give the JLabel a layout manager, and then add the button to it as suggested above, or draw directly in a JPanel's paintComponent(...) method, and add the button to the JPanel.

For an example of the latter:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

public class MyImageTest extends JPanel {
   // public static final String SPEC = "https://duke.kenai.com/SunRIP/.Midsize/SunRIP.jpg.png";
   public static final String SPEC = "http://upload.wikimedia.org/wikipedia/commons/3/37/"
         + "Mandel_zoom_14_satellite_julia_island.jpg";
   private static final int PREF_H = 786;
   private static final int GAP = 100;
   private BufferedImage img;

   public MyImageTest() {
      try {
         URL imgUrl = new URL(SPEC);
         img = ImageIO.read(imgUrl);
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      setLayout(new FlowLayout(FlowLayout.LEADING));
      setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
      JButton b = new JButton();
      b.setPreferredSize(new Dimension(GAP, GAP));
      add(b);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (img != null) {
         g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (img == null) {
         return super.getPreferredSize();
      } else {
         int width = (img.getWidth() * PREF_H) / img.getHeight();
         return new Dimension(width, PREF_H);
         // return new Dimension(img.getWidth(), img.getHeight());
      }
   }

   private static void createAndShowGui() {
      try {
         for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
               UIManager.setLookAndFeel(info.getClassName());
               break;
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      MyImageTest mainPanel = new MyImageTest();

      JFrame frame = new JFrame("MyImageTest");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

enter image description here

Upvotes: 4

Related Questions