• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Converting an Object to int

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, guys!

As a newbie in programming, nevermind Java, I got myself in yet another conundrum, *sigh*.

Ok, here we go.
I'm using a third party GUI library that, much like Swing, creates a table based off of a csv file.

Now I want to set and action for a selected cell when Enter is pressed. There's a method for that.



Now as you can see this method requires two arguments of type int, which are row and column numbers, so it knows which cell to set action to.
How do we get selected col/row? Well, there's a method for that, too!



Now you noticed that it does not return int, instead it returns Objects.
How do I get my col/row numbers, given the above?

There's a variable selectedColumn in that method, but it's private.

There's also a method that returns that variable, but I always get zeroes. Does it have to do something with the fact that variable is private?



Any ideas?

Here's the constructor for Column/Row




 
Sheriff
Posts: 17071
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a Column, isn't its equivalent int stored in the x field? Similarly for Row, I would think it's the y field. Do those fields have getters?
 
oroszCapa Blaszczekievicz
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:For a Column, isn't its equivalent int stored in the x field? Similarly for Row, I would think it's the y field. Do those fields have getters?



Actually, yes! There's a getX() / getY()

But they also return zeroes:(

I tried the following, but now there were no zeroes, nothing at all...

 
Junilu Lacar
Sheriff
Posts: 17071
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are those classes you wrote or did someone else write them?
 
oroszCapa Blaszczekievicz
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Are those classes you wrote or did someone else write them?



Not me, third party library.
I figured it out, though. My approach was wrong altogether, issue closed.

Junilu, thanks for your help!
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you like to post your solution back here, or tell us what you did?  There may be other people with similar problems that would want to know.
 
oroszCapa Blaszczekievicz
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Would you like to post your solution back here, or tell us what you did?  There may be other people with similar problems that would want to know.



Ok, since I'm new to this, I kinda solved it intuitively, I can't even explain what I did, and why my first approach didn't work, so I hope someone could chip in and explain what happened there better than I'm going to do now.

Anyway, basically my understanding is, that when the table is created the selected col/row is 0 0 by default and that's when I call method setCellEnterAction, so basically the method is called once on creation of the table, so why would it be called every time I select a cell in a table during runtime? It won't, right?
So I have to prepare all cells to do smth when enter is pressed beforehand, before runtime.

Hence the nested for loop that I did and it worked like a charm.

for (int rows = 0; rows <= totalRows - 1; rows++ ){
     for (int cols = 0; cols <= totalCols -1; cols++){
         setCellEnterAction (cols, rows, ACTION);
 }
}

So now whichever cell I select, if I press Enter it does what I told it to do. Maybe someone could give me some tips on this whole mess, I'd really appreciate it.
Thank you!
 
Knute Snortum
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only suggestion I'd make is then you write for loops, instead of this:
write this:
It's clearer and more idiomatic.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic