Jump to content

How to take user input from a textfield of one form and use it in another? (java)

Shammikit

 

i have a form called screen1 which is the first form.it has a text field called username_TextField. i want to take the user input from this textfield and use it for a sql statement in my second form.the code below is the one i have written but it gives me a error saying: Non-static variable username_TextField cannot be referenced from a static context. what could be the problem.please help.

String username = screen1.username_TextField.getText();   

 

Link to comment
Share on other sites

Link to post
Share on other sites

29 minutes ago, ShadySocks said:

my guess is that screen1 cannot be static?

i have changed the jframe and all other elements to public static in screen 1. i forgot to mention,the code that i mentioned before is inside a jbutton which is private.is this a problem?

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

You just need a reference to the other form and for there to be something publicly exposed: Form A has a public property and an instance of A is passed into Form B. Form B can then access Form A's publicly exposed property.

 

This kind of architecture is highly questionable however as you should instead be thinking in terms of Model View ViewModel (MVVM) or Model View Controller (MVC) with the use of various abstraction patterns to decouple your view from your business logic. In other words you don't want to be mixing view and business logic, it is bad design.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Look up "Getters and Setters". Should do what you need.

Intel i7 5820K (4.5 GHz) | MSI X99A MPower | 32 GB Kingston HyperX Fury 2666MHz | Asus RoG STRIX GTX 1080ti OC | Samsung 951 m.2 nVME 512GB | Crucial MX200 1000GB | Western Digital Caviar Black 2000GB | Noctua NH-D15 | Fractal Define R5 | Seasonic 860 Platinum | Logitech G910 | Sennheiser 599 | Blue Yeti | Logitech G502

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to comment
Share on other sites

Link to post
Share on other sites

On 18/12/2016 at 1:25 PM, Nuluvius said:

You just need a reference to the other form and for there to be something publicly exposed: Form A has a public property and an instance of A is passed into Form B. Form B can then access Form A's publicly exposed property.

 

This kind of architecture is highly questionable however as you should instead be thinking in terms of Model View ViewModel (MVVM) or Model View Controller (MVC) with the use of various abstraction patterns to decouple your view from your business logic. In other words you don't want to be mixing view and business logic, it is bad design.

+1 for using a pattern like MVC (or MVVM) for this.

The simplest way to think about it as a starting point is how is the user switching between form 1 and form2, is there a button he clicked? In that case you are most probably calling a method that does the switch, can you give an extra parameter for that method with the values you need? When you start passing the values you need from form to form, you could not think of a class that does that communication, which gets you closer to a controller in the MVC, the data you need to send between forms is the model part.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×