Suppose you have class A
and class B
. Class A
is the main class which builds a frame with a GUI. It contains all the GUI variables (such as buttons, labels, strings) along with whatever methods that you'll be using. Class A
also creates a class B
object:
ClassB name = new ClassB();
Inside class B
you will find a for loop. Now, once the for loop is finished looping, I want to call a method located in class A
. Whenever I try calling a method located in class A
, Eclipse suggests making that method static
. I'm trying to avoid making static methods. Is there a way of calling class A
's methods from class B
without making anything static?
Class A:
public class Game extends JFrame implements ActionListener {
// init variables
private JPanel contentPane;
private JPanel panel_actions;
private JButton btn_strike;
private JProgressBar progBar_loading;
private Load load;
// create the frame
public dsgsd() {
load = new Load();
// frame initializing
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setBounds(100, 100, 890, 480);
setTitle("BeyondInfinity - Group Project for CS1100");
getContentPane().setLayout(null);
setVisible(true);
// create a root panel
contentPane = new JPanel();
contentPane.setLayout(null);
contentPane.setBounds(0, 0, 884, 451);
contentPane.setVisible(true);
getContentPane().add(contentPane);
// create actions panel for displaying attack buttons
panel_actions = new JPanel();
panel_actions.setBorder(new EmptyBorder(10, 10, 10, 10));
panel_actions.setBounds(10, 306, 854, 68);
panel_actions.setBackground(new Color(100, 149, 237));
panel_actions.setLayout(new GridLayout(0, 6, 10, 0));
panel_actions.setVisible(true);
contentPane.add(panel_actions);
// create attack button #1
btn_strike = new JButton("Strike");
btn_strike.setFocusable(false);
btn_strike.setVisible(true);
btn_strike.addActionListener(this);
panel_actions.add(btn_strike);
}
// create action listener
public void actionPerformed(ActionEvent evt) {
if (evt.getSource().equals(btn_strike)) {
load.start();
}
}
public void executeTasks() {
//TODO do something
}
// set value for the loading bar
public void setProgBar_loading(int val) {
progBar_loading.setValue(val);
progBar_loading.repaint();
}
}
Class B:
public class Load {
private Timer timer;
private int i;
public void start() {
// reset loading bar
Game.setProgBar_loading(0);
i = 0;
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (i > 100) {
timer.stop();
Game..executeTasks();
} else
Game.setProgBar_loading(i++);
}
};
// timer which triggers the actionlistener every 15ms
timer = new Timer(15, listener);
timer.start();
}
}