Reputation: 57
I have simple Jframe with 2 buttons.
public class Animace extends javax.swing.JFrame {
public Animace() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Play");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Pause");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addContainerGap(237, Short.MAX_VALUE))
);
getContentPane().add(jPanel1, java.awt.BorderLayout.LINE_END);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 319, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// PLAY BUTTON
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//PAUSE BUTTON
}
public static void Hi(){
System.out.println("Hi guys");
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Animace().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
// End of variables declaration
}
After click on Jbutton1 (play button) need call method Hi(), every 10s.
After click on Jbutton2 (pause button) need pause this call..
Can you help me? I build in java netbeans.
Upvotes: 1
Views: 5782
Reputation: 16403
If you plan on changing elements of your GUI inside the repeatedly called function (Hi
in your case) you should use javax.swing.Timer
:
private javax.swing.Timer timer;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
timer = new Timer(10*1000, new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
Hi();
}
});
timer.start();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
timer.stop();
}
Upvotes: 2
Reputation: 832
Java threads are powerful tool for concurrent processing and multitasking. You should use thread that will definetly what you should need you must google for java threads how to create,interrupt,sleep,run moreon...you will easily done with java after geting threads knowledge
Upvotes: 1
Reputation: 8640
just have at Timer schedule
syntax should be more or less
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// PLAY BUTTON
//make it field so you can stop it
Timer t = new Timer();
TimerTask task = new TimerTask(){
public void run()
{
Hi();
}
}
t.scheduler(task,0,10*1000);
}
sorry if code will contains some errors, im writing from memory, and i'm stil half sleep:)
Upvotes: 1
Reputation: 9317
There is java tutorial Swing Timer which explains how to use Swing Timer, this is what you need here.
Upvotes: 3