3 import java.awt.event.*;
7 * Graphical representation of a MIDI note.
9 * @author Andersson, Andreen, Lanneskog, Pehrson
13 public class MooNoteProp extends JDialog{
16 private JOptionPane optionPane;
17 private JTextField pitch;
18 private JTextField velocity;
19 private JTextField length;
22 * Creates a new note preference dialog.
23 * @param mn The note that will be graphically represented
25 public MooNoteProp(MooNote mn) {
26 super((JFrame)null, true);
28 setTitle("Note properties");
30 pitch = new JTextField(new Integer(note.getPitch()).toString(),3);
31 JPanel pitchpanel = new JPanel();
32 pitchpanel.add(new Label("Pitch: "));
33 pitchpanel.add(pitch);
36 velocity = new JTextField(new Integer(note.getVelocity()).toString(),3);
37 JPanel velocitypanel = new JPanel();
38 velocitypanel.add(new Label("Velocity: "));
39 velocitypanel.add(velocity);
41 length = new JTextField(new Integer(note.getDuration()).toString(),5);
42 JPanel lengthpanel = new JPanel();
43 lengthpanel.add(new Label("Length: "));
44 lengthpanel.add(length);
46 Object[] array = {"Set the note properties",
51 final String btnString1 = "Enter";
52 final String btnString2 = "Cancel";
53 Object[] options = {btnString1, btnString2};
55 optionPane = new JOptionPane(array,
56 JOptionPane.QUESTION_MESSAGE,
57 JOptionPane.YES_NO_OPTION,
61 setContentPane(optionPane);
62 setDefaultCloseOperation(EXIT_ON_CLOSE);
64 ActionListener intValidator = new ActionListener() {
65 public void actionPerformed(ActionEvent e) {
66 if (e.getSource() instanceof JTextField){
67 JTextField s = (JTextField)e.getSource();
68 int num = Integer.parseInt(s.getText());
71 else if (num > 127 && s != length)
73 s.setText(new Integer(num).toString());
77 pitch.addActionListener(intValidator);
78 velocity.addActionListener(intValidator);
79 length.addActionListener(intValidator);
81 optionPane.addPropertyChangeListener(new PropertyChangeListener() {
82 public void propertyChange(PropertyChangeEvent e) {
83 String prop = e.getPropertyName();
86 && (e.getSource() == optionPane)
87 && (prop.equals(JOptionPane.VALUE_PROPERTY) ||
88 prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
89 Object value = optionPane.getValue();
91 if (value == JOptionPane.UNINITIALIZED_VALUE) {
96 // Reset the JOptionPane's value.
97 // If you don't do this, then if the user
98 // presses the same button next time, no
99 // property change event will be fired.
101 JOptionPane.UNINITIALIZED_VALUE);
103 if (value.equals(btnString1)) {
104 note.setPitch(Integer.parseInt(pitch.getText()));
105 note.setVelocity(Integer.parseInt(velocity.getText()));
106 note.setDuration(Integer.parseInt(length.getText()));
109 } else { // user closed dialog or clicked cancel
118 public static void main(String[] args){
120 MooNote n = new MooNote(0,0,10,20,0,30);
121 MooNoteProp prop = new MooNoteProp(n);
122 prop.setVisible(true);
123 prop = new MooNoteProp(n);
124 prop.setVisible(true);