Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,455 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,593 people online right now. Registration is fast and FREE... Join Now!




JButton in JButton problem

 

JButton in JButton problem

tiebird

2 Jul, 2009 - 12:57 PM
Post #1

New D.I.C Head
*

Joined: 2 Jul, 2009
Posts: 4


My Contributions
As a JAVA beginner I ran in a bit a problem.
The program runs fine but everytime after several compilations there are JButtons created in the JButtons...
I think there is a big structural mistake but I can't seem to find where

Code of MainProgram.java

CODE
package wieIsHet;

public class MainProgram
{    
    private static boolean restart = true;
    
    public static void main(String[] args)
    {
        Singleton singleton = new Singleton();
        
        //for(;restart;)
        //{
            Game game = new Game();
            game.start();
        //}
    }
}


Code of Game.java

CODE
package wieIsHet;

public class Game
{
    private Lijst lijst;
    private Singleton singleton;
    
    public void start()
    {
        lijst = new Lijst();
        singleton = Singleton.getSingletonInstance();
        singleton.geefLijst(lijst);
        singleton.venster2.teken(1);
        singleton.plaatsLabels();
    }
}


Code of Lijst.java

CODE
package wieIsHet;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JOptionPane;

public class Lijst
{
    private ArrayList<Persoon> personen;
    public List<Persoon> gekozenPersonen;
    private Singleton singleton;

    public Lijst()
    {        
        singleton = Singleton.getSingletonInstance();
        try {
            //lees de personen in
            BufferedReader lezen = new BufferedReader(new FileReader("personen.txt"));
            personen = new ArrayList<Persoon>();
            gekozenPersonen = new ArrayList<Persoon>();
            String s = lezen.readLine();
            for(;s!=null;)
            {
                //splits in kenmerken en maak nieuw object persoon aan
                String[] temp = s.split(";");
                personen.add(new Persoon(temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8]));
                s = lezen.readLine();
            }
            lezen.close();
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null,
                    "De lijst van personen kon niet ingeladen worden",
                    "Error 2!", JOptionPane.OK_OPTION);
            System.exit(2);
        } catch (IOException e)
        {
            
            JOptionPane.showMessageDialog(null,
                    "De lijst van personen kon niet ingeladen worden",
                    "Error 3!", JOptionPane.OK_OPTION);
            System.exit(3);
        }
        
        if(personen.size() < 20)
        {
            System.exit(4);
        }
        else
        {
            Random willekeurig = new Random();
        
            for(int i = 0; i < 20; i++)
            {
                //kies 20 willekeurige personen
                int randomCijfer = willekeurig.nextInt(personen.size());
                gekozenPersonen.add(personen.get(randomCijfer));
                personen.remove(randomCijfer);
            }
        }
    }
}


Code of Persoon.java

CODE
package wieIsHet;

public class Persoon
{
    private String pad;
    private String haarKleur;
    private String naam;
    private String haarType;
    private String ogenKleur;
    private String ornament;
    private String leeftijd;
    private String huidsKleur;
    private String geslacht;
    private boolean geraden = false;

    public Persoon(String pad, String naam, String haarKleur, String haarType, String ogenKleur, String ornament, String leeftijd, String huidsKleur, String geslacht)
    {
        this.pad = pad;
        this.naam = naam;
        this.haarKleur = haarKleur;
        this.haarType = haarType;
        this.ogenKleur = ogenKleur;
        this.ornament = ornament;
        this.leeftijd = leeftijd;
        this.huidsKleur = huidsKleur;
        this.geslacht = geslacht;
    }
    
    public boolean isGeraden() {
        return geraden;
    }

    public void setGeraden(boolean geraden) {
        this.geraden = geraden;
    }

    public String getPad() {
        return pad;
    }

    public String getHaarKleur() {
        return haarKleur;
    }

    public String getNaam() {
        return naam;
    }

    public String getHaarType() {
        return haarType;
    }

    public String getOgenKleur() {
        return ogenKleur;
    }

    public String getOrnament() {
        return ornament;
    }

    public String getLeeftijd() {
        return leeftijd;
    }

    public String getHuidsKleur() {
        return huidsKleur;
    }

    public String getGeslacht() {
        return geslacht;
    }
}


Code of Singleton.java

CODE
package wieIsHet;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Singleton extends JFrame implements ActionListener
{
    private static final long serialVersionUID = 1L;
    private static Singleton instance;
    private GraphicsDevice graphicsDevice;
    public JFrame venster;
    private DisplayMode origDisplayMode;
    public MyFrame venster2;
    public Lijst lijst;
    public JButton[] buttons;
    public JButton button1;
    public JButton button2;
    public JButton button3;
    public JButton button4;
    public JButton button5;
    public JButton button6;
    public JButton button7;
    public JButton button8;
    public JButton button9;
    public JButton button10;
    public JButton button11;
    public JButton button12;
    public JButton button13;
    public JButton button14;
    public JButton button15;
    public JButton button16;
    public JButton button17;
    public JButton button18;
    public JButton button19;
    public JButton button20;
    
    protected Singleton()
    {
        super();
        //verkrijg scherminstellingen
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
        FullScreen01(devices[0]);
        origDisplayMode = graphicsDevice.getDisplayMode();
        maakFrame();
        
        
        }
    
    public static Singleton getSingletonInstance()
    {
        if(instance==null)
        {
            instance = new Singleton();
        }
        return instance;
    }
    
    public void FullScreen01(GraphicsDevice graphicsDevice)
    {
        this.graphicsDevice = graphicsDevice;
    }
    
    public void maakFrame()
    {
        venster2 = new MyFrame();
        //minimale resolutie van 800 x 600
        if(origDisplayMode.getHeight() >= 600 && origDisplayMode.getWidth() >= 800)
        {
            if(graphicsDevice.isFullScreenSupported())
            {
                venster2.setUndecorated(true);
                venster2.setResizable(false);
                DisplayMode tempDisplay = new DisplayMode(origDisplayMode.getWidth(), origDisplayMode.getHeight(), 32, 60);
                graphicsDevice.setFullScreenWindow(venster2);
                graphicsDevice.setDisplayMode(tempDisplay);
                venster2.copyData(0, origDisplayMode, venster2, graphicsDevice);
            }
            else
            {
                JOptionPane.showMessageDialog(venster2,
                        "Uw computer of grafische kaart ondersteund geen volledig scherm,\n" +
                        "installeer de laatste nieuwe drivers en probeer opnieuw.",
                        "Error 0!", JOptionPane.OK_OPTION);
                System.exit(0);
            }
        }
        else
        {
            JOptionPane.showMessageDialog(venster2,
                    "Uw resolutie moet minstens 800 x 600 pixels berdragen,\n" +
                    "verhoog uw resolutie en probeer opnieuw.",
                    "Error 1!", JOptionPane.OK_OPTION);
            System.exit(1);
        }
    }

    public void endGame()
    {
        graphicsDevice.setDisplayMode(origDisplayMode);
        System.exit(2);
    }
    
    public void vulTabelButtons()
    {
        buttons = new JButton[20];
        for(int i = 0; i < 20; i++)
        {
            buttons[i] = new JButton();
        }
        buttons[0].add(button1);
        buttons[1].add(button2);
        buttons[2].add(button3);
        buttons[3].add(button4);
        buttons[4].add(button5);
        buttons[5].add(button6);
        buttons[6].add(button7);
        buttons[7].add(button8);
        buttons[8].add(button9);
        buttons[9].add(button10);
        buttons[10].add(button11);
        buttons[11].add(button12);
        buttons[12].add(button13);
        buttons[13].add(button14);
        buttons[14].add(button15);
        buttons[15].add(button16);
        buttons[16].add(button17);
        buttons[17].add(button18);
        buttons[18].add(button19);
        buttons[19].add(button20);
    }
    
    public void newButtons()
    {
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        button4 = new JButton();
        button5 = new JButton();
        button6 = new JButton();
        button7 = new JButton();
        button8 = new JButton();
        button9 = new JButton();
        button10 = new JButton();
        button11 = new JButton();
        button12 = new JButton();
        button13 = new JButton();
        button14 = new JButton();
        button15 = new JButton();
        button16 = new JButton();
        button17 = new JButton();
        button18 = new JButton();
        button19 = new JButton();
        button20 = new JButton();
    }
    
    public void plaatsLabels()
    {
        venster2.getContentPane().setLayout(null);
        //venster2.setLayout(null);
        setLayout(null);
        int stapX = (int) ((origDisplayMode.getWidth() - 300 - 500)/6);
        int stapY = (int) ((origDisplayMode.getHeight() - 200 - 400)/5);
        int cnt = 0;
        newButtons();
        vulTabelButtons();
        
        for(int i = 0; i < 5 && cnt < 20; i++)
        {
            for(int j = 0; j < 4 && cnt < 20; j++)
            {
                Persoon persoon = lijst.gekozenPersonen.get(cnt);
                ImageIcon iconGekozen = new ImageIcon("images/wie is het.png");
                String path = "images/" + persoon.getPad();
                try {
                    BufferedImage foto = ImageIO.read(new File(path));
                    BufferedImage bi = new BufferedImage(85, 85,
                               BufferedImage.TYPE_INT_ARGB);
                    bi.getGraphics().drawImage(foto, 0, 0, 85, 85, null);
                    ImageIcon icon = new ImageIcon(bi);
                    
                    if(persoon.isGeraden())
                    {
                        buttons[cnt].setBorder(null);
                        buttons[cnt].setEnabled(false);
                        buttons[cnt].setIcon(iconGekozen);
                        buttons[cnt].setDisabledIcon(iconGekozen);
                    }
                    else
                    {
                        buttons[cnt].setIcon(icon);
                    }
                    
                    buttons[cnt].setBounds(((150 + 100 * i + stapX + stapX * i)+15/2), ((100 + 100 * j + stapY + stapY * j)+15/2), 85, 85);
                    
                    venster2.getContentPane().add(buttons[cnt]);
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(venster,
                            "Een foto kon niet geladen worden",
                            "Error 5!", JOptionPane.OK_OPTION);
                    System.exit(5);
                }
                cnt++;
            }
        }
        
        for(int i = 0; i < 20; i++)
        {
            buttons[i].requestFocus();
        }
    }
    
    public void geefLijst(Lijst lijst)
    {
        this.lijst = lijst;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    
    static class MyFrame extends JFrame {
        
        private static final long serialVersionUID = 2L;
        
        private Integer keuze = 0;
        private DisplayMode origDisplayMode;
        private Color c;
        private boolean switcher = false;
        private Color c2;
        private GradientPaint gradatie;
        private Image bordje;
        private MyFrame venster;
        private JPanel labels;
        private JFrame frame;
        private GraphicsDevice graphicsDevice;
        
        public void copyData(Integer keuze, DisplayMode origDisplayMode, MyFrame venster, GraphicsDevice graphicsDevice)
        {
            this.keuze = keuze;
            this.origDisplayMode = origDisplayMode;
            this.graphicsDevice = graphicsDevice;
            switcher = true;
            this.venster = venster;
            keuze = 0;
            
            repaint();
        }
        
        public void teken(Integer keuze)
        {
            this.keuze = keuze;
            repaint();
        }
        
        public void paint(Graphics g) {
            if(switcher)
            {
                switcher = true;
                if(keuze == 0)
                {
                    //teken achtergrond
                    c = new Color(186,171,250);
                    g.setColor(c);
                    g.fillRect(0, 0, origDisplayMode.getWidth(), origDisplayMode.getHeight());
                }
                
                if(keuze == 1)
                {
                    c = new Color(186,171,250);
                    g.setColor(c);
                    g.fillRect(0, 0, origDisplayMode.getWidth(), origDisplayMode.getHeight());
                    
                    Graphics2D g2 = (Graphics2D) g;
                    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
                    c2 = new Color(53,32,140);
                    Rectangle2D l = new Rectangle2D.Float(0,0,150,origDisplayMode.getHeight());
                    gradatie = new GradientPaint(0,0,c2,150,0,c);
                    g2.setPaint(gradatie);
                    g2.fill(l);
                    Rectangle2D r = new Rectangle2D.Float((origDisplayMode.getWidth()-150),0, origDisplayMode.getWidth(), origDisplayMode.getHeight());
                    gradatie = new GradientPaint((origDisplayMode.getWidth()-150),0,c,origDisplayMode.getWidth(),0,c2);
                    g2.setPaint(gradatie);
                    g2.fill(r);
                    
                    //middenscherm
                    int stapX = (int) ((origDisplayMode.getWidth() - 300 - 500)/6);
                    int stapY = (int) ((origDisplayMode.getHeight() - 200 - 400)/5);
                    ImageIcon icon = new ImageIcon("images/bordje.png");
                    Image bordje = icon.getImage();
                    for(int i = 0; i < 5; i++)
                    {
                        for(int j = 0; j < 4; j++)
                        {
                            g.drawImage(bordje, (150 + 100 * i + stapX + stapX * i), (100 + 100 * j + stapY + stapY * j), null);
                        }
                    }
                }
                
                if(keuze == 2)
                {
                    
                }
            }
        }
    }
}


User is offlineProfile CardPM
+Quote Post


nick2price

RE: JButton In JButton Problem

2 Jul, 2009 - 03:12 PM
Post #2

D.I.C Lover
*****

Joined: 23 Nov, 2007
Posts: 1,296



Thanked: 119 times
My Contributions
Is it just me or does java written in another language make it difficult to understand? Your doing a lot of extra work here. Your vulTabelButtons method can just be
CODE
  public void vulTabelButtons()
    {
        buttons = new JButton[20];
        for(int i = 0; i < 20; i++)
        {
            buttons[i] = new JButton("Button" + i);
        }
   }


Then the next method u create each button seperately. Put them in an array too. Clean your code up a little first, u will find it a lot easier to follow.
Explain exactly what you mean by button inside of a button? Does that mean i what i think it means?
User is offlineProfile CardPM
+Quote Post

pbl

RE: JButton In JButton Problem

2 Jul, 2009 - 03:14 PM
Post #3

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
You are better to learn how to use arrays
CODE

JButton[] button = new JButton[20];
for(int i = 0; i < button.length; i++)
    button[i] = new JButton();


is a lot better than
CODE

        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        button4 = new JButton();
        button5 = new JButton();
        button6 = new JButton();
        button7 = new JButton();
        button8 = new JButton();
        button9 = new JButton();
        button10 = new JButton();
        button11 = new JButton();
        button12 = new JButton();
        button13 = new JButton();
        button14 = new JButton();
        button15 = new JButton();
        button16 = new JButton();
        button17 = new JButton();
        button18 = new JButton();
        button19 = new JButton();
        button20 = new JButton();



And why would somebody wanted to put a JButton in a JButton ? Have you ever seen a GUI with a button inside a button ?
What do you try to achieve ?
And if you put an ActionListener (JButtons without ActionListener are wuite useless) on your JButtons how would you know which one was clicked ?

This post has been edited by pbl: 2 Jul, 2009 - 03:20 PM
User is offlineProfile CardPM
+Quote Post

tiebird

RE: JButton In JButton Problem

2 Jul, 2009 - 03:21 PM
Post #4

New D.I.C Head
*

Joined: 2 Jul, 2009
Posts: 4


My Contributions
I mean that mostly everything looks fine. The buttons are drawn on the screen and they work like they should do but after a few startups there suddenly appear buttons inside the buttons that are already there. And this is not the intention.

This ONLY happens after about 5 (this number is random) startups.

I am making a game but the actionlisteners are not implemented yet. it's not finished but I want to get rid of this bug otherwise everythings is getting bugier and bugier...

User is offlineProfile CardPM
+Quote Post

pbl

RE: JButton In JButton Problem

2 Jul, 2009 - 03:27 PM
Post #5

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
QUOTE(tiebird @ 2 Jul, 2009 - 03:21 PM) *

I am making a game but the actionlisteners are not implemented yet. it's not finished but I want to get rid of this bug otherwise everythings is getting bugier and bugier...

Rethinking of it for the last 15 minutes, don't see any reason why somebody would want to put a JButton inside another JButton
You have a serious design problem

User is offlineProfile CardPM
+Quote Post

nick2price

RE: JButton In JButton Problem

2 Jul, 2009 - 03:34 PM
Post #6

D.I.C Lover
*****

Joined: 23 Nov, 2007
Posts: 1,296



Thanked: 119 times
My Contributions
I dont know why you would only get that occuring on random occasions, but one thing is for sure and that is you really should try and neaten up your code. I am trying to work out what you are doing here. You have a static inner class which extends a JFrame from within a class which extends a JFrame? Firstly, you should only be having one JFrame per application. Next, some buttons you create from an Array, so you dont. You want to try and be consistent. Make them all from an Array as you are creating a lot of buttons. Also, as pbl says, where are your ActionListeners? Without them, your buttons are not able to do anything useful.
If I was you, In your button methods, I would create a JPanel, create the buttons, add the buttons to your JPanel however you like, and then add this JPanel to your JFrame somewhere else.

@PBL I dont think he is intending to put a JButton inside another JButton. I think its an error he has. But it only happens on random runs.....So cant figure out why this error would occur somtimes and why it doesnt others.....

This post has been edited by nick2price: 2 Jul, 2009 - 03:36 PM
User is offlineProfile CardPM
+Quote Post

pbl

RE: JButton In JButton Problem

2 Jul, 2009 - 04:28 PM
Post #7

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
QUOTE(nick2price @ 2 Jul, 2009 - 03:34 PM) *

@PBL I dont think he is intending to put a JButton inside another JButton. I think its an error he has. But it only happens on random runs.....So cant figure out why this error would occur somtimes and why it doesnt others.....

So this topic title is REALLY misleading

User is offlineProfile CardPM
+Quote Post

toshiro

RE: JButton In JButton Problem

2 Jul, 2009 - 04:34 PM
Post #8

New D.I.C Head
Group Icon

Joined: 27 Jun, 2009
Posts: 11



Thanked: 1 times
Dream Kudos: 25
My Contributions
is each button generating another jbutton somewhere in the panel?
User is offlineProfile CardPM
+Quote Post

pbl

RE: JButton In JButton Problem

2 Jul, 2009 - 06:29 PM
Post #9

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
QUOTE(nick2price @ 2 Jul, 2009 - 03:34 PM) *

@PBL I dont think he is intending to put a JButton inside another JButton. I think its an error he has. But it only happens on random runs.....So cant figure out why this error would occur somtimes and why it doesnt others.....

And what is that ?

CODE

buttons[0].add(button1);
        buttons[1].add(button2);
        buttons[2].add(button3);
        buttons[3].add(button4);
        buttons[4].add(button5);
        buttons[5].add(button6);
        buttons[6].add(button7);
        buttons[7].add(button8);
        buttons[8].add(button9);
        buttons[9].add(button10);
        buttons[10].add(button11);
        buttons[11].add(button12);
        buttons[12].add(button13);
        buttons[13].add(button14);
        buttons[14].add(button15);
        buttons[15].add(button16);
        buttons[16].add(button17);
        buttons[17].add(button18);
        buttons[18].add(button19);
        buttons[19].add(button20);

User is offlineProfile CardPM
+Quote Post

tiebird

RE: JButton In JButton Problem

3 Jul, 2009 - 11:41 AM
Post #10

New D.I.C Head
*

Joined: 2 Jul, 2009
Posts: 4


My Contributions
I'm cleaning my code removed the extends frame from singleton but the problem still exists.

@nick2price: as I said earlier the program isn't finished so there are no ActionListeners nor is the code in the other classes complete. But as long this bug exists I'm not going to code any further.

I'm thinking this problem is created by setting the Layoutmanager to null. I think some problem with the repainting.
Or maybe a problem with the loading of the images but imageIcon uses mediatracker if i'm correct.

I already tried with JPanels but no luck there.

@pbl: why is the topic title misleading? JButton in JButton problem is really my problem... :-p it's really really not what I want :-D


User is offlineProfile CardPM
+Quote Post

tiebird

RE: JButton In JButton Problem

3 Jul, 2009 - 11:49 AM
Post #11

New D.I.C Head
*

Joined: 2 Jul, 2009
Posts: 4


My Contributions
Thanx for your help guys it's working now just used an array to make the buttons and the problem is solved.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:58AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month