java - setOnMouseClicked not working properly -
i've been working on gui part of card game.
what i'm doing show confirmation dialog when player clicks on 1 of card's imageview.
things has been working smoothly there's bug couldn't fix. after repeatedly clicking on imageview , closing alert dialog. dialog somehow fails render body on subsequent calls.
is bug? or doing things wrongly? first question in stackoverflow, hope find soon. thanks!
i took away of game-logic codes may still seem lengthy:
public class game extends application implements igame { final alert confirmationdialog = new alert(alerttype.confirmation); final alert errordialog = new alert(alerttype.information); player currentplayer; public static void main(string[] args) { // launch application launch(args); } @override public void start(stage startstage) throws exception { // initialization startstage.settitle("welcome card game"); choicebox<integer> choicebox = new choicebox<integer>(); label promptlabel = new label("number of players:"); promptlabel.setstyle("-fx-font: 30 arial"); choicebox.getitems().addall(2, 3, 4); choicebox.setvalue(2); choicebox.setstyle("-fx-font: 20 arial"); vbox outerlayout = new vbox(50); hbox innerlayout = new hbox(30); innerlayout.getchildren().addall(promptlabel, choicebox); innerlayout.setstyle("-fx-alignment: center;"); button buttonstart = new button("start game"); buttonstart.setonaction(e -> { startgame(choicebox.getvalue()); startstage.close(); }); outerlayout.setstyle("-fx-font: 20 arial; -fx-alignment: center; -fx-padding: 100; -fx-background-color: #993333"); outerlayout.getchildren().addall(innerlayout, buttonstart); scene scene = new scene(outerlayout); startstage.setminwidth(700); startstage.setminheight(500); startstage.setscene(scene); startstage.show(); } void startgame(int numberofplayers) { // initialize ui components showmessage("the game has started."); currentplayer = players[playerindex]; getuserselection(); gamestage.settitle("the card game"); scene scene = new scene(mainlayout); scene.setfill(null); gamestage.setscene(scene); gamestage.show(); } @override public void getuserselection() { showmessage("it player " + (playerindex+1) + "'s turn."); playerlabel.settext("player " + (playerindex+1)); if (currentplayer.isplayable(piletop)) { counter=0; playerhand.getchildren().clear(); for(int i=0; i<currentplayer.hand.size(); i++) { card playercard = currentplayer.hand.get(i); playerhand.getchildren().add(playercard.imageview); playercard.imageview.setonmouseclicked(e -> userselectiondialog(playercard)); } playtable.setbottom(playerhand); } else { counter++; if (currentplayer.hand.size() < 5) { showmessage("player " + (playerindex+1) + " draws card."); currentplayer.addcard(stock.remove(stock.size()-1)); if (stock.isempty()) { showmessage("stock pile exhausted, shuffling discard pile.."); shuffle(); } } showmessage("player " + (playerindex+1) + " passes round."); if (counter == playerno) end(); else { nextplayer(); getuserselection(); } } } void userselectiondialog(card playercard) { confirmationdialog.settitle("confirmation dialog"); confirmationdialog.setheadertext("you play " + playercard + "."); confirmationdialog.setcontenttext("confirm playing card?"); confirmationdialog.getbuttontypes().setall(buttontype.yes, buttontype.cancel); optional<buttontype> result = confirmationdialog.showandwait(); if (result.get() == buttontype.yes){ if (playercard.match(piletop)) { currentplayer.playcard(playercard); showmessage("player " + (playerindex+1) + " discarded " + playercard); pile.add(playercard); piletop = pile.get(pile.size()-1); pilepane.getchildren().add(piletop.imageview); // end game if player's hand empty if (currentplayer.hand.isempty()) end(); else { nextplayer(); getuserselection(); } } else { errordialog.settitle("message"); errordialog.setheadertext("cannot play card."); errordialog.setcontenttext("your card not match rank , suit."); errordialog.showandwait(); } } } } what might problem? tried solve myself, no avail. :(
i realize problem comes after fixed number of mouseclick events. suspect application detaches program after time?
the mouse click event works when i'm doing things non-gui, example:
playercard.imageview.setonmouseclicked(e -> system.out.println("test")); however, simple gui functions such adding item listview doesn't work after number of events:
playercard.imageview.setonmouseclicked(e -> listview.add("test"));
screenshots:
no problem first 10 alerts
alert body becomes blank after that
edit
it seems me there problems, might memory leaks causes javafx ui hang. add more load ui components, ui seem hang faster.
i.e: added line set stage fullscreen , causes ui hang within 5 seconds application.
gamestage.setfullscreen(true);
edit 2
i've tested application method test memory consumption of java program eclipse , showing constant 80 megabytes memory usage. memory might not cause of problem.
apparently, couldn't reproduce problem after restart of java eclipse ide. not sure why, guess question no longer valid.


Comments
Post a Comment