Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble changing the text of a jlabel from another class  RSS feed

 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so for the past 3 days i have been going nuts, heres my goal
create a dnd (dungeons and dragons) character creator and back ground generator, have it display and run on a gui, to start with i decided to creator the gui as i go so i can see the progress, first i tried eclipse and windows builder, well after 1 day of reinstalling windows builder in about 5 different ways from multiple guides and sometimes getting it to partly work i ditched that idea

After doing some more research i learnt the intelij idea was a better ide (to some) and has a built in gui editor...fantastic!

after a day or so of reading, googling and playing about i have figured out how to get the main program to display the gui
heres what i have:



MainWindow.form is also present and is a wysiwyg editor inside Idea.

and the main class that runs on start up is



now this works fine and the gui displays when the program is run

but i cant change the text of any of the jlabels or textfield

if i try to do it in the main class i need to change them from private to public static but then in the form builder it says "cannot bind to static field *name of field*"
but if i take static off then i cant change its value!

from what i understand this is because the window is an instance, so how to i change the value in that instance?

i could put all my code in the one class, the one that creates the gui, but i really want it seperate, i would like the gui to update as the code runs...it really is driving me mad.

so how do i do it, how do i change the contents of jlabels on the fly, read the contents of text boxes on the fly etc, help


edit 1:

i tried adding a getter setter

public void setCharFirst(JLabel charFirst) {
this.charFirst = charFirst;
}
in the MainWindow.class

then i tried to set it from my main function
MainWindow.setCharFirst("test");

but it cant access it as my main function is static and it is not now can i make it static arghhhhhhhhhhhhhhhh
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The setter should not assign a JLabel object - it should take a String as parameter, and then set that as title of the appropriate JLabel.

MainWindow.main(null)

Only the JVM launcher should invoke he main method. All the main method should do is to allocate an instance of the class it's part of (and then maybe call a method on that object to get things going). Other classes can then instantiate the class as needed as well. More generally, you should have few "static"s in your code, but rather pass around object instances to invoke methods on them.
 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second main was created by the gui builder of idea. All values of the labels are specified in the .form file which is an X ml so no way to have them as a string ... well as a static string.


I have always used public static String as this allows to get and set the string from any class etc an incredibly useful thing.

I'm guessing from your reply that what I'm doing is not the right way but you don't mention what is or how to fix/workaround the issue which is what I really need at the moment, I have tried to use the java docs but thier so badly written from a novices view that they merely make me give up rather then try again

So how in idea do I change the values of jlebel or have them link to a string like jlabel1 etc (I would ofcourse use more descriptive titles) or how do I use the .settext function from a static

If I should not be setting my strings and ints to static how should I be accessing them from other classes (really don't want to be using just one class for the while program)

As I say I'm completely stuck

Note this reply was tapped out on my phone as I'm at work so please ignore the hurried tone and auto correct errors.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second main was created by the gui builder of idea. All values of the labels are specified in the .form file which is an X ml so no way to have them as a string ... well as a static string.

Not sure how to respond to that, as I don't know how this form builder works, or what the overall flow of control is. I'll state again that I think it's bad design to pass around JLabel objects just to change the title of them.

I have always used public static String as this allows to get and set the string from any class etc an incredibly useful thing.

Maybe so, but not good design. It encourages calls from wherever to wherever, which often results in spaghetti code rather than anything structured. Also makes maintenance much harder.

I'm guessing from your reply that what I'm doing is not the right way but you don't mention what is or how to fix/workaround the issue

Actually, I did: avoid the statics by passing around object instances to where they are needed. Not GUI objects like Swing components - objects of your own design. To know which objects you need, and what data and what functionalities they would encapsulate, you need to do a proper OO design first, though, before you go ahead with implementation.

really don't want to be using just one class for the while program

Indeed, that would be much worse, and certainly not what OO is all about.
 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so i went back and watched some vids on the basics of instances etc, one particular one helped me a lot and actually allowed me to grasp a concept i had not understood in all the time of playing around with java (on and off for a year or so, mainly off)

so changed the gui code so its function is called start() instead of main, it was called main as the helper built into intellij called it main and i assumed it had to be so.

i then added in a way to call the gui and update it (note im aware that at the moment the code can only be called once as repeated calls would just make a new instance! but i didnt get as far as to figure that bit out)

so first i created a new instance, an object which was referenced with mainwindow
MainWindow mainwindow = new MainWindow();

then i told it to make the gui
mainwindow.start();

all good so far, the gui starts

ok so next lets get one of the labels to change
mainwindow.charSecond.setText("test");

code shows no errors but the label never changes its text

i tried adding
mainwindow.getMainWindow().repaint();
but nope it refuses to change

im trying to understand this but when i was at school i was taught how to use BASIC to make a turtle draw a pattern, it was a lesson or two and that was it, i grewup with a spectrum, then i moved on to an amiga and easy amos, im used to a flow a to b to c to d with the occasional GOTO g and GOSUB s. perhaps thats making playing around with java harder, i dont know but its really frustrating when i look at it and dont see anything wrong but it just wont work

edit 1 ****************

the code for MainWindow.class


the code thats in the MainWindow.form (in intellij its editable as a wysiwyg ONLY i had to load it into an external editor to get this)


edit 2**************
hmmm perhaps ill retry eclipse maybe i can get it to have swing this time
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds as if you're still a bit weak on fundamental OO concepts, so I'm afraid my best advice is to work through some introductions on that, maybe at first without any GUI. The Oracle Java Tutorial is a starting point: http://docs.oracle.com/javase/tutorial/, particularly the "Trails Covering the Basics".

Personally, I'm also not a fan of GUI builders, certainly not for (Java and/or Swing) beginners, as they hide a lot of stuff that you really need to understand. I think you're currently running into some of that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!