witam, chcę napisać program który będzie wypisywał liczby od 0 do 100 w odstępach czasowych po kliknięciu na przycisk i wypisywane w komponencie JTextArea. Chciałem się dowiedzieć czy do tego lepiej jest użyć Timera który te liczby będzie wypisywał w odstępach czasowych czy lepiej SwingWorkera?? Według mnie Timer odpada ponieważ zależy mi na aktywności pozostałych przycisków a tak to będą zamarzać. Drugi mój problem to ScrollBar w komponencie JTextArea, jest dodany i widoczny ale pozostaje tak jakby nie aktywny. proszę o opinie, zamieszczam również kod
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingodliczacz;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
/**
*
* @author marcin
*/
public class KlasaOdliczacz extends JFrame implements ActionListener{
private int wysokosc = 100;
private int szerokosc = 300;
private int pozycja_okna_x = 500;
private int pozycja_okna_y = 400;
private JPanel jPanel = new JPanel();
private JPanel jPanelLewy = new JPanel();
private JPanel jPanelPrawy = new JPanel();
private JButton jButtonStart = new JButton();
private JButton jButtonStop = new JButton();
private JButton jButtonReset = new JButton();
private JTextArea jTextArea = new JTextArea();
private JScrollPane jScrollPane = new JScrollPane();
private GridLayout gridLayoutGlowny = new GridLayout(1, 2);
private BorderLayout borderLayout = new BorderLayout();
private GridLayout gridLayoutPrawy = new GridLayout(3, 1);
public KlasaOdliczacz() {
setTitle("odliczacz !!!");
setLocation(pozycja_okna_x, pozycja_okna_y);
setSize(szerokosc, wysokosc);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(jPanel);
jTextArea.setText("");
jTextArea.setLineWrap(true);
jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jButtonStart.setText("start");
jButtonStart.addActionListener(this);
jButtonReset.setText("reset");
jButtonStop.setText("stop");
jPanel.setLayout(gridLayoutGlowny);
jPanel.add(jPanelLewy);
jPanel.add(jPanelPrawy);
jPanelLewy.setLayout(borderLayout);
jPanelLewy.add(jTextArea, BorderLayout.CENTER);
jPanelLewy.add(jScrollPane, BorderLayout.EAST);
jPanelPrawy.setLayout(gridLayoutPrawy);
jPanelPrawy.add(jButtonStart);
jPanelPrawy.add(jButtonStop);
jPanelPrawy.add(jButtonReset);
}
@Override
public void actionPerformed(ActionEvent e) {
Object object = e.getSource();
if(object == jButtonStart){
startLiczenia();
}
}
void startLiczenia(){
for(int i = 0; i < 100; i++){
jTextArea.append(Integer.toString(i) + "\n");
Thread.currentThread();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(KlasaOdliczacz.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaswingodliczacz;
import javax.swing.SwingUtilities;
/**
*
* @author marcin
*/
public class JavaSwingOdliczacz {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
KlasaOdliczacz klasaOdliczacz = new KlasaOdliczacz();
}
});
}
}