Results 1 to 8 of 8
  1. #1
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    35
    Rep Power
    0

    Default buttons and icons dont show

    Hello,
    I made a frame with 100 buttons in it (a checkers board).
    And if I run it, it should show them and some of them with pictures of checker-stones.

    But I wrote one new loop and now it only shows one button. When I move my mouse over the frame it shows the rest.
    I tried putting

    Java Code:
    validate();
    pack();
    in my code, but that does not help.
    Any ideas what I should do?

    thanks ,
    Maarten.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: buttons and icons dont show

    Make sure setVisible() is called AFTER the GUI has been completely setup.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    35
    Rep Power
    0

    Default Re: buttons and icons dont show

    Hi
    Thanks for the advice, however, I tried it, but with the same result.
    These are the fields and the constructor of the main class:

    Java Code:
    public class DamBord extends JFrame
    {
        JPanel hetBord = new JPanel();
        private DamSchijf[] schijvenLijst;
        private DamButton[] buttons = new DamButton[100]; 
        private VerwerkZet verwerker = new VerwerkZet();
        
        public static void main (String args[]){
                new DamBord();
            }
    
        /**
         * Constructor for objects of class DamBord
         */
        public DamBord()
        {
            super("Dammen");
            setSize(600,600);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            hetBord.setLayout(new GridLayout(10,10));
            schijvenLijst = new DamSchijf[40];
            vulLijst();
            maakBord();
            plaatsSchijven();
            add(hetBord);
            setVisible(true);
            revalidate();
               
        }

    vullijst() makes a list of DamSchijf.
    maakBord() makes the 100 buttons.
    plaatsSchijven() places the images of the DamSchijf on the right place in the list of buttons. Its after I changed this code that the problem occured. This is the code for plaatsSchijven():

    Java Code:
    public void plaatsSchijven()
       {
         buttons = verwerker.verwerkZet(schijvenLijst,buttons);
       }
    And this is the whole class verwerkZet:

    Java Code:
    public class VerwerkZet
    {
           
        public VerwerkZet()
        {
            
        }
        
        public DamButton[] verwerkZet(DamSchijf[] deSchijvenLijst, DamButton[] deButtons)
        {
            for (DamSchijf deSchijf : deSchijvenLijst)
            {
                for (int i=0; i<100; i++)
                {
                    if ((deSchijf.getX() == deButtons[i].getX()) && (deSchijf.getY() == deButtons[i].getY()))
                    {
                        deButtons[i].plaatsSchijf(deSchijf);
                       
                    }
                }
            }
            
            return deButtons;
            
        }

    I'm probably giving to much information, but I don't know what triggered the problem, so I dont really know what to add or leave.
    Maybe someone can figure it out?
    Thanks
    Maarten

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: buttons and icons dont show

    Can you make a small, complete program that compiles, executes and shows the problem?
    Something that can be copied for testing.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: buttons and icons dont show

    It is hard to tell with what you submitted but a repaint of the board is probably needed.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    35
    Rep Power
    0

    Default Re: buttons and icons dont show

    Hello,
    I managed to make the program shorter and the problem still occurs. There are two classes, DamBord and DamButton. DamBord fills a panel with 100 buttons. But only one shows and when I move my mouse over the field the rest shows. The strange(!) thing is when I leave out the getX() and getY() methods in DamButton the problem is gone. I can't see how they influence the situation. I sort of hope the same is true if somebody else executes the code.

    Here is the code for DamBord and DamButton, it is the whole program.

    Java Code:
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.GridLayout;
    
    
    
    public class DamBord extends JFrame
    {
        
        JPanel hetBord = new JPanel();
        private DamButton[] buttons = new DamButton[100]; 
    
        
       
        public DamBord()
        {
            super("Dammen");
            setSize(600,600);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            hetBord.setLayout(new GridLayout(10,10));
            add(hetBord);
            maakBord();
            setVisible(true);
          
          
        }
    
       
        public void maakBord()
        {
           for (int i=0; i<100 ; i++)
           {
               buttons[i]= new DamButton(0,0,0);
               hetBord.add(buttons[i]);
            }
        }
    }
    and


    Java Code:
    import javax.swing.JButton;
    import javax.swing.ImageIcon;
    import java.awt.Color; 
    
    
    public class DamButton extends JButton
    {
        
        int zwartOfWit; // wit = 0 en zwart = 1
        int x; // de x coordinaat van de button, oftewel het veld
        int y; // de y coordingaat van de button, oftewel het veld
        
            
        
    
        /**
         * Constructor for objects of class DamButton
         */
        public DamButton (int zwartOfWit, int xcoord, int ycoord)
        {
            super();
            this.x = xcoord;
            this.y = ycoord;
            switch(zwartOfWit)
                {
                    case 0: 
                        setBackground(Color.WHITE);
                        break;
                    case 1: 
                        setBackground(Color.BLACK);
                        break;
                 }
        }
        
        
        public int getX()
        {
            return x;
        }
     
        public int getY()
        {
            return y;
        }
           
        
    }
    Any ideas why only the top-left button shows up? Is this also true when the code is executed on another computer?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: buttons and icons dont show

    it is the whole program.
    How do you start the program's execution? I don't see a main() method.

    when I leave out the getX() and getY() methods in DamButton the problem is gone
    Those methods override an extended class's methods. Do they return the values that the class expects/requires?
    Last edited by Norm; 07-12-2015 at 02:56 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    35
    Rep Power
    0

    Default Re: buttons and icons dont show

    Hello,
    It is the whole program. I made it in BlueJ and the book I used to learn (programming java in BlueJ), doesn't teach the main method, I start the program within BlueJ.

    When I changed the getX() and getY() methods in getTheX() and getTheY(), the problem was solved! Somehow this interfered I guess? I didn't realize I was overriding a method. But it did give me the right results, also before I changed the name of the method.
    Anyway, thanks a lot for the help! It is very rewarding to find the solution after a small search.

    Maarten.

Similar Threads

  1. Replies: 9
    Last Post: 07-03-2015, 04:34 PM
  2. Replies: 3
    Last Post: 12-08-2013, 09:28 PM
  3. Replies: 2
    Last Post: 10-31-2012, 12:24 AM
  4. Buttons to show new panels
    By Lehane_9 in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 04:22 PM
  5. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 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
  •