• Post Reply Bookmark Topic Watch Topic
  • New Topic

Where to place constructor?  RSS feed

 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making a Dice Roll GUI and I have most of it down and I only need this constructor to work for the program to work (I think). I don't know where to place the constructor, I tried placing it around the RollButton class but it still didn't work and gave me


Here's my constructor:


Here's my code:
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create a constructor in class RollButton that takes a JPanel as an argument, then at the place where you create a new RollButton object (line 32), you must pass a JPanel object:
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't need an explicit constructor. The static panel object will be in-scope for action listener.

You do need to fix where you you create a new local panel object in the main method however -- you should reference the static one, not create a new one.

 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:You shouldn't need an explicit constructor. The static panel object will be in-scope for action listener.

You do need to fix where you you create a new local panel object in the main method however -- you should reference the static one, not create a new one.



Alright, I took out the extra JPanel panel = new JPanel(); and kept with the static one.
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:When you create a constructor in class RollButton that takes a JPanel as an argument, then at the place where you create a new RollButton object (line 32), you must pass a JPanel object:


When I do pass the JPanel


I get these errors


 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ronald chu wrote:Alright, I took out the extra JPanel panel = new JPanel(); and kept with the static one.

If you did that, then you can either remove your constructor , and leave button.addActionListener(new RollButton()) as-is, or follow Jesper's advice and include the reference to the panel: button.addActionListener(new RollButton(panel))
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:
ronald chu wrote:Alright, I took out the extra JPanel panel = new JPanel(); and kept with the static one.

If you did that, then you can either remove your constructor , and leave button.addActionListener(new RollButton()) as-is, or follow Jesper's advice and include the reference to the panel: button.addActionListener(new RollButton(panel))


Okay so I put in button.addActionListener(new RollButton(panel));, it compiles fine but when I run it, I get

 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you delete JPanel panel = new JPanel(); all-together rather than referencing the static object like this: panel = new JPanel();
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:Did you delete JPanel panel = new JPanel(); all-together rather than referencing the static object like this: panel = new JPanel();


Yeah I deleted JPanel panel = new JPanel(); and using the static object instead
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ronald chu wrote:Yeah I deleted JPanel panel = new JPanel(); and using the static object instead

Then you you do not have a JPanel instance (and resulting in a NPE). You still will need panel = new JPanel(); in your main method.
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:
ronald chu wrote:Yeah I deleted JPanel panel = new JPanel(); and using the static object instead

Then you you do not have a JPanel instance (and resulting in a NPE). You still will need panel = new JPanel(); in your main method.


I just add the panel = new JPanel(); in my main method now it gives me another NPE:

 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to post your updated code.
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:I think you need to post your updated code.


Updated code:

 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 45 in the constructor looks like a problem. You are trying to add a label to the panel, but the value is null. You can probably just remove lines 45 and 46.
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:Line 45 in the constructor looks like a problem. You are trying to add a label to the panel, but the value is null. You can probably just remove lines 45 and 46.


I removed those lines and when I run the program the button is there, when I press it, nothing happens. :\
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess that you have something wrong with your image files - probably the wrong path.

You could make the label show the value of the roll as well as the image: JLabel diceRoll = new JLabel("" + roll, dice, JLabel.CENTER); to help debug the problem.
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:I would guess that you have something wrong with your image files - probably the wrong path.

You could make the label show the value of the roll as well as the image: JLabel diceRoll = new JLabel("" + roll, dice, JLabel.CENTER); to help debug the problem.


Okay I have images popping up on the GUI(Thanks!) but they stack on each other and don't disappear also only 2 + 5 are only appearing (I put the wrong names and put .png instead of .jpeg)
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ronald chu wrote:Okay I have images popping up on the GUI(Thanks!) but they stack on each other and don't disappear

You are adding an additional label to the panel every time you roll the die. If you want to only see one image - the last roll, then you might want to add the label to the panel once (where you add the button to the panel?), and then update the label's icon with each roll.
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:
ronald chu wrote:Okay I have images popping up on the GUI(Thanks!) but they stack on each other and don't disappear

You are adding an additional label to the panel every time you roll the die. If you want to only see one image - the last roll, then you might want to add the label to the panel once (where you add the button to the panel?), and then update the label's icon with each roll.


Ok I changed my code but when I run it nothing happens when I press the "Roll" button

 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still after all these posts you have a local JLabel which you're setting instead of the global one?
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Still after all these posts you have a local JLabel which you're setting instead of the global one?


If I take out diceRoll = new JLabel(); I get NPE.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ronald chu wrote:Ok I changed my code but when I run it nothing happens when I press the "Roll" button

You should be adding the label to the same panel as the button. Compare how you add diceRoll and button.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!