I have a JFrame in which I have drown a background:
class ImagePanel extends JComponent {
private Image image;
public ImagePanel(Image image
you should create a simple List Cell Renderer and call it in your listName
.setCellRenderer(new YourListCellRenderer());
Eg:- Your List Cell Renderer Code should something like this
public class MyListCellRender extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list1, Object obj, int index, boolean isSelected, boolean isFocus) {
super.getListCellRendererComponent(list1, obj, index, isSelected, isFocus);
setForeground(Color.WHITE);
setOpaque(isSelected);
return this;
}
}
then in call it in what or wherever you want like form active event or initial stage in my case I callit in initial Stage which after form create.It mean after initComponent();
initComponents();
jList1.setUI(new WebListUI());
scrollBar.setOpaque(false);
scrollBar.getViewport().setOpaque(false);
jList1.setOpaque(false);
jList1.setCellRenderer(new TransparentListCellRender());
This is my Sample
You need to make the JList
, JScrollPane
AND the JScrollPane
's JViewport
all transparent, for example...
list.setOpaque(false);
JScrollPane sp = new JScrollPane(list);
sp.setOpaque(false);
sp.getViewport().setOpaque(false);
Oh, and one last trick, you need to replace the JList
's cell renderer with one which will make it transparent when it's not selected, for example
public class TransparentListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
setOpaque(isSelected);
return this;
}
}
package javaapplication1.pkg295;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());
DefaultListModel model = new DefaultListModel();
model.addElement("Apple");
model.addElement("Banana");
model.addElement("Pear");
model.addElement("Peach");
model.addElement("Grap");
JList list = new JList(model);
list.setCellRenderer(new TransparentListCellRenderer());
list.setOpaque(false);
JScrollPane sp = new JScrollPane(list);
sp.setOpaque(false);
sp.getViewport().setOpaque(false);
frame.add(sp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TransparentListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
setForeground(Color.WHITE);
setOpaque(isSelected);
return this;
}
}
public class TestPane extends JPanel {
private BufferedImage bgImg;
public TestPane() {
setLayout(new BorderLayout());
try {
bgImg = ImageIO.read(new File("C:\\Users\\shane\\Dropbox\\MegaTokyo\\Aqua\\200x200\\aria_manga_wallpaper-5541.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return bgImg == null ? new Dimension(200, 200) : new Dimension(bgImg.getWidth(), bgImg.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bgImg != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - bgImg.getWidth()) / 2;
int y = (getHeight() - bgImg.getHeight()) / 2;
g2d.drawImage(bgImg, x, y, this);
g2d.dispose();
}
}
}
}