1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;
public class AlphaListenerScrollPanel extends JScrollPane{
ArrayList _list;
AlphaListenerScrollPanel _me;
AlphaPanel _alphapan;
AlphaListenerScrollPanel(String[] list)
{
super(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
_me = this;
this.setBounds(2, 25, 96,80);
_list = new ArrayList();
for (int i = 0; i< list.length; i++)
{
MyCheckBox jc = new MyCheckBox(list[i],true);
jc.addActionListener(new CheckListener());
_list.add(jc);
}
Collections.sort(_list);
_alphapan = new AlphaPanel(_list);
this.setWheelScrollingEnabled(true);
this.setViewportView(_alphapan);
this.revalidate();
}
class MyCheckBox extends JCheckBox implements Comparable
{
MyCheckBox(String val, boolean check)
{
super(val, check);
}
public int compareTo(Object o) {
return( this.getText().compareTo(((JCheckBox)o).getText()));
}
}
public void alphaScroll(String val)
{
System.out.println("auto scroll?");
for (int i = 0 ; i< _list.size(); i++)
{
String t = ((MyCheckBox)_list.get(i)).getText();
if (t.matches(val+".*"))
{
Rectangle rec = ((MyCheckBox)_list.get(i)).getBounds();
rec.height=rec.height+40;
rec.width = rec.width-20;
System.out.println("autoscrollmatch?"+ rec);
_alphapan.scrollRectToVisible(rec);
_me.revalidate();
return;
}
}
}
class CheckListener implements ActionListener
{
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
}
}
private class AlphaPanel extends JPanel
{
AlphaPanel(ArrayList list)
{
super();
this.setLayout(null);
this.setBackground(Color.BLUE);
int pos = 0;
for (int i =0 ; i< list.size() ; i++)
{
System.out.println("ajout" + _me.getWidth() );
((MyCheckBox)list.get(i)).setBounds(0,pos , _me.getWidth(), 20);
this.add(((MyCheckBox)list.get(i)));
pos = pos + 20;
}
System.out.println(this.getSize());
this.setBounds(0, 0, _me.getWidth(), pos);
this.setVisible(true);
_me.getViewport().updateUI();
}
}
public String[] getSelectedString()
{
int count = 0;
for (int i = 0 ; i< _list.size(); i++)
{
if (((MyCheckBox)_list.get(i)).isSelected()) count++;
}
String[] selection = new String[count];
count = 0;
for (int i = 0 ; i< _list.size(); i++)
{
if (((MyCheckBox)_list.get(i)).isSelected())
{
selection[count] = ((MyCheckBox)_list.get(i)).getText();
count++;
}
}
return selection;
}
public void selectAll()
{
for (int i=0; i< _list.size(); i++)
{
((MyCheckBox)_list.get(i)).setSelected(true);
}
}
public void selectNone()
{
for (int i=0; i< _list.size(); i++)
{
((MyCheckBox)_list.get(i)).setSelected(false);
}
}
public void updateList(String[] items)
{
System.out.println("bouh");
ArrayList oldlist = new ArrayList();
for (int i=0; i< _list.size(); i++)
{
oldlist.add(((MyCheckBox)_list.get(i)).getText());
}
for (int i = 0 ; i< items.length; i++)
{
if (oldlist.contains(items[i])) oldlist.remove(items[i]);
else
{
_list.add(new MyCheckBox(items[i],false));
}
}
if (!oldlist.isEmpty()) // some items are in the old list but not in the new
{
for (int i =0 ; i< oldlist.size(); i++)
{
for (int j =0 ; j< _list.size(); j++)
{
if (((MyCheckBox)_list.get(j)).getText().equals(items[i]))
_list.remove(j);
}
}
}
Collections.sort(_list);
}
/* public static void main(String[] args) {
String[] l= new String[]{"bi","be","bu"};
AlphaListenerScrollPanel al = new AlphaListenerScrollPanel(l);
System.out.println("op "+_list.size());
String[] l2= new String[]{"bi","be","toto","titi"};
al.updateList(l2);
System.out.println("op "+_list.size());
}*/
} |
Partager