• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance + Swing questions  RSS feed

 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two questions that regard the same piece of code. First the following:



In the first example, the call to super() should be the first statement. But isn't it already? I can get the error fixed by removing the "int" from the line above. But I would like to pass a value to the code that calls the method, and that's what the "int" is for. Can I do something smarter?




In the second example, the JComboBox returns a data type that cannot be cast to "int" (or "String" - I tried). Yet I would like to use the value in a calculation with a string or and int. How can I do this?

Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your first piece of code, loadLevel() is a method, not a constructor, because you specified a return type, int.

You cannot call super(...) in a regular method. Constructors do not have a return type. A constructor does not return anything.

By removing the return type int you make loadLevel() a constructor, as it ought to be.

A JComboBox is not is not an int, so you cannot cast it to an int. Casting is not how you get the value that the user selected out of the combo box.

Trying to get the selected number right after creating the combo box is also the wrong place to do this. At that point, the combo box has not been displayed on screen at all and the user hasn't had the chance to see and use it. you should get the selected value at some other point in the program, for example in a handler for the "OK" button that you might also have in your window.
 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, loadLevel() is a method, not a constructor., which also do not have a return type. But how can I then return a value from this constructor to the code that calls it?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a method whose name is the same as the name of the class. This is a recipe for confusion (perhaps leading you to think that a constructor returns a value). And it doesn't help that you went non-standard and have a class whose name starts with a lower-case character.

So before you continue I'd suggest two things. First rename the class so that its name starts with a capital letter. If you look around you'll find that class names are capitalized, starting with String and going onwards. And second, change the method so its name isn't the same as the name of the class, or change it so it's a constructor. Whichever you meant to do in the first place.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Evert DeBoer wrote:In the second example, the JComboBox returns a data type that cannot be cast to "int" (or "String" - I tried). Yet I would like to use the value in a calculation with a string or and int. How can I do this?


No, the JComboBox doesn't return a data type. Constructors don't return anything. However "new JComboBox(...)" creates a JComboBox object... that's what the new operator does. I don't know what kind of calculation you want to do with a JComboBox and an int, but probably that wasn't what you thought the calculation would be all about. So what exactly did you expect to be calculating?
 
Evert DeBoer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
No, the JComboBox doesn't return a data type. Constructors don't return anything. However "new JComboBox(...)" creates a JComboBox object... that's what the new operator does. I don't know what kind of calculation you want to do with a JComboBox and an int, but probably that wasn't what you thought the calculation would be all about. So what exactly did you expect to be calculating?


Welll, basically I'd like to pick up the value the user has selected and then do a simple calculation with it. For example, if the user has selected "2", I'd like two load "level2".
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, first you want to create the JComboBox and incorporate it into your GUI. Just creating one doesn't cause it to appear on the screen.

Then if you want to know what entry the user selected, there's a method which tells you that. I suggest you read the tutorial to find out more about normal ways of handling JComboBox objects: How to Use Combo Boxes. I strongly suggest you download the example code and look at it to find out more about the usual way to write Swing applications.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!