Results 1 to 7 of 7
- 07-29-2012, 06:34 PM #1
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Java Swing - JLabel setText() doesn't work
Problem has been solved, it was a typo!
Hello!
I'm very new to GUI-coding, and I'm trying to make a GUI to a crawler I've made earlier.
I have the following GUI-code that runs just fine:
On line 18, the method addSBWineToList is run, which parses information from a website and saves it as a wine object in an ArrayList.Java Code:public WineKeeperGUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); controller = new Controller(); JButton addNewWineBtn = new JButton("L\u00E4gg till nytt vin"); addNewWineBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //HERE GOES CODE FOR ADD NEW WINE artnr = Integer.parseInt(txtArtikelnummer.getText()); sartnr = Integer.toString(artnr); lblSBnr.setText(sartnr); try { controller.addSBWineToList(artnr); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } lblName.setText(controller.getName()); } });
To try whether or not my code is working, I want the JLabel lblName to be set to what the wine name is. To do so, I use the code on line 23.
This is the code in the controller:
I have made sure that it actually saves the wine-object in the ArrayList, because using an index of "1" when trying getNameInformation, I get an index out of bounds.Java Code:public void addSBWineToList(int arntr) throws IOException{ SBWine wine = new SBWine(artnr); wineList.add(wine); } public String getName(){ return wineList.get(0).getNameInformation(); }
The code for getNameInformation:
Java Code:public String getNameInformation(){ String nameInfo = this.name + " " + this.extName; return nameInfo; };
So what is the big problem? Well, my program runs fine so far without any exceptions. Though, the name is never set to anything except an empty string it seems. Can anyone spot any errors in my code or in my way of executing what I want to achieve.
Thank you!Last edited by Zyril; 07-29-2012 at 08:02 PM.
- 07-29-2012, 06:40 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: Java Swing - JLabel setText() doesn't work
Try this for a little debugging:
Do you see that String? If so, name and extName are probably empty.Java Code:public String getNameInformation() { String nameInfo = "TEST:"+this.name + " " + this.extName; return nameInfo; }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-29-2012, 06:56 PM #3
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Java Swing - JLabel setText() doesn't work
Thank you Jos, for the quick reply and input! I was just thinking about how I the most easy way could debug and test this, since a simple println wouldn't work! I tried your suggested method and you were spot on the problem! The strings are both empty, so now I have to look into them!
Thanks alot,
Z!
- 07-29-2012, 07:11 PM #4
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Java Swing - JLabel setText() doesn't work
Alright, quick update since I found the trouble making code!
At line 14, it appears that my integer artnr doesn't get the value of the JTextfield. Is the code really correct here, or am I missing an actionlistener or something?Java Code:public WineKeeperGUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); controller = new Controller(); JButton addNewWineBtn = new JButton("L\u00E4gg till nytt vin"); addNewWineBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //HERE GOES CODE FOR ADD NEW WINE artnr = Integer.parseInt(txtArtikelnummer.getText()); sartnr = Integer.toString(artnr); lblSBnr.setText(sartnr); try { controller.addSBWineToList(artnr); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } lblName.setText(controller.getName()); } });
The result is that at line 18, the value of artnr is 0, that renders an empty wine-object for me.
Thanks,
Z!Last edited by Zyril; 07-29-2012 at 07:13 PM.
- 07-29-2012, 07:56 PM #5
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Java Swing - JLabel setText() doesn't work
Alright, I have debugged it over and over, and decided just to start fresh and look through every line of code of the involving methods.
Lesson learned, check for typos first... I had the parameter arntr instead of artnr in one crucial line of code.
Do you guys have any ideas on how to stop typos like this from happening in the first place? Use the auto-fills from Eclipse perhaps more often..?
Thanks,
Z!
- 07-29-2012, 08:51 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: Java Swing - JLabel setText() doesn't work
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-29-2012, 09:51 PM #7
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Java Swing - JLabel setText() doesn't work
Yep!
The thing I did wrong here was that I had useless code that kinda made it harder to notice the error.
If I hadn't had the useless code in line 3, the compiler would have complained about line 16.Java Code:public class Controller { int artnr; ArrayList<SBWine> wineList = new ArrayList<SBWine>(); public Controller(){ } /** * System operation that GUI calls to create new SB-wine and add to winelist. * @param arntr * @throws IOException */ public void addSBWineToList(int arntr) throws IOException{ SBWine wine = new SBWine(artnr); wineList.add(wine); }
But, since this is the newbie forum I can gladly say I've learned a lesson! =)
Similar Threads
-
setText() doesn't work
By Ratsha in forum New To JavaReplies: 9Last Post: 03-22-2012, 02:37 PM -
JAVA RMI - Can't create server, because casting to it's interface doesn't work?
By arcelivez in forum NetworkingReplies: 9Last Post: 02-26-2012, 09:13 PM -
webstart link some time doesn't work after Java upgrade
By Richard Gillespie in forum Advanced JavaReplies: 0Last Post: 12-21-2010, 04:01 PM -
problem with JLabel.setText();
By nonabhai in forum AWT / SwingReplies: 5Last Post: 10-09-2010, 04:44 AM -
If statement and JLabel.setText() problem
By peterhabe in forum New To JavaReplies: 5Last Post: 07-25-2010, 10:11 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks