What I did all day was just one programme:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ListTest
{
public static void main(String[] args)
{
JFrame frame = new ListFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ListFrame extends JFrame
{
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 300;
private JPanel listPanel;
private JLabel label ;
private JList wordList;
private JPanel buttonPanel ;
private ButtonGroup group;
private String prefix = "The ";
private String suffix = "for jumps over the lazy dog.";
public ListFrame()
{
setTitle("ListTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
String words[] =
{
"quick","brown","hungry","wild","silent",
"huge","private","abstract","static","final"
};
wordList = new JList(words);
wordList.setVisibleRowCount(4);
JScrollPane scrollPane = new JScrollPane(wordList);
listPanel = new JPanel();
listPanel.add(scrollPane);
wordList.addListSelectionListener(
new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
Object[] values = wordList.getSelectedValues();
StringBuilder text = new StringBuilder(prefix);
for ( int i = 0 ; i < values.length ; i++ )
{
String word = (String) values[i];
text.append(word);
text.append(" ");
}
text.append(suffix);
label.setText(text.toString());
}
});
buttonPanel = new JPanel();
group = new ButtonGroup();
makeButton ("Vertival",JList.VERTICAL);
makeButton("Vertical Wrap",JList.VERTICAL_WRAP);
makeButton("Horizontal Wrap",JList.HORIZONTAL_WRAP);
add(listPanel,BorderLayout.NORTH);
label = new JLabel(prefix+suffix);
add(label,BorderLayout.SOUTH);
}
private void makeButton(String label, final int orientation)
{
JRadioButton button = new JRadioButton(label);
buttonPanel.add(button);
if (group.getButtonCount() == 0)
{
button.setSelected(true);
}
group.add(button);
button.addActionListener( new
ActionListener()
{
public void actionPerformed(ActionEvent e )
{
wordList.setLayoutOrientation(orientation);
listPanel.revalidate();
}
});
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ListTest
{
public static void main(String[] args)
{
JFrame frame = new ListFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ListFrame extends JFrame
{
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 300;
private JPanel listPanel;
private JLabel label ;
private JList wordList;
private JPanel buttonPanel ;
private ButtonGroup group;
private String prefix = "The ";
private String suffix = "for jumps over the lazy dog.";
public ListFrame()
{
setTitle("ListTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
String words[] =
{
"quick","brown","hungry","wild","silent",
"huge","private","abstract","static","final"
};
wordList = new JList(words);
wordList.setVisibleRowCount(4);
JScrollPane scrollPane = new JScrollPane(wordList);
listPanel = new JPanel();
listPanel.add(scrollPane);
wordList.addListSelectionListener(
new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
Object[] values = wordList.getSelectedValues();
StringBuilder text = new StringBuilder(prefix);
for ( int i = 0 ; i < values.length ; i++ )
{
String word = (String) values[i];
text.append(word);
text.append(" ");
}
text.append(suffix);
label.setText(text.toString());
}
});
buttonPanel = new JPanel();
group = new ButtonGroup();
makeButton ("Vertival",JList.VERTICAL);
makeButton("Vertical Wrap",JList.VERTICAL_WRAP);
makeButton("Horizontal Wrap",JList.HORIZONTAL_WRAP);
add(listPanel,BorderLayout.NORTH);
label = new JLabel(prefix+suffix);
add(label,BorderLayout.SOUTH);
}
private void makeButton(String label, final int orientation)
{
JRadioButton button = new JRadioButton(label);
buttonPanel.add(button);
if (group.getButtonCount() == 0)
{
button.setSelected(true);
}
group.add(button);
button.addActionListener( new
ActionListener()
{
public void actionPerformed(ActionEvent e )
{
wordList.setLayoutOrientation(orientation);
listPanel.revalidate();
}
});
}
}