Results 1 to 5 of 5
  1. #1
    JFReturns is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default Question regarding JButton .setVisible() and .setEnabled()

    Good morning,

    First of all hi, I am a noob to the forum and novice to Java. I know the basics.

    I am creating an applet that helps novice guitarists learn the electric guitar. The basic idea is the user presses a combination of buttons representing notes on the guitar, and then the program calculates the tablature and plays back a slide show of the notes being played along with the corresponding AudioClips. And it pretty much works.

    However, I do have problems with JButtons not disappearing or blanking out when I want them to. Sometimes they do, sometimes they don't, and it seems completely random. They are, however, always unselectable so I presume its something to do with refreshing the graphics. Does anyone know what I am doing wrong? Heres a snippet of the context in which I am using .setEnabled():

    Java Code:
    if (e.getActionCommand() == "start over"){
                
             enableControls();
    }
    public void enableControls (){
            playFull.setEnabled(true);
            playHalf.setEnabled(true);
            clearNotes.setEnabled(true);
            pause.setEnabled(true);
            stop.setEnabled(true);
            close.setEnabled(true);
     }

    I have attached the rest of the program code if required. Two things:

    - Apologies for the coarse naming of some of the variables, this is where I got frustrated.
    - The code is lengthy, is not OO, is ugly and needs refactoring. Sorry.

    I would really appreciate and pointers that can be given!

    Regards,

    J

    EDIT: I have attached another file that is much shorter and more basic (but still part of my applet) and has the same problem. The 'pause' and 'stop' buttons start off as .setEnabled(false) and when either of the play buttons are pressed they are set to true. However, they appear grayed out UNTIL the mouse cursor passes over them?!

    This applet is online if anyone wants to view, but I would rather not put the URL on a public forum just yet, so PM me if necessary.

    FURTHER EDIT: Can't seem to attach another file, so here is the shorter code:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    import javax.swing.*;
    /*
    <APPLET CODE=lickOne WIDTH=700 HEIGHT=540>
    </APPLET>
    */
    
    
    
    
    public class lickOne extends JApplet implements ActionListener, Runnable{
        JButton playFull, playHalf, stop, pause;
        
        Image playB;
        JLabel  loadText;
        String chosenNotes, loadingText, loader;
        
        Thread trackThread = null;
        
        String imageList[] = new String[16];
        String audioList[] = new String[16];
        //loaded with all notes sound and images
        
        int notes[] = new int[20];
        // array of ints that represent the combination of notes chosen e.g.
        // 1 = 6-5, 2 = 6-8, 3 = 5-5 etc
        
        int counterForLoadingNotes = 0;
        int counterForNotesChosen = 13;
        int storageForNotePaused = 0;//not needed
        // array of ints that represent the combination of notes chosen e.g.
        // 1 = 6-5, 2 = 6-8, 3 = 5-5 etc
        
        int xOne, yOne, xTwo, yTwo, xThree, yThree, sqW, inc, speed = 0;
        
        Image[] currentNote = new Image[20];
        AudioClip[] currentClip = new AudioClip[20];
        
        boolean okayToPlay, alreadyPlayed, paused, bollocks,paintSq, hideButtons = false;
        boolean loadingReq = true;
        
        public void init(){
            
            
            setUpGUI();
            
        }
        public void setUpGUI(){
            setSize(700,460);
            setBackground(Color.white);
            setVisible(true);
            
            Container guitar = getContentPane();
            guitar.setLayout(null);
         
            
           playB = getImage(getCodeBase(),"playGFull.jpg");
           
           
           
              
            
            playFull = new JButton("play fast",new ImageIcon(playB));
            playB = getImage(getCodeBase(),"playGHalf.jpg");
            
            playHalf = new JButton("play slow   ",new ImageIcon(playB));
            
            playB = getImage(getCodeBase(),"pause.jpg");
            pause = new JButton("pause       ",new ImageIcon(playB));
            
            playB = getImage(getCodeBase(),"stop.jpg");
            stop = new JButton("stop         ",new ImageIcon(playB));
            
            
            
            
            
            
            
            
            chosenNotes = new String ("Notes chosen: ");
            loadingText = new String ("Loading");
            
            loadText = new JLabel(loadingText);
            
            
            guitar.add(playFull);
            guitar.add(playHalf);
            guitar.add(pause);
            guitar.add(stop);
            
            
            
            guitar.add(loadText);
            
            playFull.setBounds(20, 20, 90, 25);
            
            playFull.setSize(138, 30);
            
            playHalf.setBounds(20, 60, 90, 25);
            playHalf.setSize(138, 30);
            pause.setBounds(20, 100, 90, 25);
            pause.setSize(138, 30);
            
            
            stop.setBounds(20, 140, 90, 25);
            stop.setSize(138, 30);
                 
            playFull.addActionListener(this);
            playHalf.addActionListener(this);
            pause.addActionListener(this);
            stop.addActionListener(this);
            
            
            
            for (int j = 0; j < 16; j = j + 1){
                imageList[j] =  j + ".jpg";
                audioList[j] =  j + ".au";
            }
            
            playFull.setEnabled(true);
            playHalf.setEnabled(true);
            pause.setEnabled(false);
            stop.setEnabled(false);
            
            
            xOne = 144;// points top to the right 170
            yOne = 390;// brings top up 100
            xTwo = 134; // 160
            yTwo = 440;// brings bottom right down right 150
            xThree = 154;// 180
            yThree = 440;// 150
            //sqW = 335; //120
            inc = 25;
        }
        
        public void start(){
            if (trackThread == null){
                trackThread = new Thread(this);
                trackThread.start();
            }
        }
        
        public void stop(){
            if (trackThread != null){
                trackThread = null;
            }
        }
        
        public void run(){
            while (trackThread != null){
                //displayNotes.setText(chosenNotes);
                //if (noteLimit()) {
               //     blankOutNotes();
               // } 
                
                
                while (okayToPlay && counterForLoadingNotes < counterForNotesChosen){
                    
                    
                    repaint();
                    
                    
                    xOne = xOne + inc;
                    xTwo = xTwo + inc;
                    xThree = xThree + inc;
                    sqW = sqW + inc;
                        
                    
                    try{
                        Thread.sleep(speed);
                    }
                    catch(InterruptedException e){
                    }
                    
                    //currentClip[counterForLoadingNotes].stop();
                    if (counterForLoadingNotes < (counterForNotesChosen-1)){
                        currentClip[counterForLoadingNotes].stop();
                    }
                    
                    counterForLoadingNotes++;
                }
                //okayToPlay = false;
                
                
                
                
                
                
            }//while (trackThread != null){
            
        }
        
        public void paint(Graphics g){
            //displayNotes.setText(chosenNotes);
            
            Graphics2D g2d = (Graphics2D)g;
            //if(counterForNotesChosen == 0){
                    blankTab(g);
                    
             //   }
            if (paintSq){
                
                rectangle(g,110,265,600,300);
                paintSq = false;
            }        
            
            if (bollocks){
                
                
                
                
                g2d.drawImage(currentNote[counterForLoadingNotes], 200, 25, this);
                currentClip[counterForLoadingNotes].play();
                
                drawTab(g);
                drawTabNotes(g);
                triangle (g, xOne, yOne, xTwo, yTwo, xThree, yThree);
                rectangle(g,50,470,sqW,55);
                
              
            }
            else if(!bollocks){
                
            }
            
           
        }
        
        
        
        public void actionPerformed(ActionEvent e){
            
            
            if (e.getActionCommand() == "play fast"){
                
                enableControls();
                if (loadingReq){
                   loadNotes();
                   loadingReq = false;
                }
                
                
                speed = 500;
                
               
                
                
                if(!paused){
                    if (!alreadyPlayed){
                        okayToPlay = true;
                        alreadyPlayed = true;
                        bollocks = true;
                        paintSq = true;
                    }
                    else if (alreadyPlayed){
                        counterForLoadingNotes = 0;
                        okayToPlay = true;
                        bollocks = true;
                        paintSq = true;
                        
                        xOne = 144;// points top to the right 169
                        xTwo = 134; // 159
                        xThree = 154;// 179
                        sqW = 0;
                        
                        
           
                    } 
                }
                else if(paused){
                    okayToPlay = true;
                    paused = false;
                    bollocks = true;
                    inc = 25;
                    
                }
                
            }
            if (e.getActionCommand() == "play slow   "){
                
                enableControls();
                if (loadingReq){
                   loadNotes();
                   loadingReq = false;
                }
                //disableNotes();
                hideButtons = true;
                speed = 1500;
                
               
                //blankOutNoteButtons ();
                
                if(!paused){
                    if (!alreadyPlayed){
                        okayToPlay = true;
                        alreadyPlayed = true;
                        bollocks = true;
                        paintSq = true;
                    }
                    else if (alreadyPlayed){
                        counterForLoadingNotes = 0;
                        okayToPlay = true;
                        bollocks = true;
                        paintSq = true;
                        
                        xOne = 144;// points top to the right 169
                        xTwo = 134; // 159
                        xThree = 154;// 179
                        sqW = 0;
                        
                        
           
                    } 
                }
                else if(paused){
                    okayToPlay = true;
                    paused = false;
                    bollocks = true;
                    inc = 25;
                    
                }
            }
            
            
            if (e.getActionCommand() == "stop         "){
                if (counterForLoadingNotes > 0){
                    //currentClip.stop();
                    currentClip[counterForLoadingNotes].stop();
                    okayToPlay = false;
                    paused = false;
                    inc = 25;
                }
                
                
            }
            
            if (e.getActionCommand() == "pause       "){
                if (counterForLoadingNotes > 0){
                    //currentClip.stop();
                    currentClip[counterForLoadingNotes].stop();
                    paused = true;
                
                //storageForNotePaused = counterForLoadingNotes;
                    okayToPlay = false; 
                    inc = 0;
                    paintSq = true;
                }
            }
            
           
            
        }
        
       
        public void loadNotes(){
            //loadText.setVisible(true);
            //loading();
            
            for (int i = 1; i < 20; i++){
                    notes[i] = i;
            }
            Graphics g = getGraphics();
            loader = "Loading";
            /*for (int i =0; i < 20; i++){
                currentClip[i] = null;
                currentNote[i] = null;
            }*/
            g.setColor(Color.white);
            g.fillRect(20, 420, 200, 40);
            for (int i =0; i <= counterForNotesChosen; i++){
                currentNote[i] = getImage(getCodeBase(),imageList[notes[i]]);
                currentClip[i] = getAudioClip(getDocumentBase(),audioList[notes[i]]);
                g.setColor(Color.black);
                g.drawString(loader, 20, 450);
                loader = loader + "  .  ";
                
                
                
                
                
                //chosenNotes = chosenNotes + "B2, ";
                //displayNotes.setText(chosenNotes);
                try{
                    Thread.sleep(100);
               }
               catch(InterruptedException e){
               }
               
                
            }
            g.setColor(Color.white);
            g.fillRect(20, 420, 200, 40);
            g.setColor(Color.black);
                loader = "Loaded";
                g.drawString(loader, 20, 450);
            
                
        }
        
       
        
        
        
        
        
        public void blankTab(Graphics g){
            g.setColor(Color.white);
            g.fillRect(145,350,520,120);
            drawTab(g);
        }
        public void drawTab (Graphics g){
            
            g.setColor (Color.LIGHT_GRAY);
            g.drawRect (180,280,500,100);
           
            //g.setColor (Color.red);
            g.drawLine(185, 380, 185, 280);
            
            
           // g.setColor (Color.LIGHT_GRAY);
            g.drawLine(180, 300, 680, 300);
            g.drawLine(180, 320, 680, 320);
            g.drawLine(180, 340, 680, 340);
            g.drawLine(180, 360, 680, 360);
            
            g.setColor (Color.black);
            g.drawString("E", 170, 285);
            g.drawString("A", 170, 305);
            g.drawString("D", 170, 325);
            g.drawString("G", 170, 345);
            g.drawString("B", 170, 365);
            g.drawString("E", 170, 385);
        }
        
        public void drawTabNotes (Graphics g){
            int inc = 190;
                    
            for(int i = 0; i < counterForNotesChosen; i++){
                if (notes[i] == 1){
                    g.drawString("5",inc,285);
                    inc = inc + 25;
                }
                if (notes[i] == 2){
                    g.drawString("8",inc,285);
                    inc = inc + 25;
                }
                if (notes[i] == 3){
                    g.drawString("5",inc,305);
                    inc = inc + 25;
                }
                if (notes[i] == 4){
                    g.drawString("7",inc,305);
                    inc = inc + 25;
                }
                if (notes[i] == 5){
                    g.drawString("5",inc,325);
                    inc = inc + 25;
                }
                if (notes[i] == 6){
                    g.drawString("7",inc,325);
                    inc = inc + 25;
                }
                if (notes[i] == 7){
                    g.drawString("5",inc,345);//dfgggggggggggg
                    inc = inc + 25;
                }
                if (notes[i] == 8){
                    g.drawString("7",inc,345);
                    inc = inc + 25;
                }
                if (notes[i] == 9){
                    g.drawString("5",inc,365);
                    inc = inc + 25;
                }
                if (notes[i] == 10){
                    g.drawString("8",inc,365);
                    inc = inc + 25;
                }
                if (notes[i] == 11){
                    g.drawString("5",inc,385);
                    inc = inc + 25;
                }
                if (notes[i] == 12){
                    g.drawString("8",inc,385);
                    inc = inc + 25;
                }
                if (notes[i] == 13){
                    g.drawString("7^",inc,345);
                    inc = inc + 25;
                }
                if (notes[i] == 14){
                    g.drawString("8^",inc,365);
                    inc = inc + 25;
                }
                if (notes[i] == 15){
                    g.drawString("8^",inc,385);
                    inc = inc + 25;
                }
            }
        }
        
        
        public void triangle (Graphics g, int x1, int y1,int x2, int y2, int x3, int y3)  {
           
            g.setColor (Color.BLACK);
            
            g.drawLine (x1, y1, x2, y2);
            g.drawLine (x2, y2, x3, y3);
            g.drawLine (x3, y3, x1, y1);
        }
        
        public void rectangle(Graphics g, int x, int y, int w, int h){
          
            g.setColor(Color.white);
            g.fillRect(x, y, w, h);
        }
        
        public void enableControls (){
            
            
            pause.setEnabled(true);
            stop.setEnabled(true);
            
        }
        
        
        
        
        
    
    }
    Attached Files Attached Files
    Last edited by JFReturns; 02-26-2009 at 10:28 AM. Reason: Adding a simpler code example

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    for comparing Strings
    please use stringVar1.equals(stringVar2)

  3. #3
    JFReturns is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    for comparing Strings
    please use stringVar1.equals(stringVar2)
    Thank you, I will do. Good tip.

  4. #4
    JFReturns is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    EDIT: Noob comment deleted.
    Last edited by JFReturns; 02-26-2009 at 12:18 PM. Reason: Stupid question asked, and deleted

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    For changing the UI, you have to make sure that the code is executed by the event handling thread. This can be done using either SwingUtilities.invokeLater() or having the code in an event listener (e.g. using addActionListener()). Also, be sure to call repaint() if you change the appearance of something, and revalidate() if you change the contents of a container (add/remove components)

Similar Threads

  1. JButton Problem
    By wassim in forum AWT / Swing
    Replies: 6
    Last Post: 02-18-2009, 10:29 PM
  2. [SOLVED] Problem with setVisible(); on LINUX
    By lepetitprince in forum AWT / Swing
    Replies: 9
    Last Post: 11-22-2008, 04:14 PM
  3. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 AM
  4. setVisible(true) does not work
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2007, 07:32 PM
  5. Few action in one Jbutton
    By kubiasty in forum New To Java
    Replies: 0
    Last Post: 07-25-2007, 10:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •