开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜