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
Related
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.
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("");
}
}
I am building an application with a Room class which is abstract and a Standard class which inherits from Room. I have then created a Hostel class. Within the Hostel class is ArrayList<Room> rooms to which rooms can be added. I have created a method in the Hostel class which shows all available rooms but when I try and instantiate this in another class (MainGUI) nothing is shown. As far as I can see this is because I am creating a new hostel each time I click the button but would like to know how to pass the data across instead of creating a new hostel each time. Below are the relevant snippets of code.
Hostel Class
public Hostel()
{
rooms = new ArrayList<Room>();
}
public void showAvail()
{
for (Room room : rooms)
{
if (room.available == true)
{
theString = room.getRoomData() + "\n";
//System.out.println("Available Rooms" + "\n" + theString);
JOptionPane.showMessageDialog(null,theString);
}
}
}
public void addRoom(Room theRoom)
{
rooms.add(theRoom);
}
MainGUI Class
roomsFreeB.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Hostel host = new Hostel();
host.showAvail();
}
});
Any help would be appreciated
Unless you omitted code between the Hostel host = new Hostel(); and host.showAvail();, you never add any rooms to the hostel so there are no available rooms (or any at all) to show. You need to either add rooms to host after you create it and before you showAvail, or create a Hostel instance variable, fill it somewhere, and then call showAvail on that.
Would be good to see how you initialize your rooms inside Hostel. And if you like to initialize Hostel once only, do it outside of Listener. In this case it must not be a field inside MainGUI.
public void actionPerformed(ActionEvent e)
{
Hostel host = new Hostel();
host.showAvail();
}
In the previous code, the object is created and then destroyed once the method is done. The variable host is a local variable and consequently lives only during the execution of the method.
Depending on what you want to do, you should declare your host variable inside the main method or declare an array of hostel inside the main method again.
Since you are creating new Hostel each time. I guess there will be to Rooms in the ArrayList.
You need to create the Hostel Object outside the actionPerformed. And in your case it should be created only one time. And on this created Hostel object you will be adding the Room object.
If the question is where to do it.. Its left to you. Its upon your design.
For eg it can be.
You can create a class called ABC inside which you can create the hostel object. Write a static method called getHostel(). Then call ABC.getHostel()
Yes you are right that everytime action is performed a new Hostel is created and so is the list of rooms associated with hostel.
On click of button you would know which hostel you want to show (May be reading your application database or something), in case this is the first time your hostel will have empty room, else once you have read the hostel information you would also know about rooms which belong to the hostel, which can then be passed to your hostel object either through constructor or setter method.
Code snippet:
createHostel(String hostelName) {
//read from database
//No hostel with hostelname found create a new hostel else if hostel is found send the same (by this time hostel object would have room information also
}
Your action
public void actionPerformed(ActionEvent actionEvent) {
//MyFactory.getHostel(String hostelName)
//Once you have hostel object call showAvail on it, if its new you will get nothing else you will get all the rooms available
}
Hope this gives you some insight.
Your problem is exactly what you thought, you are making a new ArrayList each time you click the button so you will never see the data. You should begin by creating a hostel object in your MainGUI class,
private Hostel hostel;
this will allow previously entered information to be referenced
I need to pull a JavaScript var off a site so I can use it in my code. Following this tutorial, I was able to display the string in an alert message. But what do I have to do to use the string outside of the alert message? Thanks.
EDIT: My code is basically the same as in the tutorial.
Instead of calling AlertDialog, just do something in Java with the value of the "html" parameter, unless I'm completely misunderstanding what you are asking.
String savedHtml = null;
/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
#SuppressWarnings("unused")
public void showHTML(String html)
{
savedHtml = html; // this ought to work.
}
}
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.