File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes how do I get a JButton string from a linkedlist value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how do I get a JButton string from a linkedlist value?" Watch "how do I get a JButton string from a linkedlist value?" New topic
Author

how do I get a JButton string from a linkedlist value?

Logan Wilson
Greenhorn

Joined: Sep 21, 2012
Posts: 17
I've been working separately on methods to move nodes around in a linkedlist, and I've got that part all figured out. BUT, now that I've started incorporating all my code into my GUI, I'm lost trying to populate my JButtons with the String values from my linked list nodes. I'm winding up with nulls in their spot. Any idea what I could do?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Well, there's a lot of code there. And all we know is that somewhere in that code, you have a null value appearing somehow and you don't think that should be happening. So could you be a little bit more specific about just where in that code you see the null value appearing?
Logan Wilson
Greenhorn

Joined: Sep 21, 2012
Posts: 17
Paul Clapham wrote:Well, there's a lot of code there. And all we know is that somewhere in that code, you have a null value appearing somehow and you don't think that should be happening. So could you be a little bit more specific about just where in that code you see the null value appearing?


Shoot, sorry! I'm having the difficulty in this section.


and here:


I'm trying to get the value (String name) out of my node and populating into the JButton. But, I haven't been able to figure out how to get and set those values. :(
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

But you don't even have any code there (in the first fragment) which tries to get data out of the nodes you created in the second fragment.

So is that your question? What code you should write to get data?

If you have a ProbNode object, then you just call its getName() method to get the string which was stored in the node. Are you asking how to get one of those? My guess would be that you have a ProbMgr object handy somewhere, and you can get the nodes from that. But that's just a guess, though. I don't know what this list is for or why you are using it. If it were me I would just read the text file and generate the buttons from that, rather than mess about with an intermediate step where you stash the data somewhere and then read it back.
Logan Wilson
Greenhorn

Joined: Sep 21, 2012
Posts: 17
Paul Clapham wrote:But you don't even have any code there (in the first fragment) which tries to get data out of the nodes you created in the second fragment.

So is that your question? What code you should write to get data?

If you have a ProbNode object, then you just call its getName() method to get the string which was stored in the node. Are you asking how to get one of those? My guess would be that you have a ProbMgr object handy somewhere, and you can get the nodes from that. But that's just a guess, though. I don't know what this list is for or why you are using it. If it were me I would just read the text file and generate the buttons from that, rather than mess about with an intermediate step where you stash the data somewhere and then read it back.


There's no code in that first section because I got extremely frustrated and deleted all the stuff that didn't work for me. I have a getName() method in my Node class that just returns the String name. I also have a method in my manager class to try and find the value and assign it to an array, the getName():



I'm lost trying to figure out how to place the String value in the JButton. Eclipse keeps warning me I have invalid type comparing them if I try to directly place the Node.name element, or even the Node.name.toString(), in there. I've tried multiple ways to fit a square peg into a round hole.

My question is: Is there a way to directly place those elements into the JButton, or should I assign my link list node Strings into an array, and then just repopulate the buttons with the array?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Logan Wilson wrote:I'm lost trying to figure out how to place the String value in the JButton. Eclipse keeps warning me I have invalid type comparing them if I try to directly place the Node.name element, or even the Node.name.toString(), in there. I've tried multiple ways to fit a square peg into a round hole.


You call the JButton's setText(String) method and pass it a String value. I can't comment on any of that because you didn't post any of your proposed code.

My question is: Is there a way to directly place those elements into the JButton, or should I assign my link list node Strings into an array, and then just repopulate the buttons with the array?


What elements? My problem here is that I don't know what you mean by an "element". My guess is that all you have to do is to get a String out of your list and assign it to the JButton using its setText() method, but the whole thing seems like an excessively complicated way of doing something which should be quite simple.
Logan Wilson
Greenhorn

Joined: Sep 21, 2012
Posts: 17
Paul Clapham wrote:
What elements? My problem here is that I don't know what you mean by an "element".


Sorry, by element, I meant just the String name that's in all my nodes.

My guess is that all you have to do is to get a String out of your list and assign it to the JButton using its setText() method, but the whole thing seems like an excessively complicated way of doing something which should be quite simple.


Your guess is absolutely correct, I just have to get the String out of the list and populate the list of buttons. It was super easy with Arrays, I just had to do an insertion sort and then repopulate the list. This has quickly turned into an exercise in frustration for me. You're absolutely correct with how this is more complicated than it seems. Hell, it's probably me making it more complicated.

I built all these different methods where it worked reading the file and printing out the results to the console, but I tried importing my methods into the SwingGui. I'm wondering if something got lost along the way, and the file isn't getting read and populated.

One of the things I tried was a JButton.setText(node.readList), but I'm wondering if I should alter the code to only read and output one line at a time? Since it appears to be trying to load all the Strings into an i-th location.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Okay. I'm looking at that readList() method. Apparently that's supposed to be there for the purpose of reading from a text file and adding the lines from it into a list. As I already said, if it were me I would just skip the whole list business and create the JButtons right there. But at any rate that's all moot because you aren't actually calling that method anywhere.
Logan Wilson
Greenhorn

Joined: Sep 21, 2012
Posts: 17
Paul Clapham wrote:Okay. I'm looking at that readList() method. Apparently that's supposed to be there for the purpose of reading from a text file and adding the lines from it into a list. As I already said, if it were me I would just skip the whole list business and create the JButtons right there. But at any rate that's all moot because you aren't actually calling that method anywhere.


Got it! Thanks!!



I had to over complicate it. Now, to get the massive list of red compiler errors to disappear while pushing them. :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how do I get a JButton string from a linkedlist value?