Implementing Next and Previous Buttons Using LinkedList

This may be a stupid question, but it's hard for me to figure it out.

I wrote a method that uses LinkedList to navigate through loaded MIDI instruments. I want to make the next and previous button so that every time you click the button, you go through the LinkedList.

If I hardcode itr.next();or itr.previous();several times, I can go through LinkedList

public void setInsturment(Synthesizer start,MidiChannel currentChannel[])
{
    try
    {
        start.open();

        Soundbank bank = start.getDefaultSoundbank();

        start.loadAllInstruments(bank);

        LinkedList<Instrument> currentInstrument = new LinkedList<Instrument>();

        Instrument instrs[] = start.getLoadedInstruments();

        currentInstrument.add(instrs[0]);
        currentInstrument.add(instrs[1]);
        currentInstrument.add(instrs[2]);
        currentInstrument.add(instrs[3]);
        currentInstrument.add(instrs[4]);

        ListIterator itr = currentInstrument.listIterator();
        itr.next();
        itr.next();
        itr.next();
     // nextInstrument();

        currentChannel[1].programChange(0,itr.nextIndex());

    }

    catch(MidiUnavailableException e)
    {
        System.out.print("error");
    }

}

I have a lot of problems creating a button that can go through the list. Is there an effective way to do this? I tried something like this without success.

public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == nextButton)
    {
        sound.nextInstrument();
    }

public void nextInstrument()
{
    itr.next();
}

Thanks in advance guys!

+3
source share
4 answers

, , - , , , , .

, ArrayList .

:

class NextPrevList {
    private int index = 0;
    private List currentList; //initialize it with some list

    public Object next() {
        return list.get(++index);
    }
    public Object prev() {
        //maybe add a check for out of bounds
        if (index == 0) return null;
        return list.get(--index);
    }
}

, ArrayList, LinkedList

+4

ListIterator#next() , . , , , , .

someInstrument = itr.next();
// fire notify observers method.
+4

Interface code: List<Instrument>. This related example moves List<ImageIcon>, but the implementation can be changed as needed.

+4
source

MIDI Instruments .. next and previous button

Use an array (e.g. Instrument[]). It can be displayed in JComboBox, a JListor JSpinnerso that the user can select a tool. Here is an example of using combos with a renderer.

enter image description here

import java.awt.Component;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.*;
import javax.sound.midi.*;

class InstrumentSelector {

    public static void main(String[] args) throws MidiUnavailableException {
        Synthesizer synthesizer = MidiSystem.getSynthesizer();
        synthesizer.open();
        final Instrument[] orchestra = synthesizer.getAvailableInstruments();
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JComboBox orchestraSelector = new JComboBox(orchestra);
                orchestraSelector.setRenderer(new InstrumentRenderer());

                JOptionPane.showMessageDialog(null, orchestraSelector);
            }
        });
    }
}

class InstrumentRenderer extends BasicComboBoxRenderer {

    @Override
    public Component getListCellRendererComponent(
        JList list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(
            list, value, index, isSelected, cellHasFocus);
        if (c instanceof JLabel && value instanceof Instrument) {
            JLabel l = (JLabel)c;
            Instrument i = (Instrument)value;
            l.setText(i.getName());
        }
        return c;
    }
}
+4
source

All Articles