Re: Updating The Components
How many MainWindow objects are created by the program? Add a println statement in the constructor that prints a message so you will know.
How many should it create?
What if one is visible and the others are not?
Re: Updating The Components
Thanks Norm. It does create another window instead of just updating the textPane in the existing one. And when I set the new window (presented by "m" in Hail class), it shows another window with the updated textPane.
However, this makes my app faulty. I don't want to create another window. How else can I update the textPane to show the info?
Re: Updating The Components
Quote:
I don't want to create another window.
You need to make a reference to the existing, visible window available to the code that wants to use it.
One way would be to pass the reference in the call to the Hail class's constructor and have the constructor save the reference in a class variable.
Re: Updating The Components
Norm I am sorry but I don't understand you. I thought I did, but I sat in front of my code to make a change but it turns out, I did not understand you :D
Re: Updating The Components
Where is there a reference to the existing instance of the MainWindow class? If the code in the MainWindow class is calling the constructor, use the this variable. this refers to the class where the code is executing.
Pass that in the Hail class's constructor. Have the constructor save the arg that is passed to it in a class variable.
Which part of this don't you understand?
Pass a variable in a constructor:
Code :
... = new TheClass(theVarToPass);
Save value in constructor:
Code :
class TheClass {
ThePassedArg theArg; // save arg here
public TheClass(ThePassedArg theArg) { // the constructor with arg
this.theArg = theArg; // save arg in class variable
Re: Updating The Components
I still can't get my head around it.
Actually I am not sure anymore if I don't understand you or I don't know how to implement your idea.
What I am doing here is that, the mainWindow class basicly constructs the window. And there is one button. This button runs hail class. The hail class runs the procedure with the given number value for the given count times. Then it gives the result to the console and tells me if the number fits with the Hailstone sequence.
Now, if the method in the hail class returns a String, it is easy to write it to textPane. But I don't want this, because I want to write other things to the textPane, too. That is, if you look at the code, it says "Starting", "Number/2" or things like that. These should be put to textPane too.
About your suggestion, believe me I tried. But I couldn't find a way to write it WITHOUT creating a new mainWindow object. The mainWindow is the first running part of the program, I don't know how to change the values there. And to be honest, I could not implement what I think you are saying. If I don't make a new mainWindow object, my setYazi method is useless too. So basicly, I don't actually have a method to write down to textPane.
I still need help. Is there something horribly wrong with my code?
Re: Updating The Components
Quote:
y to write it WITHOUT creating a new mainWindow object.
Use the reference to the MainWindow object that is passed to the constructor.
Code :
m.setYazi("Suitable. Count: "+sayac+");
In the above, m should be the reference that was passed to the Hail class.
In my code example, the reference was: theArg
Code :
theArg.setYazi("Suitable. Count: "+sayac+");
Do NOT create a new instance of MainWindow to get the needed reference. Pass the reference into the constructor as shown above.
Quote:
About your suggestion, believe me I tried.
Post the code where you tried my suggestion.
Re: Updating The Components
I got it. However, I am not sure if this is what you told me to do. But I passed an JTextPane to hail class' test method.
I am sharing my final code here, and it is cleaned from the System.out.println lines.
If this is not what you meant, please you share your version when you have time and let me learn another way of doing this.
OK, this is my mainWindow class, I didn't change much here:
Code Java:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class mainWindow extends JFrame {
private JPanel contentPane;
private JTextField Number;
private JTextField Count;
public JTextPane textPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainWindow frame = new mainWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public mainWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNumber = new JLabel("Number");
lblNumber.setBounds(10, 11, 46, 14);
contentPane.add(lblNumber);
Number = new JTextField();
Number.setBounds(66, 8, 86, 20);
contentPane.add(Number);
Number.setColumns(10);
JLabel lblCount = new JLabel("Count");
lblCount.setBounds(10, 44, 46, 14);
contentPane.add(lblCount);
Count = new JTextField();
Count.setBounds(66, 41, 86, 20);
contentPane.add(Count);
Count.setColumns(10);
textPane = new JTextPane();
textPane.setEditable(false);
textPane.setBounds(10, 69, 414, 181);
contentPane.add(textPane);
JButton btnStart = new JButton("START");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1 = Number.getText();
int num = Integer.parseInt(s1);
String s2 = Count.getText();
int count = Integer.parseInt(s2);
Hail h = new Hail();
String result = textPane.getText();
h.test(num, count, textPane);
repaint();
}
});
btnStart.setBounds(162, 7, 89, 23);
contentPane.add(btnStart);
}
}
And this is the Hail class. It passes the String variable resulter to the textPane component of mainWindow:
Code Java:
import javax.swing.JTextPane;
public class Hail {
String resulter = "";
public void test(int c, int d, JTextPane tp){
int sayac=0;
while(c!=1){
if(c%2==0){
c = c/2;
resulter = resulter + "Number is even. So I divide it by two. Result: "+c+"\n";
tp.setText(resulter);
}else{
c = (3*c)+1;
resulter = resulter + "Number is odd. So I multiply it and add 1. Result: "+c+"\n";
tp.setText(resulter);
}
sayac++;
if(sayac==d){
break;
}
}
if(c==1){
resulter = resulter + "Suitable. Count: "+sayac+"\n";
tp.setText(resulter);
}else{
resulter = resulter + "Not suitable or not enough repetition"+"\n";
tp.setText(resulter);
}
}
}
Re: Updating The Components
That's another way to do it.
Re: Updating The Components
Yes but can you give a short example of your way or just implement it with my code? I'm very interested in learning your way.
Re: Updating The Components
See post #6 for a short piece of code that passes a reference to a constructor and has the constructor save the reference in a class variable.
Re: Updating The Components
Thanks. So, just to be clear on this, considering my code;
-You say you would pass a variable of the mainWindow class to the constructor of Hail class, right? I did not use a constructor. Maybe that is why I could not understand you in the first place?
-I pass a component to the method of Hail class. But I could also pass a String to store the information, then I would just write it to the JTextPane component within the mainWindows class. Is that right?
Re: Updating The Components
As you have discovered there is more than one way to solve the problem:
Pass a reference in:
the constructor where it is saved for later use
the method call where it is used
Re: Updating The Components