Results 1 to 8 of 8
  1. #1
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Question How to put JSpinners value into an array?

    Hi everyone,

    i have the following code working fine :)

    Java Code:
    import javax.sound.midi.*;
    
     public class MidiSynthesizerSample {
    
         public static void main(String[] args) {
    
             int[] notes = new int[]{60, 62, 64, 65, 67, 69};
    
             try {
                 Synthesizer synthesizer = MidiSystem.getSynthesizer();
                 synthesizer.open();
    
                 MidiChannel channel = synthesizer.getChannels()[0];
    
                 for (int note : notes) {
                     channel.noteOn(note, 50);
    
                     try {
                         Thread.sleep(200);
                     } catch (InterruptedException e) {
                         break;
                     }
    
                     finally {
                         channel.noteOff(note);
                     }
                 }
             } catch (MidiUnavailableException e) {
                 e.printStackTrace();
             }
         }
     }

    NOW how can i get values from the bottom 6 JSpinners at runtime and put them in the array:confused:

    int[] notes = new int[]{60, 62, 64, 65, 67, 69};


    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner jSpinner2;
    private javax.swing.JSpinner jSpinner3;
    private javax.swing.JSpinner jSpinner4;
    private javax.swing.JSpinner jSpinner5;
    private javax.swing.JSpinner jSpinner6;

    I am using NetBeans IDE 6.7.1

    Thanks you ;)
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You'll probably want to add a ChangeListener to the spinners and then react when they change their values. Please have a look at the Sun Java Swing JSpinner tutorial.

  3. #3
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Default

    Hi Fuba,

    Good tutorial, I got the values using the change listener. but how can i place them in the this array nt[] notes = new int[]{60, 62, 64, 65, 67, 69};:confused:
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Within the change listener, in the stateChanged() method, read each of the spinner values, and use the values set the array values. How you do it in code will all depend on what your current code looks like. It should be fairly easy for you to do, I'm thinking.

  5. #5
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Default

    Done, but it give me errors in the notes array:confused:

    int[] notes = new int[]{value1, value2, value3, value4, value5, value6};

    the error says:
    Incompatible types
    required: Int
    found in: java.lang.Object

    times the six values in the array above.

    below are my code for your perusal.

    Java Code:
     public void stateChanged(ChangeEvent evt) {
            
        JSpinner jSpinner1 = (JSpinner) evt.getSource();
        JSpinner jSpinner2 = (JSpinner) evt.getSource();
        JSpinner jSpinner3 = (JSpinner) evt.getSource();
        JSpinner jSpinner4 = (JSpinner) evt.getSource();
        JSpinner jSpinner5 = (JSpinner) evt.getSource();
        JSpinner jSpinner6 = (JSpinner) evt.getSource();
    
        // Get the new value
        Object value1 = jSpinner1.getValue();
        Object value2 = jSpinner2.getValue();
        Object value3 = jSpinner3.getValue();
        Object value4 = jSpinner4.getValue();
        Object value5 = jSpinner5.getValue();
        Object value6 = jSpinner6.getValue();    
        
        int[] notes = new int[]{value1, value2, value3, value4, value5, value6};
    
             try {
                 Synthesizer synthesizer = MidiSystem.getSynthesizer();
                 synthesizer.open();
    
                 MidiChannel channel = synthesizer.getChannels()[0];
    
                 for (int note : notes) {
                     channel.noteOn(note, 50);
    
                     try {
                         Thread.sleep(200);
                     } catch (InterruptedException e) {
                         break;
                     }
    
                     finally {
                         channel.noteOff(note);
                     }
                 }
             } catch (MidiUnavailableException e) {
                 e.printStackTrace();
             }
        }
        
        // This is the button i want to use after i change the values in the spinners
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
    Thank you very much ...
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Here:
    Java Code:
        JSpinner jSpinner1 = (JSpinner) evt.getSource();
        JSpinner jSpinner2 = (JSpinner) evt.getSource();
        JSpinner jSpinner3 = (JSpinner) evt.getSource();
        JSpinner jSpinner4 = (JSpinner) evt.getSource();
        JSpinner jSpinner5 = (JSpinner) evt.getSource();
        JSpinner jSpinner6 = (JSpinner) evt.getSource();
    All spinners will be one and the same -- the one that triggered the change listener, and this isn't what you want.

    Here:
    Java Code:
    Object value1 = jSpinner1.getValue();
    the Object returned by the spinner's getValue will likely be an Integer, if your spinner holds ints. So cast it as such and use the Integer in the array.

    here:
    Java Code:
        int[] notes = new int[]{value1, value2, value3, value4, value5, value6};
    you'll be creating a new int array variable with each stateChanged method call. I'm not sure that you really want to do this.

    here:
    Java Code:
             try {
                 Synthesizer synthesizer = MidiSystem.getSynthesizer();
                 synthesizer.open();
    
                 MidiChannel channel = synthesizer.getChannels()[0];
    
                 for (int note : notes) {
                     channel.noteOn(note, 50);
    
                     try {
                         Thread.sleep(200);
                     } catch (InterruptedException e) {
                         break;
                     }
    you're trying to do some nasty things such as Thread.sleep, on the EDT. Just don't do that.

    I suggest that you try to work on your spinners in isolation of your Midi code, just to see if you can get things to work, say create an SSCCE.

    Here's my example of a sample SSCCE on this matter:
    Java Code:
    import java.awt.GridLayout;
    import java.util.Arrays;
    
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    @SuppressWarnings("serial")
    public class SpinnerSSCCE extends JPanel {
      
      private static final int SPINNER_COUNT = 5;
      
      private int[] values = new int[SPINNER_COUNT];
      private JSpinner[] spinners = new JSpinner[SPINNER_COUNT];
      private JTextField tField = new JTextField();
      
      public SpinnerSSCCE() {
        JPanel spinnerPanel = new JPanel(new GridLayout(1, 0, 10, 0));
        SpinnerListener spinnerListener = new SpinnerListener();
        
        for (int i = 0; i < spinners.length; i++) {
          spinners[i] = new JSpinner(new SpinnerNumberModel(5, 0, 10, 1));
          spinners[i].addChangeListener(spinnerListener);
          spinnerPanel.add(spinners[i]);
        }
        
        setLayout(new GridLayout(0, 1, 0, 10));
        add(spinnerPanel);
        add(tField);
      }
      
      private class SpinnerListener implements ChangeListener {
        public void stateChanged(ChangeEvent e) {
          for (int i = 0; i < spinners.length; i++) {
            values[i] = (Integer)spinners[i].getValue();
          }
          tField.setText("values array: " + Arrays.toString(values));
        }
      }
    
      private static void createAndShowUI() {
        JFrame frame = new JFrame("SpinnerGUI");
        frame.getContentPane().add(new SpinnerSSCCE());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Do you really want to get the JSpinner values when Chord button is pressed? If so, get your spinner values in this button's ActionListener's actionPerformed method. Cast your spinner values as Integers, use them to create your array and call your playNote method.

    BTW, sorry, but I find netbeans-generated code impossibly hard to read.

  8. #8
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Default

    lemme have a go again. will get back to you the progress ...

    thanks.
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

Similar Threads

  1. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  2. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  3. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. Can you Limit JSpinners??
    By ashton in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 06:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •