If you have to present users with a JTable for editing properties in an application, the following source code might be of interest for you.

Source Code

The below code also supports updates to a Properties object through this TableModel.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package gui;
 
import java.util.Properties;
 
import javax.swing.table.AbstractTableModel;
 
/**
 * @author Alexander Bolte - Bolte Consulting 2015
 
 *         This class provides a simple table model for presentation of
 *         Properties to a user in a JTable.
 */
public class PropertyTableModel extends AbstractTableModel {
	private static final long serialVersionUID = -3063971024649603117L;
	private Properties props = null;
	private String[] key = null;
	private final String[] cols = new String[] { "Property", "Value" };
 
	/**
	 * Constructs a new object of this class.
	 
	 * @param props
	 *            - an object of type Properties.
	 */
	public PropertyTableModel(Properties props) {
		this.props = props;
		key = this.props.keySet().toArray(new String[] { "" });
	}
 
	@Override
	public int getRowCount() {
		return key.length;
	}
 
	@Override
	public int getColumnCount() {
		return 2;
	}
 
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (columnIndex == 0) {
			return key[rowIndex];
		} else {
			return props.getProperty(key[rowIndex]);
		}
	}
 
	@Override
	public String getColumnName(int column) {
		return cols[column];
	}
 
	@Override
	public boolean isCellEditable(int row, int col) {
		if (col == 1) {
			return true;
		} else {
			return false;
		}
	}
 
	@Override
	public void setValueAt(Object value, int row, int col) {
		props.put(key[row], value);
 
		fireTableCellUpdated(row, col);
	}
 
	/**
	 * Returns the internal Properties object referenced by this table model.
	 
	 * @return a Properties object used by this table model.
	 */
	public Properties getProperties() {
		return props;
	}
 
	/**
	 * Returns the index of a given key for a property in a table as it is
	 * displayed to a user in a JTable top to bottom and stored internally in
	 * this table model.
 
	 
 
	 
	 * This method is intended to be used by calling procedures, which have to
	 * update a value for a certain property through a JTable presented to a
	 * user.
 
	 
 
	 
	 * An index returned by this method always reflects the index in a displayed
	 * JTable. Example: 
	 * Property 1 | Value 1
 
	 * Property 2 | Value 2
 
	 * Property 3 | Value 3
 
 
	 
	 * For a search value "Property 1" this method would return 0 in above
	 * example. Accordingly a search value "Property 3" would result in 2.
	 
	 
 
	 
 
	 * The performance of this method is expected to be poor, but Property files
	 * should not contain as many properties in order for us having to consider
	 * performance for this kind of task. 
 
	 * However we could replace this poor search with querying an internal hash
	 * table initialized when calling a constructor, which is a good idea for a
	 * future release.
	 
	 * @param searchVal
	 *            - a String providing the exact (case sensitive) name of a
	 *            property as it is stored in this table model.
	 * @return an int providing the index of a property in this table model
	 *         which can be used for updating a corresponding value.
	 */
	public int getIndex(String searchVal) {
		int index = -1;
 
		for (int i = 0; i < key.length; i++) {
			if (key[i].equals(searchVal)) {
				index = i;
				break;
			}
		}
 
		return index;
	}
}