-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddressTable.java
executable file
·151 lines (131 loc) · 4.94 KB
/
AddressTable.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//---------------------------------------------------------------------------
// Name: AddressTable
//
// Description: AddressTable implements a class to display a table with
// rows containing an address, latitude, and longitude
//---------------------------------------------------------------------------
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
public class AddressTable extends JTable
{
private AddressModel tableModel; // model for the data in the table
// AddressModel defines the model to use for the AttributeTable
class AddressModel extends AbstractTableModel
{
String[] columnNames = {"Address", "Latitude", "Longitude"};
String[][] addressData; // full list of addresses
// method to return the name of the requested column
//--------------------------------------------------
public String getColumnName(int col)
{
return columnNames[col];
}
// method to set the addresses in the table. Each address in the
// input array should hold the longitude, latitude, then address
// separated by a ':'.
//---------------------------------------------------------------
public void setAddresses(String[] addresses)
{
// allocate space for the table data
addressData = new String[addresses.length][3];
for (int index = 0; index < addresses.length; index++)
{
// split each address apart and save it in the table
String[] addressList = addresses[index].split(":");
addressData[index][0] = addressList[2];
addressData[index][1] = addressList[1];
addressData[index][2] = addressList[0];
}
// notify the table that things have changed
fireTableDataChanged();
// if only one row in the table, select it automatically
if (getRowCount() == 1)
addRowSelectionInterval(0,0);
}
// method to return the number of rows in the table
//-------------------------------------------------
public int getRowCount()
{
if (addressData != null)
return addressData.length;
else
return 0;
}
// method to return the number of columns in the table
//----------------------------------------------------
public int getColumnCount()
{
return columnNames.length;
}
// method to return the value in a table cell
//-------------------------------------------
public Object getValueAt(int row, int column)
{
return addressData[row][column];
}
// get the latitude and longitude from a row
//------------------------------------------
public LatLong getLatLong(int row)
{
// convert the latitude to a double
Double lat = new Double(addressData[row][1]);
Double lon = new Double(addressData[row][2]);
LatLong latLong = new LatLong(lat.doubleValue(),lon.doubleValue());
return latLong;
}
// get the address name from a row
//--------------------------------
public String getAddress(int row)
{
return new String(addressData[row][0]);
}
// clear the table contents
//-------------------------
public void clearAddresses()
{
addressData = null;
// notify the table that things have changed
fireTableDataChanged();
}
}
// Constructor for the attribute table
//------------------------------------
public AddressTable()
{
tableModel = new AddressModel();
setModel(tableModel);
TableColumn column = getColumnModel().getColumn(1);
column.setMinWidth(50);
column.setPreferredWidth(100);
column.setMaxWidth(100);
column = getColumnModel().getColumn(2);
column.setMinWidth(50);
column.setPreferredWidth(100);
column.setMaxWidth(100);
}
// method to set the addresses to display in the table
//----------------------------------------------------
public void setAddresses(String[] addresses)
{
tableModel.setAddresses(addresses);
}
// clear the contents from the table
//----------------------------------
public void clearAddresses()
{
tableModel.clearAddresses();
}
// get the latitude and longitude for a particular row
//----------------------------------------------------
public LatLong getLatLong(int row)
{
return tableModel.getLatLong(row);
}
// get the address for a particular row
//-------------------------------------
public String getAddress(int row)
{
return tableModel.getAddress(row);
}
}