Home Button.setEnabled doesn't work in the first loop
Reply: 0

Button.setEnabled doesn't work in the first loop

user47301
1#
user47301 Published in September 21, 2018, 2:48 am

I am working on a little game which I am coding using JFrame. I created a JPanel in which I have a board created with JButtons. I had to check if any letter is the same in a column/line and diagonally. So I created a method which does that for me. The problem is the first statement I put in method won't disable the button even though the program actually reaches that part of code. So I tried a few things to fix it and I just don't understand why the first loop will not disable the Button while following loops will.

 private void checkRepetitions()
{

    Boolean check = false;
    int i, j;
    //check in line
    i = lastI;

        for (j = 0; j < 8; j++) {
            if (tab[lastI][lastJ].getText().equals(tab[i][j].getText()) && lastI != j && !tab[lastI][lastJ].getText().equals("")) {
                confirm.setEnabled(false);
                check = true;
                break;
            } else {
                confirm.setEnabled(true);

            }
        }


    //checkin column
    j = lastJ;

        for (i=0; i<8; i++){
            if(tab[lastI][lastJ].getText().equals(tab[i][j].getText()) && lastI != i && !tab[lastI][lastJ].getText().equals("")){
                confirm.setEnabled(false);
                check = true;
                break;
            }
            else{
                confirm.setEnabled(true);
            }
        }


    //scheck diagonally
    i = 0;
    j = 0;

    if (lastJ == lastI) {
        while (i<7 && j<7) {

            if(tab[lastI][lastJ].getText().equals(tab[i][j].getText()) && lastI != i && !tab[lastI][lastJ].getText().equals("")){
                confirm.setEnabled(false);
                check = true;
                break;
            }

            else if(i<8&& j<8){
                i++;
                j++;
            }

            else{
                confirm.setEnabled(true);
            }
        }
    }
    //check diagonally
    i = 0;
    j = 7;
    if (lastI + lastJ == 7) {
        while (true){

            if(tab[lastI][lastJ].getText().equals(tab[i][j].getText()) && lastI != i && lastJ != j && !tab[lastI][lastJ].getText().equals("")){
                confirm.setEnabled(false);
                check = true;
                break;
            }
            else{
                confirm.setEnabled(true);
            }
            if (i == 7 && j == 0){
                break;
            }
            else{
                i++;
                j--;
            }


        }
    }

}

lastI and lastJ are variables that holds the position of the last button user clicked on and for that button I check then if there are repetitions. confirm button is supposed to get disabled and it does with every loop but the first for loop. Boolean is not used right now so it doesn't impact the code.

share|improve this question
  • I'd be easier to debug if the code was runnable. Perhaps break the code down so it's not reliant on the UI and instead the state is carried in some kind of model which can easily be replicated – MadProgrammer Jan 13 at 3:31
  • 1
    Probably because your other loops are re-enabling the suspect button again. If the condition is true in any loop then disable the button and exit the method from within that loop or after each of the first two loops have if (check) { return; }. – DevilsHnd Jan 13 at 5:49
  • Consider posting Minimal, Complete, and Verifiable example – c0der Jan 13 at 6:11
  • I rewrote the whole method in the same way actually and added as @DevilsHnd said if (check) {return;} and now it seems to work like a charm. Thanks for all the sugestions! – D.Weltrowski Jan 13 at 13:37

active oldest votes

Your Answer

StackExchange.ifUsing("editor", function () { StackExchange.using("externalEditor", function () { StackExchange.using("snippets", function () { StackExchange.snippets.init(); }); }); }, "code-snippets"); StackExchange.ready(function() { var channelOptions = { tags: "".split(" "), id: "1" }; initTagRenderer("".split(" "), "".split(" "), channelOptions); StackExchange.using("externalEditor", function() { // Have to fire editor after snippets, if snippets enabled if (StackExchange.settings.snippets.snippetsEnabled) { StackExchange.using("snippets", function() { createEditor(); }); } else { createEditor(); } }); function createEditor() { StackExchange.prepareEditor({ heartbeatType: 'answer', convertImagesToLinks: true, noModals: false, showLowRepImageUploadWarning: true, reputationToPostImages: 10, bindNavPrevention: true, postfix: "", onDemand: true, discardSelector: ".discard-answer" ,immediatelyShowMarkdownHelp:true }); } });
 
StackExchange.ready( function () { StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48236664%2fbutton-setenabled-doesnt-work-in-the-first-loop%23new-answer', 'question_page'); } );

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged java for-loop jframe jbutton or ask your own question.

StackExchange.ready(function(){$.get('/posts/48236664/ivc/aa25');});
StackExchange.ready(function () { StackExchange.responsiveness.addSwitcher(); }) (function(i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function() { (i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); StackExchange.ready(function () { StackExchange.ga.init({ sendTitles: true, tracker: window.ga, trackingCodes: [ 'UA-108242619-1' ] }); StackExchange.ga.setDimension('dimension2', '|java|for-loop|jframe|jbutton|'); StackExchange.ga.setDimension('dimension3', 'Questions/Show'); StackExchange.ga.trackPageView(); }); /**/ var _qevents = _qevents || [], _comscore = _comscore || []; (function() { var ssl = 'https:' == document.location.protocol, s = document.getElementsByTagName('script')[0], qc = document.createElement('script'); qc.async = true; qc.src = (ssl ? 'https://secure' : 'http://edge') + '.quantserve.com/quant.js'; s.parentNode.insertBefore(qc, s); _qevents.push({ qacct: "p-c1rF4kxgLUzNc" }); /**/ var sc = document.createElement('script'); sc.async = true; sc.src = (ssl ? 'https://sb' : 'http://b') + '.scorecardresearch.com/beacon.js'; s.parentNode.insertBefore(sc, s); _comscore.push({ c1: "2", c2: "17440561" }); })();
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311955 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO