I'm trying to learn this getting value - setting value - returning the value from where it came from style of coding. I heard this is kind of 'pass by value or reference' kind of problem, did my research and still stuck with the problem. Anyways, this is my source code:
System.out.println(x);
int x = object2.getX(); **//I HAVE A PROBLEM HERE**
int x2 = rand.nextInt(100);
int y = rand.nextInt(100);
int xpost = rand.nextInt(300);
int ypost = rand.nextInt(150);
allField[x] = new JTextField(String.format(" %s + %s", x2 , y));
allField[x].setBounds(xpost, ypost, 100, 30);
allField[x].setEnabled(false);
add(allField[x]);
object2.setX(x++); **//I HAVE A PROBLEM HERE TOO**
I'm trying to get the value from another class through object2.getX(); from there it will set the indexes of the array. After done with the setting of array, which is still at 0, I want to increment it (x++) and that value, which is 1, is pass to another class and set it.
this is the other class:
public class TimerTutorial extends JFrame {
JLabel promptLabel, timerLabel;
int counter, x = 0;
int changeTest;
JTextField tf;
JButton button;
Timer timer;
public int getX(){**//I HAVE A PROBLEM HERE**
return x;
}
public int setX(int y){**//I HAVE A PROBLEM HERE**
x = y;
return this.x;
}
}
If you want the whole code, but the problem is stated before this:
import java.awt.event.*;
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class TimerTutorial extends JFrame{
JLabel promptLabel, timerLabel;
int counter, x = 0;
int changeTest;
JTextField tf;
JButton button;
Timer timer;
public int getX(){
return x;
}
public int setX(int y){
x = y;
return this.x;
}
public TimerTutorial(){
setLayout(new GridLayout(2,2,5,5));
tf = new JTextField();
add(tf);
promptLabel = new JLabel("Enter seconds:", SwingConstants.CENTER);
add(promptLabel);
button = new JButton("Start Timing");
add(button);
timerLabel = new JLabel("Waiting...", SwingConstants.CENTER);
add(timerLabel);
event e = new event();
button.addActionListener(e);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
int count = (int)(Double.parseDouble(tf.getText()));
timerLabel.setText("Time left:" +count);
TimeClass tc = new TimeClass(count);
timer = new Timer(1000, tc);
timer.start();
}
public class TimeClass implements ActionListener{
int counter;
public TimeClass(int counter){
this.counter = counter;
}
public void actionPerformed(ActionEvent tc){//every time timer updates this will spark
counter--;
JTextField[] allField = new JTextField [20];
TimerTutorial object2 = new TimerTutorial();
Random rand = new Random();
System.out.println(x);
int x = object2.getX();
int x2 = rand.nextInt(100);int y = rand.nextInt(100);
int xpost = rand.nextInt(300); int ypost = rand.nextInt(150);
allField[x] = new JTextField(String.format(" %s + %s", x2 , y));
allField[x].setBounds(xpost, ypost, 100, 30);
allField[x].setEnabled(false);
add(allField[x]);
object2.setX(x++);
if(counter>=1) {
timerLabel.setText("Time left: "+counter);
} else {
timer.stop();
timerLabel.setText("Done!");
Toolkit.getDefaultToolkit().beep();
}
}
}
}
public static void main(String args[]){
TimerTutorial gui = new TimerTutorial();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(800,800);
gui.setVisible(true);
}
}
The code is working fine actually. My aim is to just randomly post JTextFields in sec intervals with random position and values in it. The problem here is that the x does not increment. Which puts my JTextFields to index [0]. PS: still new to java, any tips will be appreciated