How to clear a text edit field that belongs from another class - java

I am trying to clear the edit text field that belongs to another class from another class. How to do this? Example i want to clear the edit text username which is in the Login class from the registration class. I am new to android

Clear edittext values by calling et.settext(""). Just after successfully login from the same Activity..
This way u dont need to do it from diff class...

Try this..
public class Registration{
public Login l;
public void doEdit(){
l = new Login();
l.setUserName(""); // setUserName is the Method in Login class to set Username.
}
}

Login myLogin = new Login;
myLogin.clearLogin();
class Login
{
public clearLogin()
{
textField.setText("");
}
}

Related

create new java class in spring application

this is my first spring application (maven project) which is supposed to allow users to upload and validate a file. At the moment I only have the default class called MyUI.java which contains all I need, here is an extract:
EDITED CODE:
FileUploader.java
public class FileUploader extends AbstractJavaScriptComponent {
public void callScript(){//as usual, it needs a method!!
JavaScript.getCurrent().execute(""
+"jsInit();"
+"");
}
And changes to the MyUI class:
`final FileUploader fileUploader = new FileUploader();//has to be final apparently
final Button button = new Button("Open popup");
button.addClickListener(new Button.ClickListener(){
#Override
public void buttonClick(ClickEvent event){
fileUploader.callScript();
}
});`
I need to move the js call to a new java class in a separate file, so I created a new class which now sits in its own file and that will extend AbstractJavaScriptComponent, my question is, what imports do I need for this class, all the ones that are in the previous class except for the UI or including those? Literally this class will only have the js call for now (there will be more stuff later)?
Also, to make use of this class presumably I will have to run As --> maven install again? And in general, what should I do?
cheers
This is a simple case of refactoring to extract a method and add a class.
You want to replace this
final Button button = new Button("Open popup");
JavaScript.getCurrent().execute("jsInit();");
With something like this
final Button button = new Button("Open popup");
JavaScriptCaller jsCaller = new JavaScriptCaller(JavaScript.getCurrent());
jsCaller.jsInit();
So, you can define a class like so
public class JavaScriptCaller extends AbstractJavaScriptComponent {
final JavaScript js;
public JavaScriptCaller(JavaScript js) {
this.js = js;
}
public void jsInit() {
this.js.execute("jsInit();")
}
}
The reason I added the parameter is so you don't need to keep calling the Javascript.getCurrent() since you only need one instance of it.

Passing string and variable from one class to another. JAVA

I coded Chat in java with gui. Now I made a simple Login with MySQL. If you login you open main chat class. But problem is I want to pass username string from login class to main class.
How to do it. And how to pass variable from one to another class too. thx guys
CODE:
Here is where you login and it opens new class
Login class. login2.java
if (res.next()) {
JOptionPane.showMessageDialog(this, "Login Sucessfull.");
new ClientGUI("localhost", 1500);
dispose();
}
else {
JOptionPane.showMessageDialog(this, "Invalid User Name/Passw");
}
Thank you for help.
Here is what I would do...create a public variable or a public object like:
public static String keepInfo; // on top of your code
Instantiate it in your code by giving it whatever value you want in the constructor. Then the class that wants to call should create an object of the class. In your case, it would something like:
ClientGUI client = new ClientGUI();
// to call your String you can do something like:
String getInfo = client.keepInfo; // if you don't have get or set methods

Passing values from jinternalframe1 to jinternalframe2

I've been reading a lot about constructors in Java as well as searching here in stackoverflow for related questions but I'm still confused on how my program will get a string value from my jinternalframe1 to jinternalframe2.
I have a jinternalframe which calls jinternalframe1. Here's my code.
ForgotPassword fp = new ForgotPassword();
JDesktopPane MainDesk = this.getDesktopPane();
MainDesk.add(fp);
this.dispose();
fp.show();
And here's my jinternalframe1..
public class ForgotPassword extends javax.swing.JInternalFrame {
public ForgotPassword(String acType, String uName) {
initComponents();
acType = AccountType.getSelectedItem() + "";
uName = username.getText();
}
AccountType variable is a jcombobox with three options: Administrator, LevelOne, LevelTwo.
username variable is a jTextField. I also have a jbutton called Next which calls jinternalframe2.
User will need to click Next button, and will check if username exist in the database. (I have already figured out this part) And then hides jinternalframe1 and calls jinternalframe2 if username exists in the database.
Now I am confused with this part.. jinternalframe2. I would like the Account Type and username value from jinternalframe1 to jinternalframe2.. I am trying this out but got no luck..
public class ForgotPassword2 extends ForgotPassword {
public ForgotPassword2(String acType, String uName) {
initComponents();
AccountType.getText() = acType;
username.getText() = uName;
}
You'll notice that the variable AccountType here in jinternalframe2 is a jTextField.
Both AccountType and username jTextField here in jinternalframe2 is not editable (disabled).
Error occurs on this lines:
ForgotPassword fp = new ForgotPassword();
public ForgotPassword2(String acType, String uName)
Error message on both lines
constructor ForgotPassword in class ForgotPassword cannot be applied
to given types; required: String,String found: no arguments
reason: actual and formal argument lists differ in length
Can someone enlighten me on how to use constructors right on my program? I am using netbeans by the way. Thank you in advance!
This has little to do with constructors and more to do with passing information between objects of different classes. For one you don't mis-use of inheritance for this purpose as you appear to be doing. Instead you use composition -- the class that needs information from another class needs a valid reference to the active object of the other class. Then the first class can call methods on the other one.
I think that for your purposes, you will likely be better off getting the user's information in a modal fashion using an internal option pane such as a JOptionPane.showInternalConfirmDialog(...). Whenever you open a modal dialog, the calling code halts at the point where you display the modal dialog. The calling code will then resume once the modal dialog is no longer visible, and at that point you can query the JPanel class that is displayed in your option pane for the data that it holds.
Note as an aside: if you are asking question about code, and you state that your code has an "error", you will want to post the full error message for all to see.
Also, this isn't valid Java:
AccountType.getText() = acType;
as you can't have a method call on the left side of an assignment statement.
What error occurs on those lines?
This is not a valid statement:
public ForgotPassword2(String acType, String uName)
It's not very clear what is your intended designe, but from what you posted I guess you need to create a new instance of ForgotPassword2:
public class ForgotPassword extends javax.swing.JInternalFrame {
String acType;
String uName;
public ForgotPassword(String acType, String uName) {
this.acType = acType;
this.uName = uName;
}
public void next(){
...
ForgotPassword2 fp2 = new ForgotPassword2(this.acType, this.uName);
...
}
}
Also this statement is very suspicious:
AccountType.getText() = acType;
This statement doesn't replace the reference to the String in the AccountType. You will need to call a setter.

Calling String from another method - Java

I am trying to use this String called "username" from another method, but I can't seem to figure out what to do.
I have this bit of code right here, which assigns a text field's entry to a variable, but I can't seem to use this variable in another method
//Configuring content pane
JFormattedTextField formattedTextField = new JFormattedTextField();
formattedTextField.setBounds(129, 36, 120, 20);
UsernameFrame.getContentPane().add(formattedTextField);
UsernameFrame.setVisible(true);
//Assigning text field entry to variable
String username = formattedTextField.getText();
Now, I am trying to use this variable in the method pasted below, but I don't know what I am missing..
public void actionPerformed(ActionEvent e){
if(username.length() < 5){
}
//Execute when the button is pressed
System.out.println("The button has been pressed");
}
This is probably something really simple I am missing, thanks for your help guys.
(full code)
http://pastebin.com/RMszazd4
Declare username right after your class declaration like this:
public class App {
private String username;
public static void main(String[] args) {
...
}
...
}
If these are two separate methods you will need to reassign the username variable again or you can create a global variable outside of your methods.
You might want to pass in the string variable "username"as a parameter to another method since it would not recognize the scope of your string in another method unless its declared global.
You don't appear to have a way of reading the JFormattedTextField. If I understand what you're trying to do correctly, you could declare formattedTextField as an instance variable, and then declare username inside the listener:
public class Frame {
JFrame UsernameFrame = new JFrame("Welcome");
private JFormattedTextField formattedTextField;
....
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = formattedTextField.getText();
if (username.length() < 5) {
}
// Execute when the button is pressed
System.out.println("The button has been pressed");
}
});
Now you have a reference to the text of the JFormattedTextField to do what you will with each time the button is pressed.

I can read but Can't edit Main class contents

I'm using netbeans to program something with a user interface...
I hava a main class that named "NewJFrame.java"(A) and one more class
that named "NewClass.java"(B). Class A is extended to class B like this:
public class NewClass extends NewJFrame{
...
}
Contents of ClassA are public static like this:
public static javax.swing.JTextField TextBox1;
I also has a button in classA .So when I click the button, it will call a function
from the classB and that function needs to edit TextBox1's text...
Here is whats going on when I click the button:
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String Str1;
NewClass nc = new NewClass();
Str1=nc.call();
}
Here is the funcion in ClassB:
public String call()
{
String Str;
Str = TextBox1.getText();
TextBox1.setText(Str + "1"); //This part isn't work.
JOptionPane.showConfirmDialog(null,Str,"22222222",JOptionPane.PLAIN_MESSAGE);
return Str;
}
So I can read the text of TextBox1 and show it in a messagebox but cannot edit his text.
If I put this code in main class it works perfectly but in another class it doesn't work.
Can someone help me to reslove this problem?
(I'm using netbeans 6.9.1)
I Just Trying to use some another class to add my code because I dont want all the codes stay in same file this is not usefull... Come on someone needs to know how to do that you can't be writing all the codes in a *.java file right?
The problem you are facing has nothing to do with NetBeans IDE,
you will face the same problem with any IDE for this code.
One way of achieving this is by aggregating the NewJFrame class in the NewClass
instead of extending it:
Let me exlplain with some code:
public class NewClass {
private NewJFrame frame = null;
public NewClass(NewJFrame frame) {
this.frame = frame;
}
public String call()
{
String text;
text = frame.TextBox1.getText();
frame.TextBox1.setText(text + "1"); //This will work now.
JOptionPane.showConfirmDialog(null,text,"22222222",JOptionPane.PLAIN_MESSAGE);
return text;
}
}
Here we will receive a reference to the calling JFrame class and will use fields
defined in that class.
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String Str1;
NewClass nc = new NewClass(this); // see the parameter we are passing here
Str1=nc.call();
}
When we create an object of class NewClass we will pass the reference of the
currently calling NewJFrame object
This will work check it.
Now coming to why your code is not working. When NewClass is extending NewJFrame
and when you create a new object of NewClass class it contains a separate
copy of the NewJFrame which is different from the calling NewJFrame reference hence
the field is getting set in another JFrame and not what you wanted.
with regards
Tushar Joshi, Nagpur
AFAIK Netbeans prevents you from editing by hand GUI's and behaves diferrently depending on strange issues like the one you have... but it was months ago, I dont know if current version sucks that much yet.
I really don't understand why you are forcing yourself to use a new class for this? Even if you NEED to, I don't understand why NewClass extends NewJFrame since you are only creating an instance to call a method that has nothing to do with GUI.
I think creating NewClass isn't necessary. Writing all the code in one class isn't bad by itself. This really depends on MANY factors: how much is "all the code"? Does it make sense to separate responsibilities? Etc, etc...
So make the JTextField and JButton NOT static and NOT public, and simply do everything in there:
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String str = TextBox1.getText();
TextBox1.setText(str + "1"); //This part isn't work.
JOptionPane.showConfirmDialog(null,Str,"22222222",JOptionPane.PLAIN_MESSAGE);
}
P.S.: variable names are start in lowercase: String str, not String Str.
I Found a solution. I'm throwing the contents whereever I'll use. Here is an Example:
Main class:
private void formWindowOpened(WindowEvent evt) {
Tab1Codes tc1 = new Tab1Codes();
if(!tc1.LockAll(TabMenu1))
System.exit(1);
tc1.dispose();
}
Another class where I added some of my codes:
public boolean LockAll(javax.swing.JTabbedPane TabMenu){
try
{
TabMenu.setEnabledAt(1, false);
TabMenu.setEnabledAt(2, false);
TabMenu.setEnabledAt(3, false);
TabMenu.setEnabledAt(4, false);
}catch(Exception e)
{
JOptionPane.showConfirmDialog(null, "I can't Lock the tabs!",
"Locking tabs...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
So, I can edit the contents in another class but it's little useless to send every content I want to read and edit.
If someone knows any short way please write here.

Categories