
Hi I'm new in Java and I have a application that reads multiple text files and displays them in a Jtable.So can you please tell me how can I make a bar graph that will display the data from my table.I tried using the code from your page (http://www.roseindia.net/chartgraphs/bar-chart.shtml) but I couldn't make it work. Thanks in advance.
Here is my code
package table2;
import java.io.*; import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*; import javax.swing.table.*; import java.awt.color.*; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset;
public class InsertFileDataToJTable extends AbstractTableModel {
Vector data;
Vector columns;
public InsertFileDataToJTable() {
String line;
data = new Vector();
columns = new Vector();
columns.addElement("ID");
columns.addElement("RSSI");
String path = "C:/Users/Nikica/Desktop/text files";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int y = 0; y < listOfFiles.length; y++) {
if (listOfFiles[y].isFile()) {
files = listOfFiles[y].getName();
if (files.endsWith(".txt") || files.endsWith(".TXT")) {
System.out.println(files);
// TreeMap<String, Integer> frequencyMap = new TreeMap<String, Integer>();
File textFile = new File(folder.getAbsolutePath() + File.separator + files);
try {
FileInputStream fis = new FileInputStream(textFile);
// DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
int i = 0;
while ((line = br.readLine()) != null) {
if (line.startsWith("17/10/2012 10:00:06.67 [RX] - E usbR<LF>") || line.startsWith("E qEnd<LF>") || line.startsWith("00<LF>")) {
continue;
}
String ID = line.substring(0, 12);
String RSSI = line.substring(13, 15);
data.addElement(ID);
data.addElement(RSSI);
}
br.close();
/*
* FileInputStream fis = new FileInputStream("ard.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);
}
public static void main(String s[]) {
InsertFileDataToJTable model = new InsertFileDataToJTable();
JTable table = new JTable();
table.setAutoCreateRowSorter(true);
JTableHeader header = table.getTableHeader();
table.setModel(model);
table.getColumnModel().getColumn(0).setHeaderValue("ID");
table.getColumnModel().getColumn(1).setHeaderValue("RSSI");
table.getTableHeader().resizeAndRepaint();
JScrollPane scrollpane = new JScrollPane(table);
JPanel panel = new JPanel();
panel.add(scrollpane);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel, "Center");
frame.pack();
frame.setVisible(true);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.