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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
| import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class test4 extends JPanel implements ListSelectionListener {
int compteur = 0;
private DefaultListModel trip;
private JList list;
private JButton addButton;
private JButton changeButton;
private JButton removeButton;
private static final String[] flightCompanyStrings = {"Air France", "Lufthansa", "US Airways"};
private static final String[] trainCompanyStrings = {"Amtrak", "SNCF", "Tshoo railways"};
private static final String flightString = "Flight";
private static final String trainString = "Train";
private static final String addString = "Add";
private static final String changeString = "Change";
private static final String removeString = "Remove";
private static final String companyString = "Company: ";
private JComboBox companyBox;
private JRadioButton flightRadioButton;
private JRadioButton trainRadioButton;
public test4() {
super(new BorderLayout());
trip = new DefaultListModel();
//Create the list and put it in a scroll pane.
list = new JList(trip);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//list.setSelectedIndex(-1);
list.addListSelectionListener(this);
list.setVisibleRowCount(5);
list.setCellRenderer(new MyCellRenderer());
JScrollPane listScrollPane = new JScrollPane(list);
JPanel listActions = new JPanel();
listActions.add(getActions(), BorderLayout.CENTER);
listActions.add(listScrollPane, BorderLayout.NORTH);
add(listActions, BorderLayout.CENTER);
}
private JPanel getActions() {
JPanel actions = new JPanel(new BorderLayout());
actions.add(getChoice(), BorderLayout.WEST);
actions.add(getFields(), BorderLayout.CENTER);
actions.add(getButtons(), BorderLayout.EAST);
return actions;
}
private JPanel getChoice() {
JPanel choice = new JPanel();
choice.setLayout(new BoxLayout(choice, BoxLayout.Y_AXIS));
flightRadioButton = new JRadioButton (flightString);
trainRadioButton = new JRadioButton (trainString);
flightRadioButton.addActionListener(new FlightActionListener());
trainRadioButton.addActionListener(new TrainActionListener());
ButtonGroup group = new ButtonGroup();
group.add(flightRadioButton);
group.add(trainRadioButton);
choice.add(flightRadioButton);
choice.add(trainRadioButton);
return choice;
}
class FlightActionListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
compteur++; System.out.println(compteur + "entree actionperformed bouton flight");
companyBox.removeAllItems();
for (String company:flightCompanyStrings)
companyBox.addItem(company);
compteur++; System.out.println(compteur + "sortie actionperformed bouton flight");
}
}
class TrainActionListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
compteur++; System.out.println(compteur + "entree actionperformed bouton train");
companyBox.removeAllItems();
for (String company:trainCompanyStrings)
companyBox.addItem(company);
compteur++; System.out.println(compteur + "sortie actionperformed bouton train");
}
}
private JPanel getFields() {
JPanel fields = new JPanel ();
fields.add(getCompany());
return fields;
}
private JPanel getCompany() {
JPanel company = new JPanel(new FlowLayout());
company.add(new JLabel(companyString));
companyBox = new JComboBox();
company.add(companyBox);
return company;
}
private JPanel getButtons() {
JPanel buttons = new JPanel();
addButton = new JButton(addString);
removeButton = new JButton(removeString);
changeButton = new JButton(changeString);
removeButton.addActionListener(new RemoveListener ());
changeButton.addActionListener(new ChangeListener());
addButton.addActionListener(new AddListener());
buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS));
buttons.add(removeButton);
buttons.add(changeButton);
buttons.add(addButton);
removeButton.setEnabled(false);
changeButton.setEnabled(false);
return buttons;
}
class RemoveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//This method can be called only if
//there's a valid selection
int index = list.getSelectedIndex();
trip.remove(index);
int size = trip.getSize();
if (size == 0) { //Nothing's left, disable removing
removeButton.setEnabled(false);
} else { //Select an index.
if (index == trip.getSize()) {
//removed item in last position
index--;
}
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
}
}
class ChangeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//This method can be called only if
//there's a valid selection
int index=list.getSelectedIndex();
if (flightRadioButton.isSelected())
trip.setElementAt("Flight" + (String) companyBox.getSelectedItem(), index);
if (trainRadioButton.isSelected())
trip.setElementAt("Train" + (String) companyBox.getSelectedItem(), index);
list.setModel(trip);
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
}
class AddListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int index = list.getSelectedIndex(); //get selected index
if (index == -1) { //no selection, so insert at beginning
index = 0;
} else { //add after the selected item
index++;
}
if (flightRadioButton.isSelected())
trip.insertElementAt("Flight" + (String) companyBox.getSelectedItem(), index);
if (trainRadioButton.isSelected())
trip.insertElementAt("Train" + (String) companyBox.getSelectedItem(), index);
if (!(flightRadioButton.isSelected() || trainRadioButton.isSelected()))
JOptionPane.showMessageDialog(null,"Select one radio button between Flight, Train and Car.");
//Select the new segment and make it visible.
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
}
private void fillFields(int index) {
compteur++; System.out.println(compteur + "entree fillFields");
String segment = (String) trip.get(index);
char first = segment.charAt(0);
if (first=='F') {
flightRadioButton.setVisible(false);
flightRadioButton.doClick();
flightRadioButton.setVisible(true);
}
if (first=='T') {
trainRadioButton.setVisible(false);
trainRadioButton.doClick();
trainRadioButton.setVisible(true);
}
compteur++; System.out.println(compteur + "sortie fillFields");
}
class MyCellRenderer extends JLabel implements ListCellRenderer {
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
compteur++; System.out.println(compteur + "entree getListCellRendererComponent");
setText((String)value);
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
fillFields(index);
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
compteur++; System.out.println(compteur + "sortie getListCellRendererComponent");
return this;
}
}
public void valueChanged(ListSelectionEvent e) {
compteur++; System.out.println(compteur + "entree valueChanged");
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() == -1) {
//No selection, disable fire button.
removeButton.setEnabled(false);
changeButton.setEnabled(false);
} else {
//Selection, enable the fire button.
removeButton.setEnabled(true);
changeButton.setEnabled(true);
}
}
compteur++; System.out.println(compteur + "sortie valueChanged");
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TripTracker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new test4();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
} |
Partager