bc.ui.swing.lists.TransparentList Maven / Gradle / Ivy
/*
* The MIT License
*
* Copyright 2016 Benny Lutati.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package bc.ui.swing.lists;
import bc.ui.swing.models.GenericListModel;
import bc.ui.swing.visuals.Visual;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.event.ListSelectionListener;
/**
*
* @author bennyl
*/
public class TransparentList extends javax.swing.JPanel {
private Color foreColor = Color.BLACK;
/** Creates new form TransparentList */
public TransparentList() {
initComponents();
scroll.getViewport().setOpaque(false);
list.setCellRenderer(new DefaultListCellRenderer() {
{
setOpaque(false);
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JComponent ret = (JComponent) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
ret.setBorder(null);
return ret;
}
});
}
public void removeScroll(){
remove(scroll);
add(list, BorderLayout.CENTER);
}
public void addSelectionListner(ListSelectionListener listener) {
list.getSelectionModel().addListSelectionListener(listener);
}
public void setItems(LinkedList visuals) {
GenericListModel data = new GenericListModel();
data.setInnerList(visuals);
list.setModel(data);
}
public JList getList() {
return list;
}
public List getSelectedItems() {
LinkedList ret = new LinkedList();
for (Object l : list.getSelectedValues()) {
ret.add((Visual) l);
}
return ret;
}
public Color getForeColor() {
return foreColor;
}
public void setForeColor(Color foreColor) {
this.foreColor = foreColor;
list.setForeground(foreColor);
list.repaint();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
scroll = new javax.swing.JScrollPane();
list = new javax.swing.JList();
setOpaque(false);
setLayout(new java.awt.BorderLayout());
scroll.setBorder(null);
scroll.setOpaque(false);
list.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
list.setOpaque(false);
scroll.setViewportView(list);
add(scroll, java.awt.BorderLayout.CENTER);
}// //GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JList list;
private javax.swing.JScrollPane scroll;
// End of variables declaration//GEN-END:variables
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy