2

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);

    }  
Dhanuka
  • 2,826
  • 5
  • 27
  • 38
Theresia
  • 33
  • 5
  • It seems to me that the method play return type is void, ergo can not be assigned to jButton1. I am not sure I understood correctly, but jButton1 is a button, right? And on its click action you want to run the pattern already optimized? Then you first need to see how to get the pattern optimized from the player, it looks like "player.play(pattern)" only runs it, maybe it is saved in some other property of the player? Once you get it, save it in some private attribute and you can use it in your jButton3ActionPerformed, but dont try to assign it to the button. – Dzyann May 26 '15 at 16:13
  • Thank you for answer sir, yes jbutton1 is a button and i want to play the pattern when jbutton1 is clicked. The pattern that already optimized, is on bestMusicString variable. – Theresia May 26 '15 at 16:17
  • I assume this code is inside a class or something like that, right? Make the pattern an attribute of the class, or such, something that can be reached from the Action of the jButton1, and inside there write the code "Player player = new Player(); player.play(pattern);". – Dzyann May 26 '15 at 16:23
  • 1) Post an [MCVE](http://stackoverflow.com/help/mcve). 2) When you get an error, post the stack trace and specify which line in your posted code throws it. – user1803551 May 26 '15 at 17:04

1 Answers1

0

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)

David Koelle
  • 20,726
  • 23
  • 93
  • 130