I found this code here: how to display console output in java JTextarea one by one in a loop when button action is triggered
and it displays console output in a jtextarea.
I have added this class as action in a jmenuitem. So that it appears when I want and when I run other classes it will show the output there. However when I launch it, it works properly, but if I try and launch another class which will show output in console and accept userinput, the jtextarea which is supposed to show console output at the same time, it freezes. How could I make it so that it stays tuned, despite invoking other classes/frames? Thanks in advance
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DynamicWrite implements ActionListener
{
JFrame frame = new JFrame("TextArea");
JTextArea tArea = new JTextArea(10,20);
JButton button = new JButton("Click");
JScrollPane pane = new JScrollPane(tArea);
SwingWorker worker;
String s= "Java is an Object Oriented Programming langauge...Java is static typed language...asbfldfjsdj";//some random String
public void prepareAndShowGUI()
{
Container container = frame.getContentPane();
container.add(pane);container.add(button,BorderLayout.NORTH);
tArea.setLineWrap(true);
tArea.setWrapStyleWord(true) ;
button.addActionListener(this);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==button)
{
tArea.setText("");
if (worker!=null)
{
worker.cancel(true);
}
worker = new SwingWorker()
{
#Override
protected Integer doInBackground()//Perform the required GUI update here.
{
try
{
for(int i = 0;i<s.length();i++)
{
tArea.append(String.valueOf(s.charAt(i)));
Thread.sleep(5);
}
}catch(Exception ex){}
return 0;
}
};
worker.execute();//Schedules this SwingWorker for execution on a worker thread.
}
}
public static void main(String st[])
{
DynamicWrite dyna = new DynamicWrite();
dyna.prepareAndShowGUI();
}
}
The example you have (apart from from being a bad example as outlined by Hovercraft Full of Eels) has nothing to do with redirecting console output.
If you want to redirect the standard out to the text area, take a look at How to set output stream to TextArea for an example
If you want to redirect the output of some other process, then you can only do this if you've launched the process yourself, there's no (easy way that I know of) to connect to the standard out/in of another running process (that your program didn't start itself directly)
Check out Printing a Java InputStream from a Process for an example
Related
I have two Jframes where frame1 has some text fields and when a button on frame1 is clicked, I open another JFrame which contains a search box and a JTable containing search results.
When I click on a result row on JTable, I want that particular values to be reflected in the frame1 text fields.
I tried passing the JFrame1's object as a parameter but I have no clear idea on how to achieve this.
Any help would be highly appreciated.
Thanks
First of all, your program design seems a bit off, as if you are using a JFrame for one of your windows where you should in fact be using a JDialog since it sounds as if one window should be dependent upon the other.
But regardless, you pass references of GUI objects the same as you would standard non-GUI Java code. If one window opens the other (the second often being the dialog), then the first window usually already holds a reference to the second window and can call methods off of it. The key often is when to have the first window call the second's methods to get its state. If the second is a modal dialog, then the when is easy -- immediately after the dialog returns which will be in the code immediately after you set the second dialog visible. If it is not a modal dialog, then you probably want to use a listener of some sort to know when to extract the information.
Having said this, the details will all depend on your program structure, and you'll need to tell us more about this if you want more specific help.
For a simple example that has one window open another, allows the user to enter text into the dialog windows JTextField, and then places the text in the first window's JTextField, please have a look at this:
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WindowCommunication {
private static void createAndShowUI() {
JFrame frame = new JFrame("WindowCommunication");
frame.getContentPane().add(new MyFramePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
// let's be sure to start Swing on the Swing event thread
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class MyFramePanel extends JPanel {
private JTextField field = new JTextField(10);
private JButton openDialogeBtn = new JButton("Open Dialog");
// here my main gui has a reference to the JDialog and to the
// MyDialogPanel which is displayed in the JDialog
private MyDialogPanel dialogPanel = new MyDialogPanel();
private JDialog dialog;
public MyFramePanel() {
openDialogeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openTableAction();
}
});
field.setEditable(false);
field.setFocusable(false);
add(field);
add(openDialogeBtn);
}
private void openTableAction() {
// lazy creation of the JDialog
if (dialog == null) {
Window win = SwingUtilities.getWindowAncestor(this);
if (win != null) {
dialog = new JDialog(win, "My Dialog",
ModalityType.APPLICATION_MODAL);
dialog.getContentPane().add(dialogPanel);
dialog.pack();
dialog.setLocationRelativeTo(null);
}
}
dialog.setVisible(true); // here the modal dialog takes over
// this line starts *after* the modal dialog has been disposed
// **** here's the key where I get the String from JTextField in the GUI held
// by the JDialog and put it into this GUI's JTextField.
field.setText(dialogPanel.getFieldText());
}
}
class MyDialogPanel extends JPanel {
private JTextField field = new JTextField(10);
private JButton okButton = new JButton("OK");
public MyDialogPanel() {
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
okButtonAction();
}
});
add(field);
add(okButton);
}
// to allow outside classes to get the text held by the JTextField
public String getFieldText() {
return field.getText();
}
// This button's action is simply to dispose of the JDialog.
private void okButtonAction() {
// win is here the JDialog that holds this JPanel, but it could be a JFrame or
// any other top-level container that is holding this JPanel
Window win = SwingUtilities.getWindowAncestor(this);
if (win != null) {
win.dispose();
}
}
}
You'd do a very similar technique to get information out of a JTable.
And again, if this information doesn't help you, then please tell us more about your program including showing us some of your code. The best code to show is a small compilable example, an SSCCE similar to what I've posted above.
I have two Jframes where frame1 has some text fields and when a button on frame1 is clicked, I open another JFrame which contains a search box and a JTable containing search results.
When I click on a result row on JTable, I want that particular values to be reflected in the frame1 text fields.
I tried passing the JFrame1's object as a parameter but I have no clear idea on how to achieve this.
Any help would be highly appreciated.
Thanks
First of all, your program design seems a bit off, as if you are using a JFrame for one of your windows where you should in fact be using a JDialog since it sounds as if one window should be dependent upon the other.
But regardless, you pass references of GUI objects the same as you would standard non-GUI Java code. If one window opens the other (the second often being the dialog), then the first window usually already holds a reference to the second window and can call methods off of it. The key often is when to have the first window call the second's methods to get its state. If the second is a modal dialog, then the when is easy -- immediately after the dialog returns which will be in the code immediately after you set the second dialog visible. If it is not a modal dialog, then you probably want to use a listener of some sort to know when to extract the information.
Having said this, the details will all depend on your program structure, and you'll need to tell us more about this if you want more specific help.
For a simple example that has one window open another, allows the user to enter text into the dialog windows JTextField, and then places the text in the first window's JTextField, please have a look at this:
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WindowCommunication {
private static void createAndShowUI() {
JFrame frame = new JFrame("WindowCommunication");
frame.getContentPane().add(new MyFramePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
// let's be sure to start Swing on the Swing event thread
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class MyFramePanel extends JPanel {
private JTextField field = new JTextField(10);
private JButton openDialogeBtn = new JButton("Open Dialog");
// here my main gui has a reference to the JDialog and to the
// MyDialogPanel which is displayed in the JDialog
private MyDialogPanel dialogPanel = new MyDialogPanel();
private JDialog dialog;
public MyFramePanel() {
openDialogeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openTableAction();
}
});
field.setEditable(false);
field.setFocusable(false);
add(field);
add(openDialogeBtn);
}
private void openTableAction() {
// lazy creation of the JDialog
if (dialog == null) {
Window win = SwingUtilities.getWindowAncestor(this);
if (win != null) {
dialog = new JDialog(win, "My Dialog",
ModalityType.APPLICATION_MODAL);
dialog.getContentPane().add(dialogPanel);
dialog.pack();
dialog.setLocationRelativeTo(null);
}
}
dialog.setVisible(true); // here the modal dialog takes over
// this line starts *after* the modal dialog has been disposed
// **** here's the key where I get the String from JTextField in the GUI held
// by the JDialog and put it into this GUI's JTextField.
field.setText(dialogPanel.getFieldText());
}
}
class MyDialogPanel extends JPanel {
private JTextField field = new JTextField(10);
private JButton okButton = new JButton("OK");
public MyDialogPanel() {
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
okButtonAction();
}
});
add(field);
add(okButton);
}
// to allow outside classes to get the text held by the JTextField
public String getFieldText() {
return field.getText();
}
// This button's action is simply to dispose of the JDialog.
private void okButtonAction() {
// win is here the JDialog that holds this JPanel, but it could be a JFrame or
// any other top-level container that is holding this JPanel
Window win = SwingUtilities.getWindowAncestor(this);
if (win != null) {
win.dispose();
}
}
}
You'd do a very similar technique to get information out of a JTable.
And again, if this information doesn't help you, then please tell us more about your program including showing us some of your code. The best code to show is a small compilable example, an SSCCE similar to what I've posted above.
I have a Java program where I plan to take input from GUI, and use that input later for processing in main(). I am using Eclipse.
I am sending an HW object(called HWObj) to the GUI JFrame, and checking for a boolean field in the object to continue processing in main().
InputWindow is custom object which extends JPanel implements ActionListener
It contains a reference to the current JFrame(parentFrame). On clicking a JButton in InputWindow, I have written a custom ActionListener which sets the value of HWObj.check to true and disposes the parentFrame. This should cause execution to resume in main().
Code for HW class is as below :
import java.awt.*;
import javax.swing.*;
public class HW {
//globals
boolean check;
public HW() {
//initialisations
check = false;
}
public static void main(String args[]) {
final HW problem = new HW();
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Create and set up the window.
JFrame frame = new JFrame("Select folders");
frame.setPreferredSize(new Dimension(640, 480));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
InputWindow Directories = new InputWindow(problem, frame);
Directories.setOpaque(true);
frame.add(Directories);
//Display the window.
frame.pack();
frame.setVisible(true);
}
});
} catch(Exception e) {
System.out.println("Exception:"+e.getLocalizedMessage());
}
while(!problem.finish);
//Do processing on problem
System.out.println("Done");
}
}
The Actionlistener in the gui is as follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class InputWindow extends JPanel
implements ActionListener {
private static final long serialVersionUID = 4228345704162790878L;
HW problem;
JFrame parentFrame;
//more globals
public InputWindow(HW problem, JFrame parentFrame) {
super();
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
this.parentFrame = parentFrame;
this.problem = problem;
JButton finishButton = new JButton("Finish");
finishButton.setActionCommand("fin");
finishButton.addActionListener(this);
gbc.gridx = 0;
gbc.gridy = 0;
this.add(finishButton, gbc);
//Initialize buttons and text areas and labels
//Code removed for ease of reading
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command.equals("fin")) {
//Do a lot of stuff, then
this.removeAll();
parentFrame.dispose();
problem.check = true;
}
}
}
I have checked, and the control to this function comes normally on button click.
Now, I would expect it to return to main, and exit the while loop, and continue processing.
This does not happen. The debugger in eclipse shows only the main thread running, and when I try to pause it, I see that the thread is stuck in the while loop. But if I try to step through, it exits the while loop as expected, and continues. However, it gets remains stuck in the while loop until I manually try to debug it.
What is the problem? Why is it not resuming the main thread as expected?
How do I resolve this issue?
Your problem is to do with how the Java memory model works. The loop in your main thread will be checking a stale value of check.
When you enter the debugger, the memory is forced to be updated, so that's why it starts working at that point.
If you mark your variable as volatile, that will force the JVM to ensure that all threads are using the up-to-date value:
volatile boolean check;
You can read more about volatile and the Java memory model in the documentation.
It looks like you're using a JFrame where you should be using a modal JDialog. If you use the modal JDialog for an input window, you will know exactly when it is "finished" since code flow will resume from the calling code from right after when the dialog was set visible.
Either that or if you are trying to swapviews, then use a CardLayout to swap your view, and use an observer type pattern to listen for change of state.
So I've been trying to work with the code in this post for making a console window in a JTextArea. That code seems to be functioning with mine, but I'm running into an odd problem.
My program: I'm basically building a quick and dirty gui for a command line tool I made recently. The only thing that the gui contains is a button that says "Start Automation Engine" and then it has a JTextArea that is supposed to display any text my program sends to System.out.println().
At the moment it displays nothing, though the program itself is running and working (and should be displaying output as a result.) I have noticed that when I click the button on my gui the button stays depressed while the program runs. This has lead me to believe that the JFrame is not updating while the program is running, thus the JTextArea, as it's child, is not updating. This is not so good...
Is there a way to get that JTextArea to update while the program is running in the background?
Here's my code for the JFrame, btw, if you'd like to look at it to get a better idea of what I'm talking about. It was mostly constructed in WindowBuilder in Eclipse. The only thing I did was add a button listener to startAutmoatorEngineButton and then add the last few lines of the initalize() method to set the JTextArea (engineOutput) as the System.out.
public class EngineGUI {
private JFrame frmAutomatorEngine;
private File logPath = new File("<redacted>", "<redacted>");
private File masterFile = new File("<redacted>", "<redacted>");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EngineGUI window = new EngineGUI();
window.frmAutomatorEngine.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public EngineGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmAutomatorEngine = new JFrame();
frmAutomatorEngine.setType(Type.UTILITY);
frmAutomatorEngine.setResizable(false);
frmAutomatorEngine.setTitle("Automator Engine");
frmAutomatorEngine.setBounds(100, 100, 636, 335);
frmAutomatorEngine.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frmAutomatorEngine.setJMenuBar(menuBar);
JMenu mnEngine = new JMenu("Engine");
menuBar.add(mnEngine);
JMenuItem mntmLoadMasterFile = new JMenuItem("Load Master File...");
mnEngine.add(mntmLoadMasterFile);
JMenuItem mntmExit = new JMenuItem("Exit");
mnEngine.add(mntmExit);
frmAutomatorEngine.getContentPane().setLayout(null);
JTextArea engineOutput = new JTextArea();
engineOutput.setBounds(10, 48, 600, 217);
frmAutomatorEngine.getContentPane().add(engineOutput);
JButton startAutomatorEngineButton = new JButton("Start Automator Engine");
startAutomatorEngineButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MasterFile master = null;
try {
master = new MasterFile(masterFile, logPath);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
AutomationLoop theLoop = new AutomationLoop(master);
theLoop.startLoop();
}
});
startAutomatorEngineButton.setBounds(441, 11, 169, 23);
frmAutomatorEngine.getContentPane().add(startAutomatorEngineButton);
//Set up textArea to be used as output stream for program
TextAreaOutputStream outputStream = new TextAreaOutputStream(engineOutput);
PrintStream printStream = new PrintStream(outputStream);
System.setOut(printStream);
//System.setErr(printStream);
}
}
It's hard to tell for sure because I don't know what your AutomationLoop and TextAreaOutputStream classes do, but it sounds like a threading problem.
All your Swing code needs to execute in the Event Dispatch Thread. If you have a long running code that is not updating the GUI, then you probably want it running another thread, otherwise the GUI doesn't get a chance to update. From your behavior, it sounds like theLoop.startLoop() is running in the Event Dispatch Thread, and so the GUI never gets a chance to update itself.
Does theLoop.startLoop() start a new thread? If not it probably should; otherwise until that code finishes executing, your GUI will not update.
I am trying to update a JLabel by using the setText() method, but I can't redraw JLabel. Do I have to use the repaint() method to do that?
Here is the part of code, I do not get any errors, but it is not updating the JLabel.
public void actionPerformed(ActionEvent e) {
fc = new JFileChooser();
if(e.getSource() == addButton) {
int returnVal = fc.showOpenDialog(Main.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
filesList = fc.getSelectedFiles();
setFilesList(filesList);
StringBuilder logString = new StringBuilder();
logString.append("Files to Convert " + "\n");
for(int i = 0; i < getFiles().length; i++) {
logString.append(filesList[i].getAbsolutePath());
}
//JLabel log = new JLabel(); created above.
log.setText(logString.toString());
} else {
//log.append("Open command cancelled by user." + newline);
}
//log.setCaretPosition(log.getDocument().getLength());
}
}
JLabel requires no repaint call. Simply calling setText(...) will change the label's text, and that is all that is required.
I wonder if your problem is a concurrency issue, that you are doing a long-running process on the Swing event thread and that this is preventing your label from updating its text.
If so, then consider doing your long-running process in a background thread such as that provided by a SwingWorker, and then updating your JLabel's text on the Swing thread, such as can be done via the SwingWorker's publish/process methods.
For more on this, please have a look at the Lesson: Concurrency in Swing tutorial.
Also Mario De... is correct about not being able to print simple new-lines on a JLabel. 1+ to his answer.
I'm a bit stumped on how the repainting of frames/component works in Java. You can Paint(Graphics g), update(Graphics g) which according to the javadoc just calls paint(g). Finally there's also repaint()...
If none of those seem to work, wouldn't it just be easier to create the label only at the line where you are currently trying to set the text?
Edit: there is also the option of using an ineditable textArea. Not only can it display the standard newline character, but you can put it in a jScrollPane, which is handy when you have lots of files in the log, and you don't need to repaint text components to display updated text. The bonus is magnificent imo...
This simple example works for me so problem is not JLabel but some bug in other part of your source code. Please post full source code.
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Application {
public static void main(String[] args) {
JFrame frame = new JFrame("JLabel test");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Panel p = new Panel();
p.setLayout(new GridLayout());
Button button = new Button("Change");
final JLabel label = new JLabel(Long.toString(Long.MAX_VALUE));
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
label.setText(Long.toString(e.getWhen()));
}
});
p.add(button);
p.add(label);
frame.add(p, BorderLayout.NORTH);
frame.pack();
}
}
I have run into a similar problem. I tried to setText("Good Bye") in actionPerformed() in an exit button ActionListener before disposing my JFrame right there, but the text was not changing.
Eventually I realized that my label was not getting updated as I was disposing the frame in the anonymous ActionListener class. After I had let the code leave ActionListener.actionPerformed(), the label text got updated.
I had to dispose my JFrame in a new thread though to ensure that:
actionPerformed is finished so that the main thread returns from the anonymous nested class and updates the label in the main class.
A new thread is started which waits for a second to allow "Good Bye" to be read.
This new thread the disposes the frame.
repaint() won't work here.
Simply use label_name.paintImmediately(label_name.getVisibleRect());
It will get updated right away.
Please try -
jlabel.setText("Your Text");
jLabel.setVisisble(true);