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.
Related
Is there a method to get the button associated with a particular command string?
For instance if I define a button with:
button.setActionCommand("unique_toggle");
Having that string "unique_toggle", is it possible to retrieve that button from another class? I am beginner at Java, excuse if this question may seem obvious to you.
Yes, you can access to your button and its associated button's command. If you need to a button's command, that possibly means that you should consider redesining your programm because this approach is not advisable and very dirty way to do what you want to achieve.
When it comes to answer to your question,
Lets say Foo1 is your GUI class.
class Foo1{
JButton button;
public Foo1(Foo2 otherClass)
{
button = new JButton();
otherClass.setButtonAddress(button);
}
..... other methods
}
Foo2 is the class, in where you want to access button's command text.
class Foo2{
JButton buttonFromOtherClass;
//This is the method, in where you need command string of the button
private void getCommandsString()
{
Foo1 foo1 = new Foo1(this);
//After the initialization of Foo1, you can get every information of the button
String actionCommand = buttonFromOtherClass.getActionCommand();
}
public void setButtonAddress(JButton button)
{
buttonFromOtherClass = button;
}
}
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
I have many .java files within my project. From FTall.java i want to access {text field} t1 ('main' jFrame -> jPanel2) of the FormTTS.java
I am right now getting errors due to that only, because it cannot find symbol t1.
It is private and i cant change it to public
Edit:
I am using this code already to open up FTall from the FormTTS.java:
In a button in FormTTS
FTall forma = new FTall();
JFrame frame = forma.getFrame();
forma.setVisible(true);
and this in FTall
public JFrame getFrame() {
return jFrame1;
}
Because of the way your code is structure, you need to supply some way for FormTTS.t1
In FormTTS, provide a method to exposes t1, something like getMainTextField for example...
public JTextField getMainTextField() {
return t1;
}
You're next problem is FTall is going to need a reference to an instance of FormTTS. Probably the easiest way would be to pass a reference to the constructor of FTall
private FormTTS mainForm;
public FTall(FormTTS mainForm) {
this.mainForm= mainForm;
}
This will allow you to access t1 by simply using the mainForm reference...
JTextField field = mainForm.getMainTextField();
Personally, I would prefer not to expose the text field as it gives too much access to callers, instead I'd prefer to return the text and if required provide a means to change it...
So in FormTTS, I might do something like...
public String getMainText() {
return t1.getText();
}
// Do this only if you need to have write access
public void setMainText(String text) {
t1.setText(text);
}
But that's just me...
To obtain the value, you would use a similar approach as above (to getting the text field)
String text = mainForm.getMainText();
if i am understanding your question its simple first ensure that your text field come in to scope before access and once it come in, then use a setter to set its refrence in required class then you can access it.
I have a problem with a variable in MyFrame class. I want to have in MyFrame class the value of a variable that is defined in a combobox listener.
This is my situation: I have a combobox with some friends' name. I have put a listener to the combobox which has to return the surname of the selected friend.
I want to insert the value of surname in a command in MyFrame class, but there are some problems: once setted surname as final (because it has to be used in the Listener), I have an error that say:
The final local variable surname cannot be assigned, since it is defined in an enclosing type.
What is (or are) the matter(s)? Here I post my code:
public class MyFrame extends {
public static void main (String[] args)
{
//other
String [] names = {"john","al","jack"};
final String surname=null;
JLabel nameLbl = new JLabel("surname: " + surname);
JComboBox box = new JComboBox(names);
JPanel centralPnl = new JPanel();
centralPnl.add(nameLbl);
centralPnl.add(box);
box.addItemListener(new ItemListener()
{
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED)
{
// Here operations from database
//that return friends' surname under the variable name of "result"
surname = result;
}
}
});
}
}
You are trying to reassign a final variable, and thats the problem.
Also your final variable needs to be initialised in the first place.
Beyond the issues with the code already pointed out, I guess the question is do you need to store surname or are you just using it to update the label?
If you need to store the data, move your surname variable to the class level.
If you are simply updating the label, then do something like
nameLbl.setText("surname: " + result);
There are two things first one is that final variable must be initialized when it is declared and that final variable cannot be reassigned a value.
Unfortunately you are doing both of the mistakes.
Another problem is that you should post a Valid code; it will make others finding problems easily.
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.