• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

use user's input in JTextField in a class as a label for JLabel, how to do that?

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JTextField's class is different from the JLabel, how to do that?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tawi Oei wrote:The JTextField's class is different from the JLabel, how to do that?



Of course a JTextField is different from a JLabel.

For your question... first you need to get the user input from the JTextField then create the JLabel.

 
Tawi Oei
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:

Tawi Oei wrote:The JTextField's class is different from the JLabel, how to do that?



Of course a JTextField is different from a JLabel.

For your question... first you need to get the user input from the JTextField then create the JLabel.



If the JLabel's class is different from the JTextField's class, can I do that thing either?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this the same problem as in your other thread?
https://coderanch.com/t/602226/java/java/setText

if so, you need to pass a reference of the class or class label when creating the class with the textfield
here's a very simple demo (would be better with 'setters', but simpler like this)


run it, click the button, change the textfield's text, click the button again
 
Tawi Oei
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:is this the same problem as in your other thread?
https://coderanch.com/t/602226/java/java/setText

if so, you need to pass a reference of the class or class label when creating the class with the textfield
here's a very simple demo (would be better with 'setters', but simpler like this)


run it, click the button, change the textfield's text, click the button again



Err, similar but I use different code I guest. The problem is I want to put the JTextField in A class but want to access and put the user's input in JTextField in A class from the B class. I want to put the user's input in JTextField in class A as a JLabel's label and the JLabel is in B class, how to do that?
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know it's kind of hard visualizing what you really want.

A JLabel in a JTextField? A JTextField in a JLabel? JLabel accessing JTextField text? JTextField accessing JLabel text?

Even if these 2 components are in different classes, you will need an instance of one of them to get/set the other.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I want to put the user's input in JTextField in class A as a JLabel's label and the JLabel is in B class, how to do that?

in my demo I have the textfield in B() and the label in A(), and you want to know how to do it so that
the textfield is in A() and the label in B() - you can't be serious.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tawi Oei wrote:The JTextField's class is different from the JLabel, how to do that?


Judging by the replies to this thread, I suspect that everyone who's trying to help you is having the same problem as me:

What do you want to do?

You're telling us how you want to do it, and that is bad design. Tell us what you want, and maybe we can offer some advice; if you've already decided that you MUST do it a particular way then I, for one, can't help you.

Winston
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:> I want to put the user's input in JTextField in class A as a JLabel's label and the JLabel is in B class, how to do that?

in my demo I have the textfield in B() and the label in A(), and you want to know how to do it so that
the textfield is in A() and the label in B() - you can't be serious.



Your example just have the class reversed. But I don't think that's what the OP want.
 
Tawi Oei
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:

Michael Dunn wrote:> I want to put the user's input in JTextField in class A as a JLabel's label and the JLabel is in B class, how to do that?

in my demo I have the textfield in B() and the label in A(), and you want to know how to do it so that
the textfield is in A() and the label in B() - you can't be serious.



Your example just have the class reversed. But I don't think that's what the OP want.



Yes, you don't understand the thing that I want. I want to access the user's input at JTextField tf in B class so the label of a JLabel in A class will be the same with user's input in tf, how to do that?

Sorry no hard feeling. I did not mean to insult you.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I want to access the user's input at JTextField tf in B class so the label of a JLabel in A class will be the same with user's input in tf

what triggers the label getting the textfield text? (in my demo it was clicking a button)

probably will save a lot of time if you posted your own demo (similar *basic* code as in mine)
of exactly what you have and the steps to follow of what you want.
 
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tawi Oei wrote:
Yes, you don't understand the thing that I want. I want to access the user's input at JTextField tf in B class so the label of a JLabel in A class will be the same with user's input in tf, how to do that?



You just treat the JTExtField in B like a field and make a getter for it like public JTextField getTf() { return tf; }. You need a reference to a B class object in class A object and do myJLabel.setText(instanceOfB.getTf().getText).
 
Tawi Oei
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give me the example? Or can you give me a new revised code that contain the technique to do that? After I click Lanjutkan button, I want the next label in the next frame to have the same label with user's input in the current frame. I cannot put my code here because the modem is not with me. I am in a a public internet and not carrying any flash discs that contain the code. However, the code is similar with the code in http://www.tawijakarta.blogspot.com in Java Program II article. If I want to do that to some JTextFields and some JLabels (more than one), what will I need to do?

Can I do this?

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I am in a a public internet and not carrying any flash discs that contain the code.

we don't want 'the code' - we just want a sample program = 20 to 30 lines only.

you now mention having 2 frames (and that's not good), so we have to continue guessing.
are the 2 frames open simultaneously? does one open the other? how are they related?

generally the quality of the help given is directly proportional to the effort put into the problem description,
and, so far, I've seen very little effort.
 
Mack Wilmot
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are getting it. Yes, you can pass the reference for A into B through the B constructor, just move the parameter to the constructor, you have it in the declaration, that is wrong.

Pass it wherever you create the new B or you could also have a function in B like public void setAObj(A obj) { aobj = obj; } to set a reference to A within B. Either way works.
 
Tawi Oei
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, actually, the ProgramTesisBaru will open first and after that FrameBaru will open. How to connect the JTextField in ProgramTesisBaru to JLabel in FrameBaru so the JLabel in FrameBaru's label is the same with the user input in JTextField in ProgramTesisBaru? Another question is how to avoid opening multiple windows?
 
Mack Wilmot
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be able to have a Listener that is notified when the JTextField changes some way and updates the JLabel... like whenever the user types anything in the JTextField would notify the Listner as apposed to clicking a button. I have never done this, but I know it is possible.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
once again there's too little information for you to get the best answer (and possibly not even the right answer)

you're saying you want the textfield input displayed as the label's text - simple enough,
but when the 2nd frame opens what happens? does the 1st frame close?
if not, can changes be made to the textfield input? do you want the label updated with the changes?
what triggers the textfield input updating the label?
or is it once only? i.e. when the 2nd frame opens, the label's text is set.

if just a once only, when you open the 2nd frame (presumably from the 1st frame), you just create the label
= new JLabel(ProgramTesisBaru.this.textfield.getText());

if changes can be made to user input, and you want these changes to reflect in the label's text, possibly
the simplest way would be to change the label to a textfield (uneditable, no border) so it looks like a label,
then you set their models the same.

> Another question is how to avoid opening multiple windows?

it's not multiple windows that's bad, just multiple frames. Generally the 2nd frame would be a modal JDialog.
depending on the application, a CardLayout might suit (as in installation wizards - next/previous to display different 'windows')
 
Tawi Oei
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here are the error messages:

Error Messages:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at programtesisbaru.FrameBaru.<init>(ProgramTesisBaru.java:636)
at programtesisbaru.ProgramTesisBaru$1.kontinu(ProgramTesisBaru.java:123)
at programtesisbaru.ProgramTesisBaru$1.actionPerformed(ProgramTesisBaru.java:120)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
BUILD SUCCESSFUL (total time: 14 minutes 57 seconds)

So what shold I do?
Answers in https://coderanch.com/t/605700/java/java/user-input-JTextField-class-label only make me to get NPE after I modify them. What is wrong?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the end of what prints out when attempting to compile your mess:

98 errors
Press any key to continue...


so, how could you possibly get a NPE from the posted code?
 
Mack Wilmot
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tawi Oei wrote:

Here are the error messages:

Error Messages:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at programtesisbaru.FrameBaru.<init>(ProgramTesisBaru.java:636)
at programtesisbaru.ProgramTesisBaru$1.kontinu(ProgramTesisBaru.java:123)
at programtesisbaru.ProgramTesisBaru$1.actionPerformed(ProgramTesisBaru.java:120)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
BUILD SUCCESSFUL (total time: 14 minutes 57 seconds)

So what shold I do?



Can you describe what you are doing on line 636? I know, but do you?
What do you think a NullPointerException means?
 
Tawi Oei
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 636, I tried to assign the content of one variable to other variable, so? What is the mistake? Can you tell me how to fix it? What should I do? It is similar with the example but I modify them because I cannot access the ProgramTesisBaru variable from other class directly. Is that wrong? What should I do?
 
Mack Wilmot
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not assigning a variable to anther variable. You are attempting to invoke a method in an object and supplying a String parameter for the method. You are attempting to use an object.

http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html
 
Tawi Oei
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what should I do? How to fix these codes?
 
Mack Wilmot
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have no object, the variable for the object (JLabel) holds "null", there was never an object created.

All you have to do is create the object and it works. That is not the only object you are trying to use in your code that does not exist.

http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
 
Tawi Oei
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What objects do you thing are null? What kind of change do you think I have to do to the codes? Why are they null? Can you expain in details? Will I need to call an exception if the user makes no input to every available JTextField?
 
Mack Wilmot
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tawi Oei wrote:What objects do you thing are null? What kind of change do you think I have to do to the codes? Why are they null? Can you expain in details? Will I need to call an exception if the user makes no input to every available JTextField?



Line 636 has nothing to do with a JTextField, it is a JLabel you are trying to use. The JLabel does not exist because your code never created it. The variable that is supposed to point to a JLabel points to null, because there was never an object created. I can't be anymore detailed than that. The ball is in your court now.
 
Tawi Oei
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you see line 123, you will be able to see that I try to access the JTextField with this technique because I cannot access the JTextField directly from FrameBaru class. Later, I assign the value of the JTextField to lblKriteria1 so lblKriteria1 is not null, I create the lblKriteria1 before. Please see line 603. If that technique is wrong, what technique will be a correct technique?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note that I split a couple of very long lines, as they were causing the forum layout to be screwed up. See Don't write long lines.
 
Mack Wilmot
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tawi Oei wrote:If you see line 123, you will be able to see that I try to access the JTextField with this technique because I cannot access the JTextField directly from FrameBaru class. Later, I assign the value of the JTextField to lblKriteria1 so lblKriteria1 is not null, I create the lblKriteria1 before. Please see line 603. If that technique is wrong, what technique will be a correct technique?



603 private JLabel labelLuarKriteria1;

Line 603 declares a variable name of type JLabel. It does NOT create a JLabel. Like I said before, you have no JLabel. Here is this link again:

http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic