how to add action to new jbutton inside another button for playing jfugue pattern

446 Views Asked by At

I want to ask how to add action jbutton in java, im newbie on java. im making a program to optimize chord with Harmony Search algorithm using netbeans. jButton3 has a function to process the algorithms, but i need to make 1 button more named jButton1 only for playing the pattern which already optimized. but ive got a message error

incompatible types: void cannot be converted to jButton1.

how can i fix it? thank you.

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:

hs = new HarmonySearch(oriChord, hms, hmcr, par, delta, max_iteration);

        //hs = new HarmonySearch();

        hs.train();
        String melodyMusicString = hs.nodeListToMusicString(oriMelody);
        String chordMusicString = hs.nodeListToMusicString(oriChord);
        String bestMusicString = hs.nodeListToMusicString(hs.best_chord());
        //System.out.println(chordMusicString);
        Pattern pattern = new Pattern("T["+jComboBox1.getSelectedItem()+"]");
        pattern.add("V0 " + melodyMusicString);
        pattern.add("V1 " + bestMusicString);
        Player player = new Player();
        jButton1 = player.play(pattern);

    }  
1

There are 1 best solutions below

0
On BEST ANSWER

Here's your problem:

jButton1 = player.play(pattern);

The problem is that player.play does not return a value, and this is not how you make a button take an action in Java.

I think what you're trying to do is play a pattern when you press the button. I believe you need this:

jButton1 = new JButton("Play pattern");
jButton1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        playPattern();
    }
});

and later, add this method:

public void playPattern() {
    player.play(pattern);
}

(you can also play the pattern within the actionPerformed, but it requires marking pattern as final, the full understanding of which requires a little more Java experience on your part)