I added a bachground color to a jpanel using

panel.setBackGround(Color.GRAY);

I want to add some color like "light blue." which do not have in the color list.

how can I do.

(2) how to add backgroud picture to jpanel????
(tried using setBackGround but failed!!!)


thankx in advance....

Dani AI

Generated

Quick corrections and practical options (short, concrete):

  • The failing call is just a typo/case problem: Java is case-sensitive. Use panel.setBackground(...) (lowercase "g"). That alone will fix the immediate error mentioned by .

  • "Light blue" isn't a named constant in Swing, so create it. Two simple ways:

Color light = Color.decode("#ADD8E6");
panel.setBackground(light);

or let the user pick one with a chooser:

Color picked = JColorChooser.showDialog(frame, "Pick color", panel.getBackground());
if (picked != null) panel.setBackground(picked);

Both are easier than hunting for a constant; 's JColorChooser tip is handy when you want the exact shade.

  • Background image approaches (choose one):

    1. Subclass and draw in paintComponent (good for dynamic/ tiled images) — remember to call super.paintComponent(g) and avoid re-scaling the image on every paint for performance (scale once and cache the scaled BufferedImage).

    2. Simpler: use a JLabel as the content pane for static images:

ImageIcon bg = new ImageIcon(getClass().getResource("/images/bg.png"));
JLabel background = new JLabel(bg);
background.setLayout(new BorderLayout());
background.add(yourPanel);   // yourPanel should be non-opaque if you want image to show through
frame.setContentPane(background);

This avoids custom painting and works well for form-like UIs.

Troubleshooting checklist: ensure resource paths are correct (getResource vs getResourceAsStream), handle null images/exceptions, call pack()/validate() after layout changes, and remember that setBackground only shows if the component is opaque. As demonstrated, painting works—these notes cover common pitfalls and easier alternatives.

Recommended Answers

All 2 Replies

(1)You can make you own Color Using Color Class Constructor with RGB Values. Search for Exact Color value you want
You can use new Color(100,149,237); or adjust these values as per your requirement

(2) You can use paint method to paint your image
maybe this can help

import java.awt.*;
import javax.swing.*;
class loginScreen extends JFrame
{
  public loginScreen()
  {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    ImagePanel panel = new ImagePanel("test.gif");
    panel.add(new JButton("OK"));
    getContentPane().add(panel);
    pack();
    setLocationRelativeTo(null);
  }
  class ImagePanel extends JPanel
  {
    Image img;
    public ImagePanel(String file)
    {
      setPreferredSize(new Dimension(600, 400));
      try
      {
        img = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource(file), file));
      }
      catch(Exception e){}//do nothing
    }
    public void paintComponent(Graphics g)
    {
      super.paintComponent(g);
      if(img != null) g.drawImage(img,0,0,getWidth(),getHeight(),this);
    }
  }
  public static void main(String[] args){new loginScreen().setVisible(true);}
}
commented: Helpful +25

You can use a JColorChooser to click the exact colour in the HSB tab or you can get the exact value in the RGB tab then specify the values of red,green and blue in your new colour as below;
Color lightblue = new Color(red,green,blue);
panel.setBackground(lightblue);

For the background you can try using look and feel coz it always works for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.