Re: Help with Swing
Try this:
---------------------- 8< ------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class ZzzMadGUI implements ActionListener {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ZzzMadGUI();
}
});
}
private List<String> textFieldValues = new ArrayList<String>();
private JTextField textField;
private int currentPosition;
ZzzMadGUI() {
final Box textBox = Box.createHorizontalBox();
textField = new JTextField(25);
JButton upButton = new JButton("UP");
upButton.addActionListener(this);
upButton.setActionCommand("UP");
JButton downButton = new JButton("DOWN");
downButton.addActionListener(this);
downButton.setActionCommand("DOWN");
textFieldValues.add("");
textBox.add(textField);
textBox.add(upButton);
textBox.add(downButton);
JFrame f = new JFrame("ZzzMadGUI");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(textBox);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("UP")) {
textFieldValues.set(currentPosition, textField.getText());
if (currentPosition == 0) {
textFieldValues.add(0, "");
} else
currentPosition--;
textField.setText(textFieldValues.get(currentPosition));
}
if (e.getActionCommand().equals("DOWN")) {
textFieldValues.set(currentPosition, textField.getText());
if (currentPosition == textFieldValues.size() - 1) {
textFieldValues.add("");
}
currentPosition++;
textField.setText(textFieldValues.get(currentPosition));
}
}
}
----------------------------- 8< ----------------------------------
The code is far from optimal but I leave it up to you to enhance it.
Stefan