JTable reading text files from second line - java

I'm working on JTable that reads text from a .txt file. the txt file gets updated dynamically after 3 sec. Now when I run the application, everything is good except that the output of .txt files comes in JTable from the second line. The first line of txt file doesn't appear on my JTable. Can anyone help? Here's the code:
public class InterfaceFrame extends JFrame implements ActionListener{
public static void main(String[] args) throws
URISyntaxException,
IOException,
InterruptedException {
panel.setSize(100,100);
panel.add(table);
model.fireTableStructureChanged();
table.setModel(model);
InsertFileToJtable model = new InsertFileToJtable();
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
RowSorter<TableModel> sorter =
new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
JScrollPane scrollpane = new JScrollPane(table);
panel.add(scrollpane, BorderLayout.CENTER);
JButton button = new JButton("Show View");
panel.add( button, BorderLayout.SOUTH );
tabbedPane.addTab("Process",null,scrollpane,"");
}
I might be doin something wrong in making the text file. Here's the code which generated the .txt file.:
import java.io.*;
import java.util.StringTokenizer;
public class GetProcessList
{
private String GetProcessListData()
{
Process p;
Runtime runTime;
String process = null;
try {
System.out.println("Processes Reading is started...");
//Get Runtime environment of System
runTime = Runtime.getRuntime();
//Execute command thru Runtime
p=runTime.exec("ps -e"); //For Linux
//Create Inputstream for Read Processes
InputStream inputStream = p.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//Read the processes from sysrtem and add & as delimeter for tokenize the output
String line = bufferedReader.readLine();
process = "&";
while (line != null) {
line = bufferedReader.readLine();
process += line + "&";
}
//Close the Streams
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
System.out.println("Processes are read.");
} catch (IOException e) {
System.out.println("Exception arise during the read Processes");
e.printStackTrace();
}
return process;
}
void showProcessData()
{
try {
//Call the method For Read the process
String proc = GetProcessListData();
//Create Streams for write processes
//Given the filepath which you need.Its store the file at where your java file.
OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(new FileOutputStream("ProcessList.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
//Tokenize the output for write the processes
StringTokenizer st = new StringTokenizer(proc, "&");
while (st.hasMoreTokens()) {
bufferedWriter.write(st.nextToken()); //Write the data in file
bufferedWriter.newLine(); //Allocate new line for next line
}
//Close the outputStreams
bufferedWriter.close();
outputStreamWriter.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Heres the code that reads ProcessList.txt and gives output into JTable:
import java.io.*;
import java.awt.*;
import java.util.*;import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
public class InsertFileToJtable extends AbstractTableModel{
Vector data;
Vector columns;
private String[] colNames = {"<html><b>PID</b></html>","<html><b>TTY</b</html>",<html> <b>time</b></html>","<html><b>Process Name</b></html>",};
public InsertFileToJtable() {
String line;
data = new Vector();
columns = new Vector();
try {
FileInputStream fis = new FileInputStream("ProcessList.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");
while (st1.hasMoreTokens())
columns.addElement(st1.nextToken());
while ((line = br.readLine()) != null) {
StringTokenizer st2 = new StringTokenizer(line, " ");
while (st2.hasMoreTokens())
data.addElement(st2.nextToken());
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int getRowCount() {
return data.size() / getColumnCount();
}
public int getColumnCount() {
return columns.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (String) data.elementAt((rowIndex * getColumnCount())
+ columnIndex);
}
#Override
public String getColumnName(int column) {
return colNames[column];
}
#Override
public Class getColumnClass(int col){
return getValueAt(0,col).getClass();
}
}

I'd do this a little differently, avoiding an intermediate text file. Instead,
Use ProcessBuilder, which "can be invoked repeatedly from the same instance." You can read the output as shown here and parse it into a suitable data structure, e.g. List<List<String>>.
ProcessBuilder pb = new ProcessBuilder("ps -ef");
Start a javax.swing.Timer having a three second period; invoke pb.start() in the timer's listener.
When parsing concludes, fireTableDataChanged() in your AbstractTableModel, shown here.
Presto, your table updates with the latest result every three seconds.

Related

How to make a String available in other classes

I've managed to make the input into a string which is available within the same class but I want to make it so the input string can be available in different classes. Current class is OpenDetails and I want the string selectedFile to be available in a different class called OpenFileInfo. How would I set it so the result from selectedFile can be stored in either selectedRequirement or make it available in other classes?
I'm new to Java so if someone could help thank you.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class OpenFile
{
String selectedRequirement = "";
public static void main(String a[])
{
JFrame parent = new JFrame();
String selectedFile;
selectedFile = JOptionPane.showInputDialog(parent, "Add a new module");
if(selectedFile.equalsIgnoreCase(selectedFile)){
//Makes the user input case insensitive
}
final JTextArea edit = new JTextArea(60,100);
JButton read = new JButton("Open "+ selectedFile +".txt");
read.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
FileReader reader = new FileReader(selectedFile + ".txt");
BufferedReader br = new BufferedReader(reader);
edit.read( br, null );
br.close();
edit.requestFocus();
}
catch(Exception e2) { System.out.println(e2); }
}
});
JButton write = new JButton("Save "+ selectedFile + ".txt");
write.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
FileWriter writer = new FileWriter(selectedFile + ".txt");
BufferedWriter bw = new BufferedWriter( writer );
edit.write( bw );
bw.close();
edit.setText("");
edit.requestFocus();
}
catch(Exception e2) {}
}
});
System.out.println("Module: " + selectedFile);
JFrame frame = new JFrame("Requirements");
frame.getContentPane().add( new JScrollPane(edit), BorderLayout.NORTH );
frame.getContentPane().add(read, BorderLayout.WEST);
frame.getContentPane().add(write, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
As you are running from a static context, you need to define selectedRequirement as static:
private static String selectedRequirement = "";
To make selectedRequirement equal to selectedFile, simply say selectedRequirement = selectedFile; towards the end of the main function (maybe where you print it already).
To make selectedRequirement available to other classes, you need to create a "getter function" in the OpenFIle class (outside of the main function) like:
public String getSelectedRequirement(){
return selectedRequirement;
}
As pointed out in the comments, it would be a good idea for you (or anyone who finds this in the future) to look at some tutorials on getters, setters, and general encapsulation.

JTextArea saving to txt

I'm making a program that requires to save user input. So I would like to know how to save JTextArea to text file and when you close and re-open program text is still in JTextArea.
Also sorry for my bad grammar.
package main;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;
public class main extends JFrame {
JLabel statusbar;
public main() {
initUI();
}
public final void initUI() {
JPanel panel = new JPanel();
statusbar = new JLabel("");
statusbar.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
panel.setLayout(null);
JTextArea area1;
area1 = new JTextArea(90, 25);
area1.setBounds(20, 20, 200, 25);
area1.setBackground(Color.white);
area1.setForeground(Color.BLACK);
area1.setText("");
panel.add(area1);
add(panel);
add(statusbar, BorderLayout.SOUTH);
setTitle("Viskis");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton o = (JButton) e.getSource();
String label = o.getText();
statusbar.setText("");
} }
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
main ms = new main();
ms.setVisible(true);
new main();
}
});
}
}
Here are some helper methods to help you read and write from Files. Look in the JavaDoc for getText() and setText() for getting and setting the text of the JTextArea.
I recommend reading the SwingWorker tutorials and using these methods in a SwingWorker, lest you lock up your GUI while saving/reading the Files
/**
* Writes a String text to a File specified by filename
*
* #param filename The filename/path of the File we would like to write the text to
* #param text The text to write to the File
*
*/
public static void writeToFile(String filename, String text) {
BufferedWriter writer = null; // This could go in a try-with-resources if you wanna get fancy
try {
writer = new BufferedWriter(new FileWriter(new File(filename))); // Open a File for writing
writer.write(text); // write the text to the file
} catch ( IOException e) {
/* We could not open the File for writing, or could not write to the File */
} finally {
try {
if (writer != null) {
writer.close(); // we are done writing to the File, close the connection
}
} catch (IOException e) {
/* We could not close the connection to the File */
}
}
}
/**
* Reads all lines from a File specified by filename
*
* #param filename The filename/path of the File we would like to read text from
*
* #return An list of Strings containing each line of the File
*/
public static List<String> readFromFile(String filename) {
BufferedReader reader = null; // This could go in a try-with-resources if you wanna get fancy
List<String> lines = new ArrayList<String>();
try {
reader = new BufferedReader(new FileReader(new File(filename))); // Open a File for writing
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
/* We could not open the File for reading, or could not read from the File */
} finally {
try {
if (reader != null) {
reader.close(); // we are done reading from the File, close the connection
}
} catch (IOException e) {
/* We could not close the connection to the File */
}
}
return lines;
}
Write and read file on a swing thread is not recommanded. Use mvc pattern. Create a data model with field binded to your component. Create an adapter to update model and write file
Then your component will always up to date with your model
Use model to write data(with the adapter) and read file will update your model with the adapter
For exemple, Focus listener will call adapter to do the task. Or you can use an obseevee pattern when you Object if modified.
This code worked for me with 'nam' as the current date and 'name' an input in a jTextField.
try {
con=Connect.ConnectDB();
pst.execute();
Date date=new Date();
SimpleDateFormat sd=new SimpleDateFormat("dd-mm-yyyy-h-m-s");
String nam= sd.format(date);
String name=CaseNoField.getText();
CaseNoField.setText("");
FileWriter writer=new FileWriter( "C:\\path"+name+"("+nam+")"+".txt");
BufferedWriter bw=new BufferedWriter( writer );
JCaseArea.write( bw );
bw.close();
JCaseArea.setText("");
JCaseArea.requestFocus();
JOptionPane.showMessageDialog(null, "Case Saved");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}

Why my JTextArea is not displaying properly from the while loop?

My GUI application allows users to type into a JTextField object stating the name of a file to open and display its contents onto a JTextArea object. If the entered information consists of the file name then it shall retrieve its contents otherwise, in other case, it shall be a directory then it shall display the files and folders. Right now, I'm stuck as in the setText() of my JTextArea does not display contents correctly. It only display once which means to say there's some problem with my while loop. Could you guys help me out here please?
Please note the code below has been altered to the correct working version provided all the helpful contributors below.
Main class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
class MyFileLister extends JPanel implements ActionListener {
private JLabel prompt = null;
private JTextField userInput = null;
private JTextArea textArea = null;
public MyFileLister()
{
prompt = new JLabel("Enter filename: ");
prompt.setOpaque(true);
this.add(prompt);
userInput = new JTextField(28);
userInput.addActionListener(this);
this.add(userInput);
textArea = new JTextArea(10, 30);
textArea.setOpaque(true);
JScrollPane scrollpane = new JScrollPane(textArea);
this.add(textArea, BorderLayout.SOUTH);
}
Scanner s = null;
File af = null;
String[] paths;
public void actionPerformed(ActionEvent f)
{
try
{
s = new Scanner(new File(userInput.getText()));
while(s.hasNextLine())
{
String as = s.nextLine();
textArea.append(as + "\n");
textArea.setLineWrap(truea);
}
}
catch(FileNotFoundException e)
{
af = new File(userInput.getText());
paths = af.list();
System.out.println(Arrays.toString(paths));
String tempPath = "";
for(String path: paths)
{
tempPath += path + "\n";
}
textArea.setText(tempPath);
}
}
}
Driver class:
import java.util.*;
import java.awt.*;
import javax.swing.*;
class TestMyFileLister {
public static void main(String [] args)
{
MyFileLister thePanel = new MyFileLister();
JFrame firstFrame = new JFrame("My File Lister");
firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
firstFrame.setVisible(true);
firstFrame.setSize(500, 500);
firstFrame.add(thePanel);
}
}
Here's one of the screenshot which I have to achieve. It shows that when the user's input is on a directory it displays the list of files and folders under it.
I tried to put in an if statement to see if I can slot in a show message dialog but I seriously have no idea where to put it.
public void actionPerformed(ActionEvent f)
{
try
{
s = new Scanner(new File(userInput.getText()));
if(af == null)
{
System.out.println("Error");
}
while(s.hasNextLine())
{
String as = s.nextLine();
textArea.append(as + "\n");
textArea.setLineWrap(true);
}
}
catch(FileNotFoundException e)
{
af = new File(userInput.getText());
paths = af.list();
System.out.println(Arrays.toString(paths));
String tempPath = "";
for(String path: paths)
{
tempPath += path + "\n";
}
textArea.setText(tempPath);
}
}
You're outputting text to textArea based on the Last File on the list !!! ( don't set your text to JTextArea directly inside a loop, the loop is fast and the UI can't render it, so concatenate the string then set it later after the loop finishes ).
// These lines below are causing only last file shown.
for(String path: paths)
{
textArea.setText(path);
}
Here is your modified version for MyFileLister class :
public class MyFileLister extends JPanel implements ActionListener {
private JLabel prompt = null;
private JTextField userInput = null;
private JTextArea textArea = null;
public MyFileLister()
{
prompt = new JLabel("Enter filename: ");
prompt.setOpaque(true);
this.add(prompt);
userInput = new JTextField(28);
userInput.addActionListener(this);
this.add(userInput);
textArea = new JTextArea(10, 30);
textArea.setOpaque(true);
JScrollPane scrollpane = new JScrollPane(textArea);
this.add(scrollpane, BorderLayout.SOUTH);
}
Scanner s = null;
File af ;
String[] paths;
public void actionPerformed(ActionEvent f)
{
try
{
s = new Scanner(new File(userInput.getText()));
while(s.hasNext())
{
String as = s.next();
textArea.setText(as);
}
}
catch(FileNotFoundException e)
{
af = new File(userInput.getText());
paths = af.list();
System.out.println(Arrays.toString(paths));
String tempPath="";
for(String path: paths)
{
tempPath+=path+"\n";
}
textArea.setText(tempPath);
}
}
}
Output :
Code:
public void actionPerformed(ActionEvent ae) {
try (Scanner s = new Scanner(new File(userInput.getText()))) {
while (s.hasNextLine()) {
String as = s.nextLine();
textArea.append(as + "\n");
textArea.setLineWrap(true);
}
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this,
"File not found",
"No File Error",
JOptionPane.ERROR_MESSAGE);
}
}
Notes:
Just try to read your file line by line so you can copy the same structure from your file into your JTextArea.
Use setLineWrap method and set it to true
read here http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html#setLineWrap(boolean)
use append method in order to add text to end of your JTextArea
read here
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html#append(java.lang.String)
Use JOptionPane to show error message to an user

How to add a JPanel from a diffent class to another JPanel on mouse click

I've been trying to populate a JPanel (here reffered to as 'BulletinsJPanel') in a class reffered to as 'Home.java' with a textfield (here reffered to as 'readerSetTxtJTextField') when a JLabel is clicked. ReaderPanel is in another class called 'Reader.java' which is supposed to read the contents of a text file and populate a JTextField object with a line of text.
I'm using netbeans which doesn't show me any code error highlights.
I would really appreciate some help in getting the textfield 'readerSetTxtJTextField' to show. Thanks a lot in advance.
Heres my code:
// The class Home
package Panels;
public class Home extends javax.swing.JPanel {
// Here's a method in the class 'Home.java' which should populate 'BulletinsJPanel' with the contents
private void MouseClickedList(java.awt.event.MouseEvent evt) {
BulletinsJPanel.add(Reader.readerMouseClickedList());
BulletinsJPanel.revalidate();
BulletinsJPanel.repaint();
}
}
// Now the class Reader below
package Database;
public class Reader {
public static JTextField readerSetTxtJTextField;
public static JTextField readerMouseClickedList() {
try {
// Our code
String fileURL = "/D:/TestFile.txt/";
List<String[]> matches = new ArrayList<String[]>();
String finalText;
FileInputStream fileInputStream = new FileInputStream(fileURL);
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
String stringLine;
while((stringLine = bufferedReader.readLine()) != null) {
String splittable = "[\\s]", splitLenth = "{955}";
if(stringLine.startsWith("2013001")) {
String[] splits = stringLine.split(splittable + splitLenth );
matches.add(splits);
}
}
dataInputStream.close();
for(String[] items : matches) {
int itemsLength = items.length;
int i;
for(i = 0; i <= itemsLength; i++) {
finalText = (items[i]);
readerSetTxtJTextField = new JTextField();
readerSetTxtJTextField.setText(finalText);
}
}
} catch (Exception e) {
// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return readerSetTxtJTextField;
}
}
As a quick fix to your error , Change :
for(i = 0; i <= itemsLength; i++)
to
for(i = 0; i < itemsLength; i++)

Drag and drop TXT file into TextArea

Here I have text area called sourceTx in which I drag and drop files, then I read content of that file with BufferedReader. As you can see from bellow code I set file from which I am reading content with absolutepath.
So, when I drag an drop some .txt file it works, it reads content and put it in text area, but when I also drag and drop some folder for example it also reads some content and put it in text area.
So I want set this drag and drop to read only .txt files? How I can get that?
Here is code of that method:
public void dragDrop(){
sourceTx.setOnDragOver(new EventHandler <DragEvent>() {
#Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
if(db.hasFiles()){
event.acceptTransferModes(TransferMode.ANY);
for(File file:db.getFiles()){
String absolutePath = file.getAbsolutePath();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(absolutePath)));
String line = null;
String text = "";
String nl = System.getProperty("line.separator", "\n");
while((line = br.readLine()) != null)
text += line + nl;
sourceTx.setText( text.trim() );
} catch (Exception e) {
MessageBox.show(MessageBoxType.ERROR, I18n.localize("File Error"), I18n.localize("Error while reading content from selected file"));
} finally{
if(br != null)
try {
br.close();
} catch (Exception e) {}
}
}
}else{
event.setDropCompleted(false);
}
event.consume();
}
});
}
Hi there try to read your file with recursion
...
for (File file : db.getFiles()) {
sourceTx.setText(handleFile(file));
}
...
private String handleFile(File file) {
String ret = "";
if (file.isDirectory()) {
for (File f : file.listFiles()) {
ret.concat(handleFile(f));
}
} else {
/*this is your filereader*/
String absolutePath = file.getAbsolutePath();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(absolutePath)));
String line = null;
String text = "";
String nl = System.getProperty("line.separator", "\n");
while ((line = br.readLine()) != null)
text += line + nl;
ret.concat(text.trim());
} catch (Exception e) {
MessageBox.show(MessageBoxType.ERROR, I18n.localize("File Error"), I18n.localize("Error while reading content from selected file"));
} finally {
if (br != null)
try {
br.close();
} catch (Exception e) {
}
}
}
return ret;
}
I found a good resource online on using drag and drop.
Here are some classes/things that you might want to investigate:
java.awt.dnd.*
I practically copied this from a tutorial online but here is some code (not mine, but tested and it works):
public class MyFrame extends JFrame
{
// insert other code here
JLabel myLabel = new JLabel("My stuff here");
// Create the drag and drop listener
MyDragDropListener myDragDropListener = new MyDragDropListener(this);
// Connect the label with a drag and drop listener
new DropTarget(myLabel, myDragDropListener);
// then just add the label
// also have a method something like "get" which will be used so that the listener can send
// the list of files dropped here, and you can process it here
}
Now for the MyDragDropListener.
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.io.File;
import java.util.List;
public class MyDragDropListener implements DropTargetListener
{
MyFrame frame; // initialize in a constructor that takes in the frame
#Override
public void dragEnter(DropTargetDragEvent event) {
}
#Override
public void dragExit(DropTargetEvent event) {
}
#Override
public void dragOver(DropTargetDragEvent event) {
}
#Override
public void dropActionChanged(DropTargetDragEvent event) {
}
#Override
public void drop(DropTargetDropEvent event)
{
// This is the main chunk of the drag and drop.
event.acceptDrop(DnDConstants.ACTION_COPY);
Transferable transferable = event.getTransferable();
DataFlavor[] flavors = transferable.getTransferDataFlavors();
for(DataFlavor flavor : flavors)
{
if(flavor.isFlavorJavaFileListType())
{
List myFiles = (List) transferable.getTransferData(flavor);
frame.get(myFiles);
}
}
}
}
You can use this to create a JFrame to drag and drop the files, then check if the filename contains ".txt" ( I am not sure if Java has methods of determining the type of file even if it has no extensions .) If it contains ".txt" then you can open it in the TextArea.
If anyone can please help me find the original tutorial/site, I would really appreciate it. Also sorry for the formatting of the answer.

Categories