import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
@SuppressWarnings({"serial", "rawtypes", "unchecked"})
public class ListPanel extends JFrame
{
public JList KingdomList;
public JScrollPane scrollPane;
public Image image;
public ImageIcon m_image;
public int winc = 0;
public int hinc = 0;
public ListPanel()
{
setUndecorated(true);
setBackground(new Color(0.0f, 0.0f, 0.0f, 0.0f));
getContentPane().setLayout(null);
((JPanel) getContentPane()).setOpaque(false);
image = Toolkit.getDefaultToolkit().createImage("Pictures/Screens/Screen_Setup.PNG");
m_image = new ImageIcon(image);
JPanel backPanel = new JPanel();
backPanel.setOpaque(false);
Object[] items = new CheckListItem[]
{
new CheckListItem("One"),
new CheckListItem("Two"),
new CheckListItem("Three"),
new CheckListItem("Four"),
new CheckListItem("Five"),
new CheckListItem("Six"),
new CheckListItem("Seven"),
new CheckListItem("Eight"),
new CheckListItem("Nine"),
new CheckListItem("Ten"),
new CheckListItem("Eleven"),
new CheckListItem("Twelve"),
new CheckListItem("Thirteen"),
new CheckListItem("Fourteen"),
new CheckListItem("Fifteen"),
new CheckListItem("Sixteen"),
new CheckListItem("Seventeen"),
new CheckListItem("Eighteen"),
new CheckListItem("Nineteen"),
new CheckListItem("Twenty"),
};
KingdomList = new JList(items);
KingdomList.setOpaque(false);
KingdomList.setBackground(new Color(1.0f, 1.0f, 1.0f, 1.0f));
KingdomList.setFont(new Font("Arial", Font.BOLD, 16));
KingdomList.setCellRenderer(new CheckBoxListRenderer());
KingdomList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
KingdomList.setVisibleRowCount(5);
KingdomList.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent event)
{
selectItem(event.getPoint());
}
});
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
Object mapKey = keyStroke.toString();
KingdomList.getInputMap().put(keyStroke, mapKey);
KingdomList.getActionMap().put(mapKey, new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
toggleSelectedItem();
}
});
scrollPane = new JScrollPane(KingdomList);
scrollPane.setSize(new Dimension(451, 301));
scrollPane.setLocation(51, 230);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
getContentPane().add(scrollPane);
backPanel= new JPanel()
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
winc = m_image.getIconWidth();
hinc = m_image.getIconHeight();
int w = getParent().getWidth();
int h = getParent().getHeight();
for (int i=0;i<h+hinc;i=i+hinc)
{
for (int j=0;j<w+winc;j=j+winc)
{
m_image.paintIcon(this,g,j,i);
}
}
}
};
getLayeredPane().add( backPanel, new Integer( Integer.MIN_VALUE ) );
backPanel.setBounds(0, 0, 500, 500);
backPanel.setSize(855, 640);
setVisible(true);
}
public static void main(String[] args)
{
ListPanel w = new ListPanel();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
w.setBounds(dim.width/2-427, dim.height/2-320, 855, 640);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void selectItem(Point point)
{
int index = KingdomList.locationToIndex(point);
if (index >= 0)
{
CheckListItem item = (CheckListItem)KingdomList.getModel().getElementAt(index);
item.setSelected(!item.isSelected());
KingdomList.repaint(KingdomList.getCellBounds(index, index));
}
}
private void toggleSelectedItem()
{
int index = KingdomList.getSelectedIndex();
if (index >= 0)
{
CheckListItem item = (CheckListItem)KingdomList.getModel().getElementAt(index);
item.setSelected(!item.isSelected());
KingdomList.repaint(KingdomList.getCellBounds(index, index));
}
}
}
----
class CheckListItem
{
private Object item;
private boolean selected;
public CheckListItem(Object item)
{
this.item = item;
}
public Object getItem()
{
return item;
}
public boolean isSelected()
{
return selected;
}
public void setSelected(boolean isSelected)
{
this.selected = isSelected;
}
@Override
public String toString()
{
return item.toString();
}
}
----
import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
@SuppressWarnings({ "rawtypes", "serial" })
class CheckBoxListRenderer extends JCheckBox implements ListCellRenderer
{
public Component getListCellRendererComponent(JList comp, Object value, int index, boolean isSelected, boolean hasFocus)
{
setEnabled(comp.isEnabled());
setSelected(((CheckListItem) value).isSelected());
setFont(comp.getFont());
setText(value.toString());
if (isSelected)
{
setBackground(comp.getSelectionBackground());
setForeground(comp.getSelectionForeground());
}
else
{
setBackground(comp.getBackground());
setForeground(comp.getForeground());
}
return this;
}
}