Here is example of the code how to append text on the TextArea at the moment the event happens.
We can use this if we wanna for example see program progress and notify the users of progress.
With click on the button the AWT-EventQueue fires action. If we don't call process in a new Thread the TextArea will be updated when AWT Thread is released.
But during that time is nice to see the progress of the program.
/**
* @author Peter Gostincar
*/
public class Start {
public static void main(String[] args) {
Manager.getInstance();
}
} |
/**
* @author Peter Gostincar
*/
public class Manager {
public static Manager instance;
private MyFrame mainFrame;
private Manager(){
mainFrame = MyFrame.newInstance();
mainFrame.setVisible(true);
}
public static Manager getInstance() {
if(instance == null){
instance = new Manager();
}
return instance;
}
public void startMethod() {
new Thread() {
public void run() {
for (int i = 0; i < 5000; i++) {
doSmething();
if (i == 0) {
sendMsg("Start");
}
if (i % 7 == 0) {
sendMsg(i+"th iteration");
}
}
sendMsg("End");
}
}.start();
}
private void doSmething() {
try{
Thread.sleep(1);
}
catch (InterruptedException e){
}
}
private void sendMsg(String str){
mainFrame.setTextArea(str);
System.out.println(str);
}
}
|
/**
* @author Peter Gostincar
*/
public class MyFrame extends javax.swing.JFrame {
private static MyFrame instance;
public MyFrame(){
initComponents();
instance = this;
}
public static MyFrame newInstance() {
if(instance == null){
new MyFrame();
}
return instance;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
textArea = new javax.swing.JTextArea();
startButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
textArea.setEditable(false);
textArea.setColumns(20);
textArea.setRows(5);
jScrollPane1.setViewportView(textArea);
startButton.setText("Start");
startButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(162, 162, 162)
.addComponent(startButton)
.addContainerGap(181, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(98, Short.MAX_VALUE)
.addComponent(startButton)
.addGap(39, 39, 39)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE
, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton startButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea textArea;
// End of variables declaration
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Manager.getInstance().startMethod();
}
public void setTextArea(String str) {
textArea.append(str + System.getProperty("line.separator"));
}
}
|
Ni komentarjev:
Objavite komentar