How to execute cmd commands via Java swing - java

I have a file to print and I want to send him a custom water mark via java swing.
i have 2 files NewJFrame.java and Test.java
package test;
import java.io.IOException;
import java.io.OutputStream;
/**
*
* #author shaharnakash
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("whater mark name");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jButton1.setText("submit");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(30, 30, 30)
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 251, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.add(layout.createSequentialGroup()
.add(17, 17, 17)
.add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 206, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(41, 41, 41)
.add(jLabel1))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.add(112, 112, 112)
.add(jButton1)
.addContainerGap(96, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String value;
value = jTextField1.getText();
try{
String command = "cmd /c start cmd.exe";
Process child = Runtime.getRuntime().exec(command);
OutputStream out = child.getOutputStream();
out.write("C:/PDF>pdfprint.exe -printer 'docPrint' -firstpage 1 -lastpage 1 -wtext 'value' -wo 100 -wa 50 -wf 'Arial' C:/readme.pdf".getBytes());
out.close();
}catch(IOException e){
}
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
and the main java class Test.java that execute the program
package test;
/**
*
* #author shaharnakash
*/
public class Test {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}
the idea is to insert a value to the text field and after that click on the submit button it will execute the cmd command
C:/PDF>pdfprint.exe -printer 'docPrint' -firstpage 1 -lastpage 1 -wtext 'value' -wo 100 -wa 50 -wf 'Arial' C:/readme.pdf
when i execute the program it will open the cmd and will not send the file to the printer
note: draft is the water mark change
i change to Process child = Runtime.getRuntime().exec("\"PDF\pdfprint.exe -printer docPrint -firstpage 1 -lastpage 1 -wtext "+value+" -wo 100 -wa 50 -wf Arial C:\readme.pdf");
and i still not working

Personally, I'd use ProcessBuilder, if for no other reason, it reduces a lot of the complexity involved with working with Process.
ProcessBuilder pb = new ProcessBuilder("pdfprint.exe", "-printer", "'docPrint'", "-firstpage", "1", "-lastpage", "1", "-wtext", "'value'", "-wo", "100", "-wa", "50", "-wf", "'Arial'", "C:/readme.pdf");
pb.redirectErrorStream(true);
Process p = pb.start();
InputStream is = p.getInputStream();
int inInt = -1;
while ((inInt = is.read()) != -1) {
System.out.print((char)inInt);
}
Now, this is a block calling, meaning if you execute this within the context of your UI (AKA The Event Dispatching Thread), the UI will appear to hang until the external process has finished running.
In this case you should call execute the command in the back. Probably the simplest approach would be to use a SwingWorker
public class ExeWorker extends SwingWorker<String, String> {
public String doInBackground() {
// Execute command here...
// Depending on what you want to return, setup a return value
// This could an error string or the path to the output file for example...
return result;
}
public void done() {
// Back on the EDT, update the UI ;)
}
}
Have a read of Concurrency in Swing for more details

use Runtime.getRuntime().exec() method. Google about its different variants.

Related

How to correctly remove the automatically added closing bracket when it is typed

in the textPaneKeyTyped(java.awt.event.keyEvent evt) method I wrote some code to automatically close brackets when the opening one is typed. I added some code for remove the closing bracket when the user type it in textPane, but it doesn't do anything
This is a working example:
package test;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
textPanel = new javax.swing.JTextPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
textPanel.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
textPanelKeyTyped(evt);
}
});
jScrollPane1.setViewportView(textPanel);
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()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(27, Short.MAX_VALUE))
);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
private void textPanelKeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
Action action1 = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
textModel = (JTextPane) e.getSource();
try {
int position2 = textModel.getCaretPosition();
textModel.getDocument().remove(position2, 1);
textModel.getDocument().insertString(position2, ")", null);
} catch (Exception e1) {}
}
};
Action action = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
int position = textModel.getCaretPosition();
textModel.replaceSelection("()");
textModel.setCaretPosition(position+1);
}
};
String key = "typed (";
String key1 = "typed )";
textPanel.getInputMap().put(KeyStroke.getKeyStroke(key), key);
textPanel.getInputMap().put(KeyStroke.getKeyStroke(key1), key1);
textPanel.getActionMap().put(key, action);
textPanel.getActionMap().put(key1, action1);
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane textPanel;
// End of variables declaration
}
The code for the automatic closure of the brackets works..
The problem is for the replace of the closing bracket: it prints ')' just when before there's a '('. If I would be able to print the ')' at any time, how should I edit my code?
Thank you.
It is never a good idea to block the usage of a KeyStroke. You never know when the ")" might need to be entered manually. Who cares if the user enters the ")". If it is not syntactically correct then they will eventually get a compile error. The smart user will realize that all then need to do is type "(" and the ")" will be entered for them.
However, if there is a situation when you do want to prevent the typing of a given character you would use Key Bindings NOT a KeyListener.
For example:
KeyStroke ignore = KeyStroke.getKeyStroke(')');
textPane.getInputMap().put(ignore, "none");
For more information on Key Bindings read the Swig tutorial. I gave you a link in one of your previous questions. Keep the tutorial link handy for the basics. You will find a section on How to Use Key Bindings when you look at the table of contents ("trail") for the tutorial.
Also for your "(" Action you can just use:
textPane.replaceSelection("()");

I can't initialize my array of objects outside of my event method

I want to make a program that will create a Array of objects that will store the number of times a user has clicked a button.
I ask the user what their id number is and then access their element of the array and update their button clicks.
The problem is that it won't let me set
surveyor[0] = new Surveyors();
surveyor[1] = new Surveyors(); unless I put the code in my attemptUpdateActionPerformed method, but I need to set it up outside of the method so it will not reset itself each time the button is clicked. There is a lot of extra stuff in here, but I think the main problem lies in the method. Sorry for the sloppiness.
package guildquality;
import java.util.Scanner;
public class NewJFrame extends javax.swing.JFrame {
Surveyors [] surveyor = new Surveyors[10];
Scanner scan = new Scanner(System.in);
public int id;
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
surveyor[0] = new Surveyors();
surveyor[1] = new Surveyors();
id = 0;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
attemptUpdate = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
attemptUpdate.setText("Attempt");
attemptUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
attemptUpdateActionPerformed(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()
.addGap(21, 21, 21)
.addComponent(attemptUpdate)
.addContainerGap(308, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(173, Short.MAX_VALUE)
.addComponent(attemptUpdate)
.addGap(104, 104, 104))
);
pack();
}// </editor-fold>
private void attemptUpdateActionPerformed(java.awt.event.ActionEvent evt) {
System.out.print("what is your id?");
id = scan.nextInt();
surveyor[id].setAttempts();
System.out.print(surveyor[id].getAttempts());
// TODO add your handling code here:
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton attemptUpdate;
// End of variables declaration
}
at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
at sun.awt.Win32GraphicsConfig.getBounds(Win32GraphicsConfig.java:222)
at java.awt.Window.init(Window.java:497)
at java.awt.Window.<init>(Window.java:536)
at java.awt.Frame.<init>(Frame.java:420)
at java.awt.Frame.<init>(Frame.java:385)
at javax.swing.JFrame.<init>(JFrame.java:180)
at guildquality.NewJFrame.<init>(NewJFrame.java:19)
at guildquality.Surveyors.<init>(Surveyors.java:17)
at guildquality.NewJFrame.<init>(NewJFrame.java:20)
Put it in the main method. It will run once and before the eventhandler can run.
You should put code you want to initialize non-static class variable once in the constructor.
eg.
public NewJFrame() {
initComponents();
// More initialization here.
surveyor[0] = new Surveyors();
surveyor[1] = new Surveyors();
}

Trying to make a screen saver from a jframe, cant switch images

I'm trying to make a screesaver frame in java.swing using the NetBeans GUI builder. The images won't display when i set an infinte loop that cycles between them. The images are set to display on a jLabel through setIcon. Here's the code below:
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
public class AdFrame extends javax.swing.JFrame {
/**
* Creates new form FifthFrame
*/
public AdFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridBagLayout());
jPanel1.setOpaque(false);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/btc_gui/newpackage/btc-zg.jpg"))); // NOI18N
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
getContentPane().add(jPanel1, new java.awt.GridBagConstraints());
pack();
}// </editor-fold>
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
AdFrame ff1 = new AdFrame();
ff1.setExtendedState(JFrame.MAXIMIZED_BOTH);
ff1.setVisible(true);
int index = 0;
String[] filearray = new String[2];
filearray[0] = "/btc_gui/newpackage/btc-zg.jpg";
filearray[1] = "/btc_gui/newpackage/pic2.jpeg";
while (true){
ff1.jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource(filearray[index])));
ff1.jLabel1.repaint();
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(AdFrame.class.getName()).log(Level.SEVERE, null, ex);
}
index++;
if (index >= filearray.length) index = 0;
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
Could you please elaborate?
Thread.sleep(5000);
You have an infinite loop and you tell the EDT Thread to sleep. Since the EDIT is responsible for painting the GUI the painting can never be done because the EDT is constantly sleeping.
Don't use an infinite loop and don't use Thread.sleep().
Instead use a Swing Timer to schedule the screen saver.

Open a console alongside a Swing GUI in java

I am building an application with a Swing GUI. If you would would open this program from the command line, Some text will print (info and error messages). The program is just a normal executable jar file, but for the people who want to see a console I want an option to open a console from the Swing GUI, which displays all these messages, outputted by System.out.print(). I have seen several applications which have such a function, but I don't know how to do this.
The basics are explained at https://blogs.oracle.com/nickstephen/entry/java_redirecting_system_out_and. That example uses logging to redirect stdout and stderr. A simple approach could be to first define an OutputStream that writes to the "console":
package outerr;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.swing.JTextArea;
public class PrintOutErrStream extends ByteArrayOutputStream {
final int maxTextAreaSize = 1000;
private JTextArea textArea;
public PrintOutErrStream(JTextArea textArea) {
this.textArea = textArea;
}
public void flush() throws IOException {
synchronized(this) {
super.flush();
String outputStr = this.toString();
super.reset();
if(textArea.getText().length() > maxTextAreaSize) {
textArea.replaceRange("", 0, 100);
}
textArea.append(outputStr);
}
}
}
Then this is the remaining part of a demo program:
package outerr;
import java.io.PrintStream;
public class StdOutErr extends javax.swing.JFrame {
/** Creates new form StdOutErr */
public StdOutErr() {
initComponents();
PrintOutErrStream poes = new PrintOutErrStream(this.jTextAreaOutErrLog);
System.setErr(new PrintStream(poes, true));
System.setOut(new PrintStream(poes, true));
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this
* method is
* always regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jButtonStdout = new javax.swing.JButton();
jButtonStderr = new javax.swing.JButton();
jPanelOutErrLog = new javax.swing.JPanel();
jScrollPaneOutErrLog = new javax.swing.JScrollPane();
jTextAreaOutErrLog = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());
jButtonStdout.setText("stdout");
jButtonStdout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonStdoutActionPerformed(evt);
}
});
getContentPane().add(jButtonStdout);
jButtonStderr.setText("stderr");
jButtonStderr.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonStderrActionPerformed(evt);
}
});
getContentPane().add(jButtonStderr);
jTextAreaOutErrLog.setColumns(20);
jTextAreaOutErrLog.setRows(5);
jScrollPaneOutErrLog.setViewportView(jTextAreaOutErrLog);
javax.swing.GroupLayout jPanelOutErrLogLayout = new javax.swing.GroupLayout(jPanelOutErrLog);
jPanelOutErrLog.setLayout(jPanelOutErrLogLayout);
jPanelOutErrLogLayout.setHorizontalGroup(
jPanelOutErrLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 835, Short.MAX_VALUE)
.addGroup(jPanelOutErrLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPaneOutErrLog, javax.swing.GroupLayout.DEFAULT_SIZE, 835, Short.MAX_VALUE))
);
jPanelOutErrLogLayout.setVerticalGroup(
jPanelOutErrLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 378, Short.MAX_VALUE)
.addGroup(jPanelOutErrLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPaneOutErrLog, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE))
);
getContentPane().add(jPanelOutErrLog);
pack();
}// </editor-fold>
private void jButtonStdoutActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("message via stdout");
}
private void jButtonStderrActionPerformed(java.awt.event.ActionEvent evt) {
System.err.println("message via stderr");
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(StdOutErr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(StdOutErr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(StdOutErr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(StdOutErr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new StdOutErr().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonStderr;
private javax.swing.JButton jButtonStdout;
private javax.swing.JPanel jPanelOutErrLog;
private javax.swing.JScrollPane jScrollPaneOutErrLog;
private javax.swing.JTextArea jTextAreaOutErrLog;
// End of variables declaration
}

Worker thread blocking GUI repaint

I'm trying to make a login screen for an application. During the login, many SQL calls will be made to a MySQL database, and it may take a few seconds to set everything up. I'd like to display a status screen via Card Layout and update a JLabel while the background thread is running.
Here's the gist of what I have for my Worker Thread:
public class LoginPrepThread extends Thread {
private final UIMain parent;
public LoginPrepThread(UIMain w){
parent = w;
}
public void exec(){
EventQueue.invokeLater(this);
}
public void run(){
try{
//SqlHelper sql = SqlHelper.instance;
sleep(500);
parent.getLoadingLable().setText("Fetching preferences...");
parent.getMainFrame().revalidate();
sleep(500);
parent.getLoadingLable().setText("Scanning workbench...");
parent.getMainFrame().revalidate();
sleep(500);
parent.getLoadingLable().setText("Updating permissions...");
parent.getMainFrame().revalidate();
sleep(500);
parent.getLoadingLable().setText("Finished...Please wait");
parent.getMainFrame().revalidate();
sleep(1000);
parent.getLayout().show(parent.getMainFrame().getContentPane(), "view.main");
}catch(Exception e){
}
}
}
Here is how I am calling it (Inside the event of a JButton, after authenticating):
setActiveProfile(user);
layout.show(frame.getContentPane(), "view.loading");
frame.repaint();
LoginPrepThread pt = new LoginPrepThread(thisTrick);
pt.exec();
I put some dummy events in for now, but the status label doesn't change...any suggestions?
A simple swing worker solved it. I guess I wasn't good enough on my google'ing
public class LoginPrepThread extends SwingWorker<String,String> {
private final UIMain parent;
public LoginPrepThread(UIMain w){
parent = w;
}
#Override
protected String doInBackground() throws Exception {
try{
publish("Fetching preferences...");
Thread.sleep(1000);
publish("Updating permissions...");
Thread.sleep(1000);
publish("Scanning workbench...");
Thread.sleep(1000);
publish("Finalizing...");
Thread.sleep(2000);
publish("Finished...Please wait");
Thread.sleep(1000);
parent.getLayout().show(parent.getMainFrame().getContentPane(), "view.main");
}catch(Exception e){
}
return null;
}
protected void process(List<String> item) {
parent.getLoadingLable().setText(item.get(0));
}
}
Ideally you would be implementing a SwingWorker to perform all your heavy non-GUI related tasks. You should take the advantage of what event-driven programming is all about.
What you need to do would be something like this:
Implement a PropertyChangeListner to your GUI class, listening to updates. Based on the property changes, update the labels. It is always a good practice to have a single GUI class handle all the GUI related update activities.
Create a SwingWorker where you would perform your background-intensive tasks. As and when there're updates available, fire a property change event and let the GUI class know that there's an update.
Here's a small SSCCE example of what you can do:
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.SwingWorker;
/**
*
* #author Sujay
*/
public class SimpleWorkerUI extends javax.swing.JFrame implements PropertyChangeListener{
/**
* Creates new form SimpleWorkerUI
*/
public SimpleWorkerUI() {
initComponents();
Worker worker = new Worker();
worker.addPropertyChangeListener(this);
worker.execute();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Current Status: ");
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()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addContainerGap(327, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addContainerGap(20, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SimpleWorkerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SimpleWorkerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SimpleWorkerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SimpleWorkerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SimpleWorkerUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
#Override
public void propertyChange(PropertyChangeEvent evt) {
if("status".equalsIgnoreCase(evt.getPropertyName())){
String currentStatus = (String) evt.getNewValue();
jLabel2.setText(currentStatus);
}
}
}
class Worker extends SwingWorker<String, String>{
private static final int FINAL_VALUE = 1000;
#Override
protected String doInBackground() throws Exception {
int counter = 0;
while(counter < FINAL_VALUE){
firePropertyChange("status", "", "value is: "+counter);
try{
Thread.sleep(100);
}catch(InterruptedException ixe){
}
counter++;
}
return null;
}
}
You should not perform any long-running operations on the event dispatch thread. You are performing an operation that takes 3 seconds to complete. During those 3 seconds, you are monopolizing the EDT and no other GUI updates can happen.
You started out with good intentions...
public class LoginPrepThread extends Thread {
private final UIMain parent;
public LoginPrepThread(UIMain w){
parent = w;
}
public void exec(){
EventQueue.invokeLater(this);
}
public void run(){
try{
//SqlHelper sql = SqlHelper.instance;
sleep(500);
parent.getLoadingLable().setText("Fetching preferences...");
parent.getMainFrame().revalidate();
sleep(500);
parent.getLoadingLable().setText("Scanning workbench...");
parent.getMainFrame().revalidate();
sleep(500);
parent.getLoadingLable().setText("Updating permissions...");
parent.getMainFrame().revalidate();
sleep(500);
parent.getLoadingLable().setText("Finished...Please wait");
parent.getMainFrame().revalidate();
sleep(1000);
parent.getLayout().show(parent.getMainFrame().getContentPane(), "view.main");
}catch(Exception e){
}
}
}
Basically, what's happening, is when you call exec your actually placing a request onto the EDT to call the run method, which is then been executed within the EDT, causing the blockage.
Also, in your original example, it's pointless to extend from Thread as you never actually start it.
For future reference, I would have a closer look at Concurrency in Java
You are correct (in your self answer), SwingWorker is a much easier solution.

Categories