1

I am trying to add background image in jTable but when I run code nothing shown. only white color and no jTable shown and my java Application hangs also. is there any thing wrong in this code. here is the preview also.

enter image description here


import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;


public class Main extends JFrame
{
  public Main() {
  JTable table = new JTable(100, 5) {
     public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        if (c instanceof JComponent) {
            ((JComponent) c).setOpaque(false);
        }
        return c;
     }
  };

  ImageJScrollPane isp = new ImageJScrollPane(table);

  isp.setBackgroundImage(new ImageIcon("/Images/user/lockscreen.png"));

  getContentPane().add(isp);

  addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent we) {
        System.exit(0);
     }
  });
 }

 public static void main(String [] args) {
  Main main = new Main();
  main.setSize(400, 400);
  main.setVisible(true);
  } 
 } 

 class ImageJScrollPane extends JScrollPane 
{
  private ImageIcon image = null;

  public ImageJScrollPane() {
  this(null, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
  }

  public ImageJScrollPane(Component view) {
  this(view, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
  }

  public ImageJScrollPane(Component view, int vsbPolicy, int hsbPolicy) {
  super(view, vsbPolicy, hsbPolicy);
  if (view instanceof JComponent) {
     ((JComponent) view).setOpaque(false);
   }
   }

  public ImageJScrollPane(int vsbPolicy, int hsbPolicy) {
  this(null, vsbPolicy, hsbPolicy);
  }

  public void setBackgroundImage(ImageIcon image) {
  this.image = image;
  }

  public void paint(Graphics g) {
  // Do not use cached image for scrolling
   getViewport().setBackingStoreEnabled(true);

    if (image != null) {
     Rectangle rect = getViewport().getViewRect();
     for (int x=0; x<rect.width; x+=image.getIconWidth()) {
     for (int y=0; y<rect.height; y+=image.getIconHeight()) {
           g.drawImage(image.getImage(), x, y, null, null); 
        }
      }

     super.paint(g);
    }
   }
}
fzprog
  • 69
  • 1
  • 14

2 Answers2

3

Here is one way:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        setLayout( new BorderLayout() );

        JTable table = new JTable(5, 5);
        table.setOpaque( false );
        DefaultTableCellRenderer renderer =
            (DefaultTableCellRenderer)table.getDefaultRenderer(Object.class);
        renderer.setOpaque(false);

        JScrollPane scrollPane = new JScrollPane( table );
        scrollPane.setOpaque( false );
        scrollPane.getViewport().setOpaque( false );

        final ImageIcon icon = new ImageIcon("mong.jpg");

        JPanel background = new JPanel( new BorderLayout() )
        {
            @Override
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);

                g.drawImage(icon.getImage(), 0, 0, getWidth(), getHeight(), this);
            }
        };
        background.add( scrollPane );
        add(background);
    }

    private static void createAndShowGUI()
    {
        JPanel panel = new JPanel();

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

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • I am using this in netbeans it works but only with full image path `final ImageIcon icon = new ImageIcon("C:/Users/aaaa/Documents/NetBeansProjects/appli/src/Images/metal_texturenna.png");` kindly explain why this happens @camickr – fzprog May 22 '15 at 05:55
  • @fzprog, Glad it helped don't forget to "accept" the answer so people know the problem has been solved. Reading an image is a different question. I don't use an IDE so I don't know how you have it configured. There are many ways to read an image, I just gave a simple example. Place the image in the same directory as the class file. Read the Swing tutorial on [How to Use Icons](http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html) for a more examples of reading images. – camickr May 22 '15 at 14:41
1

Make sure the /Images/user/lockscreen.png is in classpath, and also you are using absolute path '/' instead use relative path like ../ or ./ (or) make sure the absolute path is valid.

K139
  • 3,654
  • 13
  • 17