How to Read a CSV file onto a Text Pane Java - java

OK SO I'm doing a project for class and I've gotten what I need to work working (For now.) What I want to do now is when I click a button a the gui it displays all the data in the Text Pane. So far all it does is print out Grades (Which I understand why) But I want it to print out what is being parsed in the CSV Files. I'm not asking for anyone to magically solve my entire code I have most of that down, I just don't know how to make the button actually display the desired results and it's making me mad because I finally got the code to do what I wanted but I can't see the results. (It works in the Console but when the .jar is run nothing is displayed.)
The Question has been answered Read comments below. Code has been removed. Thank you for your time!

You shouldn't create a new JTextPane everytime your JButton was clicked. Add the pane once to the JFrame and in your ActionListener just set the value via setText:
You never set the content of your csv file to your JTextPane via setText: but you only print it out via System.out.println("Student - " + grades[0] + " "+ grades[1] + " Grade - " + grades[2]);
Here I made you an example. The example doesn't really base on your posted code since it would have been to much effort to look into your whole code and correct it, but it shows everything you need to do in order to make your code work.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Instructor {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setLayout(new BorderLayout());
final JTextPane textPane;
textPane = new JTextPane();
frame.add(textPane,BorderLayout.CENTER);
JButton button = new JButton("Read CSV");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Reset textpanes content so on every button click the new content of the read file will be displayed
textPane.setText("");
String fileResult = "";
try {
BufferedReader csvReader = new BufferedReader(new FileReader("myCSV.csv"));
String line = null;
while ((line = csvReader.readLine()) != null) {
//Do your logic here which information you want to parse from the csv file and which information you want to display in your textpane
fileResult = fileResult + "\n" +line;
}
}
catch(FileNotFoundException ex) {
System.err.println("File was not found");
}
catch(IOException ioe) {
System.err.println("There was an error while reading the file");
}
textPane.setText(fileResult);
}
});
frame.add(button,BorderLayout.SOUTH);
frame.setVisible(true);
}
}
So what did I do:
Create JTextpane not in the ActionListener but once
Read the file in the ActionListener and provide try and catch in order to make sure there is some error handling when the file can't be found or something like this.
Don't print the results of the csv via System.out.println(); but set the results to the JTextPane by calling the setText: method.
I also added a LayoutManger (BorderLayout) instead of adding the JTextPane and the button to the JFrame with NullLayout. This isn't part of the question but if you got time you should change this, since the NullLayout isn't recommended at all (setBounds in your code).

Related

Displaying console output in jtextarea even when other actions occur

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

Scroll down automatically JTextArea for show the last lines added

I have written a Java application that receives information from a server every 10 seconds. I am wanting to display this information on a form.
Currently I am using a JTextArea. However, once the JTextArea is filled up, I cannot see the new information that is added to this JTextArea. Do I need to add scroll bars to the JTextArea? Or is there a completely new different control for GUI forms that is recommended to display information that is added every x seconds?
DefaultCaret tries to make itself visible which may lead to scrolling of a text component within JScrollPane. The default caret behavior can be changed by the DefaultCaret#setUpdatePolicy method.
Assumption that the name of your variable is textArea, you only need to modify the policy of caret:
DefaultCaret caret = (DefaultCaret) textArea.getCaret(); // ←
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); // ←
...
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(textArea);
* Thanks, mKorbel
Do I need to add scroll bars to the JTextArea?
Add the text area to a JScrollPane. See How to Use Scroll Panes for more information & examples.
Here is an example:
import java.awt.*;
import java.io.*;
import javax.swing.*;
class TextAreaScrolling {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
final JTextArea out =
new JTextArea(5,10); // suggest columns & rows
JScrollPane outScroll = new JScrollPane(out);
File f = new File("TextAreaScrolling.java");
try {
Reader reader = new FileReader(f);
out.read(reader, f);
} catch (Exception ex) {
ex.printStackTrace();
}
JOptionPane.showMessageDialog(null, outScroll);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}

JLabel setText not updating text

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);

JTextArea show Caret while setEditable is false

How can I put Caret in JTextArea while setEditable is disabled?
A sample code when I need Caret to be visible:
public void run(){
JFrame frame = new JFrame();
JTextArea text = new JTextArea();
text.setEditable(false);
String line = "added line";
text.append(line);
text.setCaretPosition(text.getCaretPosition() + line.length());
frame.getContentPane().add(text);
frame.setSize(300,300);
frame.setVisible(true);
}
What I want to achieve is that, when the user types within TextArea, characters must not be displayed. Typed characters are redirected to OutputStream and appropriate InputStream is received which will be displayed within TextArea. This works fine, but Caret is hidden because of setEditable(false).
text.getCaret().setVisible(true) and/or text.getCaret().setSelectionVisible(true)
Well, I put here a code fragment which shows the caret but don't let edit the JTextArea. I hope it helps you. It's a little trick which plays with the focus of the text area, when focus is gained, the edition is disabled; but when it's losed, the edition it's possible. In this way, the user is unable to edit it but can see the caret.
public void run() {
JFrame frame = new JFrame();
final JTextArea text = new JTextArea();
text.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent fe) {
text.setEditable(true);
}
public void focusGained(FocusEvent fe) {
text.setEditable(false);
}
});
text.setEditable(true);
String line = "added line";
text.append(line);
text.setCaretPosition(text.getCaretPosition() + line.length());
frame.getContentPane().add(text);
frame.setSize(300,300);
frame.setVisible(true);
}
Notice that the user can move the caret, but he/she can't edit the text
I tried the solution originally proposed by StanislavL. However, other issues emerged. For example, after leaving the JTextArea and focusing back later, the caret would turn invisible again.
I suspect that caret was not implemented as most people would expect to behave. While I saw some authors proposing to re-implement the caret, I successfully achieved visible caret behavior with following small listener:
textArea.getCaret().setVisible(true);
textArea.getCaret().setSelectionVisible(true);
textArea.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
textArea.getCaret().setVisible(true);
textArea.getCaret().setSelectionVisible(true);
}
#Override
public void focusLost(FocusEvent e) {
textArea.getCaret().setSelectionVisible(true);
}
});
On example above, I decided to keep the selection visible even if one leaves the text area.

How can I display red text in a JTextArea?

I want to show error(text) in result in red color after compiling exec file
and display it in textarea of gui using swing in java.
A normal JTextArea doesn't support fancy things like different colors of text. However, there are similar components that do. See http://java.sun.com/docs/books/tutorial/uiswing/components/text.html
JEditorPane can get content formatted in HTML. The official Sun tutorial also gives some insight:
The JTextArea class provides a component that displays multiple lines of text and optionally allows the user to edit the text. If you need to obtain only one line of input from the user, you should use a text field. If you want the text area to display its text using multiple fonts or other styles, you should use an editor pane or text pane. If the displayed text has a limited length and is never edited by the user, use a label.
Here's a quick example of adding text to a JEditorPane using AttributeSet and StyleConstants.
This brings up a little frame with a JEditorPane and you can use it to add text of lots of colors without using HTML.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
public class TextColor extends JFrame implements ActionListener {
JTextPane myTextPane;
JTextArea inputTextArea;
public TextColor() {
super();
JPanel temp = new JPanel(new BorderLayout());
inputTextArea = new JTextArea();
JButton btn = new JButton("Add");
btn.addActionListener(this);
temp.add(btn, BorderLayout.SOUTH);
temp.add(inputTextArea, BorderLayout.NORTH);
this.getContentPane().add(temp, BorderLayout.SOUTH);
myTextPane = new JTextPane();
myTextPane.setBorder(new EtchedBorder());
this.getContentPane().add(myTextPane, BorderLayout.CENTER);
this.setSize(600, 600);
this.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
Color newTextColor = JColorChooser.showDialog(this, "Choose a Color", Color.black);
//here is where we change the colors
SimpleAttributeSet sas = new SimpleAttributeSet(myTextPane.getCharacterAttributes());
StyleConstants.setForeground(sas, newTextColor);
try {
myTextPane.getDocument().insertString(myTextPane.getDocument().getLength(),
inputTextArea.getText(), sas);
} catch (BadLocationException ble) {
ble.printStackTrace();
}
}
public static void main(String args[]) {
new TextColor();
}
}
Smita,
take care to paste snippet of your code so that one can understand where exactly problem is or help is required.
Coming to your problem,
To the best of my knowledge, there is no way to set different colors for different text elements in textArea in java. You can set only one color for all.
Alternative is to use JTextPane.
See if following code helps your cause.
String text = "Some Text..."; //This can be any piece of string in your code like
output of your program...
JTextPane myTextPane = new JTextPane();
SimpleAttributeSet sas = new SimpleAttributeSet(myTextPane.getCharacterAttributes());
// As what error you were referring was not clear, I assume there is some code in your
program which pops out some error statement. For convenience I use Exception here..
if( text.contains("Exception") ) //Checking if your output contains Exception...
{
StyleConstants.setForeground(sas, Color.red); //Changing the color of
StyleConstants.setItalic(sas, true);
try
{
myTextPane.getDocument().insertString
(
myTextPane.getDocument().getLength(),
text + "\n",
sas
);
}
catch( BadLocationException ble )
{
text.append(ble.getMessage());
}
}
else
{
StyleConstants.setForeground(sas, Color.GREEN);
try
{
myTextPane.getDocument().insertString
(
myTextPane.getDocument().getLength(),
text + "\n",
sas
);
}
catch(BadLocationException ble)
{
text.append(ble.getMessage());
}
}
I guess this will solve your problem with few modifications.
Thanks.
Sushil

Categories