Actually I am making a program which takes .properties file and show the value in GUI. I am already done with reading and writing. Now, at this point I dont how to implement this add/remove rows functionality. I want to add a row on run-time. I am using abstract table How can I add and Delete rows.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
public class StackoverflowStuff implements TableModelListener {
public static void main(String[] args) {
Vector<Vector> data = new Vector<>();
String strState[] = new String[2];
strState[0] = "Property";
strState[1] = "value";
// Vector<String> columnNames = new Vector<>();
Vector<String> columnNames = new Vector<String>(Arrays.asList(strState));
String line;
try {
FileInputStream fis = new FileInputStream("C:/properties/spanish.properties");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringTokenizer st1 = new StringTokenizer(br.readLine(), "=");
while (st1.hasMoreTokens()) {
Vector row = new Vector();
row.addElement(st1.nextToken());
data.add(row);
}
while ((line = br.readLine()) != null) {
if (line.contains("###") && (line.contains("#"))) {
continue;
}
StringTokenizer st2 = new StringTokenizer(line, "=");
while (st2.hasMoreTokens()) {
Vector row1 = new Vector();
row1.addElement(st2.nextToken());
data.add(row1);
}
System.out.println("Selected :: " + data);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
// final int rows = data.size();
// System.out.println("Selected :: " + data.size());
// final int columns = 2;
// for (int i = 0; i < columns; i++) {
// int dataas;
// dataas = data.size() - 1;
// System.out.println("Selected :: " + dataas);
// if (dataas % 2 == 0) {
// columnNames.add("Property");
// } else {
// columnNames.add("Value");
// }
// }
// for (int i = 0; i < rows; i++) {
// Vector row = new Vector();
// for (int j = 0; j < columns; j++) {
// row.add(j);
// }
// data.add(row);
// }
DefaultTableModel model = new DefaultTableModel(data, columnNames);
final JTable table = new JTable(model);
JButton saveBtn = new JButton("Save");
JScrollPane jsPane;
jsPane = new JScrollPane(table);
JPanel panel = new JPanel();
panel.add(jsPane, BorderLayout.CENTER);
panel.add(saveBtn);
JButton addRowbtn = new JButton("Add Row");
panel.add(addRowbtn, BorderLayout.PAGE_START);
try {
addRowbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// myModel.setValueAt(table, table.getRowCount() + 1, 0);
// myModel.setValueAt(table, table.getRowCount(), 1);
System.out.println("sadasdasd");
}
});
} catch (Exception e) {
e.printStackTrace();
}
try {
saveBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
BufferedWriter bfw = new BufferedWriter(new FileWriter("C:/properties/cwqesdb.properties"));
for (int i = 0; i < table.getRowCount(); i++) {
bfw.newLine();
for (int j = 0; j < 1; j++) {
if (j == 0) {
bfw.write((String) (table.getValueAt(i, j)));
System.out.println("Selected data: MouseDragged :: " + table.getValueAt(i, j));
} else {
// bfw.write((String) (table.getValueAt(i, j)));
// bfw.write("=");
// System.out.println("Selected data: MouseDragged :: " + table.getValueAt(i, j));
}
}
}
bfw.flush();
bfw.close();
JOptionPane.showMessageDialog(null, "File Successfully Saved.");
} catch (IOException ex) {
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JOptionPane.showMessageDialog(null, panel);
}
@Override
public void tableChanged(TableModelEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
here is my file contents spanisch.properties
username=Benutzername
password=Kennwort
gender=Geschlecht
male=männlich
female=weiblich
age=alter
### ewflmkdsajfaef ####sdasdfasdf
asfsadfsimpleasdf ####
asdas = asdashdash
address=Anschrift
submit=einreichen
message=Erfolgreich abgegeben ...
In your model, you have two
Vectors
, one for the data, and one for the columns. Read the data to the vectors, then just create aDefaultTableModel
with them. Though what may be messing you up, is that for the data you are using a singleVector
for the data. The data should be two dimensional, so you would want aVector<Vector>
, just like if you were to use arrays for the data, it would beObject[][] data
. So you may want to change your implementation to something likeSee the DefaultTableModel API documentation for more methods you can use to manipulate the data.
Here's a simple example
If you later want to add a row to the model, you can simply use the
addRow
method ofDefaultTableModel
, which is overloaded to accept aVector
or anObject[]
arrayUPDATE
What you are doing is
You adding a new row for each token, when you should be creating the row vector outside the loop, and adding the tokens inside the loop, then adding the row outside the loop