If you to change it on every fixed amount of time then you can use Swing Timer or Thread to do this. But for this you have to listen at least one action so that you can initialize and start it.
Up vote 0 down vote favorite share g+ share fb share tw.
I would like to create a JButton that changes its text periodically after the first click. I'm not really familiar with Swing library. What would be a good starting point?
May I update its text without an action? Thank you. Java swing jbutton actionlistener link|improve this question asked Aug 2 '11 at 13:10Mehmet Yesin1496 92% accept rate.
– Harry Joy Aug 2 '11 at 13:14 No, I want to change text without a click, lets say every 2 seconds – Mehmet Yesin Aug 2 '11 at 13:15.
If you to change it on every fixed amount of time then you can use Swing Timer or Thread to do this. But for this you have to listen at least one action so that you can initialize and start it. You can also use TimerTask class from java.
Util like follow: java.util. TimerTask timerTask = new java.util.TimerTask() { @Override public void run() { //change button text here using button. SetText("newText"); method } }; java.util.
Timer myTimer = new java.util.Timer(); myTimer. Schedule(timerTask, 3 * 1000, 3* 1000); // This will start timer task after 3 seconds and repeat it on every 3 seconds.
So whenever my desired time comes, Do I need to repaint or just setText – Mehmet Yesin Aug 2 '11 at 13:21 @Mehmet: just setText(newStr) should work. – Harry Joy Aug 2 '11 at 13:22 Thanx a lot, this is what I needed. – Mehmet Yesin Aug 2 '11 at 13:23.
For all periodical events in Swing I only suggest javax.swing. Timer output by using Timer should be, for example import java.awt. Color; import java.awt.
Dimension; import java.awt.event. ActionEvent; import java.awt.event. ActionListener; import java.util.
Random; import javax.swing. JButton; import javax.swing. JFrame; import javax.swing.
Timer; public class CrazyButtonTimer { private JFrame frame = new JFrame(" Crazy Button Timer"); private JButton be = new JButton("Crazy Colored Button"); private Random random; public CrazyButtonTimer() { b. SetPreferredSize(new Dimension(250, 35)); frame.getContentPane(). Add(b); frame.
SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame.pack(); frame. SetLocationRelativeTo(null); frame.
SetVisible(true); javax.swing. Timer timer = new Timer(500, new TimerListener()); timer. SetInitialDelay(250); timer.start(); } private class TimerListener implements ActionListener { private TimerListener() { } @Override public void actionPerformed(final ActionEvent e) { Color c = b.getForeground(); if (c == Color.
Red) { b. SetForeground(Color. Blue); } else { b.
SetForeground(Color. Red); } } } public static void main(final String args) { java.awt.EventQueue. InvokeLater(new Runnable() { @Override public void run() { CrazyButtonTimer crazyButtonTimer = new CrazyButtonTimer(); } }); } }.
1 For using Swing Timer. – mre Aug 2 '11 at 13:57.
If you want to change it periodically (e.g. Every 5th second) you could create a new Thread which sets the text of the button to the desired value and repaints it (if necessary).
I suggest you to create a timer (here you can find some doc) Timer timer = new Timer(100,this); Your class has to extend action listener ed implements the following method which allow you to change the text of your JButton(I called it ``button). Public void actionPerformed(ActionEvent e) { if(e.getSource. Equals(timer)){ button.
SetText("newText"); } } Luca.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.