|
import
java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//
The following package needed for ListSelectionListener
// interface and ListSelectionEvent names.
import javax.swing.event.*;
public
class ListDemo extends JApplet
implements ListSelectionListener
{
JRadioButton red,green,blue;
OutputPanel outputPanel;
public void init() {
// Create a JPanel and then create three
// a JList with several color choices and
// and add it to the control panel.
JPanel p = new JPanel();
String [] colors = {
"Red","Green","Blue","Yellow","White","Black"};
JList colorList = new JList(colors);
// Show only 4 items in the list at a
time.
colorList.setVisibleRowCount(4);
// Allow only one of the items to be selected.
colorList.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
//
Select initially the top item. colorList.setSelectedIndex(0);
colorList.addListSelectionListener(this);
// Add to a JScrollPane so that we can
have
// a scroller to view other items.
JScrollPane scrollPane
= new JScrollPane(colorList);
p.add(scrollPane);
getContentPane().add(p, "South");
outputPanel = new OutputPanel();
// Initialize panel
to red.
outputPanel.setColor(0xFF,0,0);
getContentPane().add(outputPanel, "Center");
}
// This class implements the ListSelectionListener
// so events come to this valueChanged method.
public void valueChanged(ListSelectionEvent
evt) {
// Default color component values.
int cr=0;
int cg=0;
int cb=0;
// Get the reference to the JList object
JList source = (JList)evt.getSource();
// and get an array
of the selected items.
Object [] values =
source.getSelectedValues();
// In this case only one value can be selected
// so just look at first item in array.
String colorSelected = (String)values[0];
if( colorSelected.equals("Red") )
cr = 0xFF;
else if(colorSelected.equals("Green") )
cg = 0xFF;
else if(colorSelected.equals("Blue") )
cb = 0xFF;
else if(colorSelected.equals("Yellow") ){
cr = 0xFF;
cg = 0xFF;
}else if(colorSelected.equals("White") ){
cr = 0xFF;
cg = 0xFF;
cb = 0xFF;
}
outputPanel.setColor(cr,cg,cb);
}
}
class OutputPanel.....same
as above.
|