如何将 JSON 数据添加到 JAVA 中的 JTable
How to add JSON data to a JTable in JAVA
我正在使用 REST 网络服务访问 MySQL 数据库。我在 NetBeans IDE 中为 REST 客户端创建了一个 java swing 应用程序。当我单击 jButton
时,我得到 JSON object
并将它们设置为 jTextArea
。现在我想要的是从收到的 JSON object
中填充 JTable
。有谁能够帮助我?提前致谢。
Code that set JSON Object to jTextArea
try {
URL url = new URL("http://localhost:8080/webservice/rest/bdetails/get");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
String json = "";
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
json += output;
}
conn.disconnect();
java.lang.reflect.Type listType = new TypeToken<ArrayList<BDetails>>() {
}.getType();
List<BDetails> bList = new Gson().fromJson(json, listType);
for( BDetails adr : bList) {
jTextArea1.append(adr.toString());
jTextArea1.append("\n");
}
System.out.print(json);
} catch (IOException | RuntimeException ex) {
System.out.println(ex);
}
BDetails Class
public class BDetails
{
private String username;
private String firstName;
private String lastName;
private String address;
public BDetails() {
}
public BDetails(String username, String firstName, String lastName, String address) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString()
{
String object = "username: "+username+" firstName: "+firstName+" lastName: "+lastName+" address: "+address;
return object;
}
您需要将每个 BDetails 对象的数据作为唯一行添加到 TableModel。
基本代码如下:
String[] columnNames = { "Username", "First Name", "Last Name", "Address" };
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
for( BDetails detail : bList)
{
Vector<String> row = new Vector<String>();
row.add(detail.getUserName());
row.add(detail.getFirstName());
row.add(detail.getLastName());
row.add(detail.getAddress();
model.addRow( row );
}
JTable table = new JTable( model );
JScrollPane scrollPane = new JScrollPane( table );
// add the scrollpane to the frame
阅读有关 How to Use Tables 的 Swing 教程部分,了解更多信息和工作示例。
我正在使用 REST 网络服务访问 MySQL 数据库。我在 NetBeans IDE 中为 REST 客户端创建了一个 java swing 应用程序。当我单击 jButton
时,我得到 JSON object
并将它们设置为 jTextArea
。现在我想要的是从收到的 JSON object
中填充 JTable
。有谁能够帮助我?提前致谢。
Code that set JSON Object to jTextArea
try {
URL url = new URL("http://localhost:8080/webservice/rest/bdetails/get");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
String json = "";
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
json += output;
}
conn.disconnect();
java.lang.reflect.Type listType = new TypeToken<ArrayList<BDetails>>() {
}.getType();
List<BDetails> bList = new Gson().fromJson(json, listType);
for( BDetails adr : bList) {
jTextArea1.append(adr.toString());
jTextArea1.append("\n");
}
System.out.print(json);
} catch (IOException | RuntimeException ex) {
System.out.println(ex);
}
BDetails Class
public class BDetails
{
private String username;
private String firstName;
private String lastName;
private String address;
public BDetails() {
}
public BDetails(String username, String firstName, String lastName, String address) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString()
{
String object = "username: "+username+" firstName: "+firstName+" lastName: "+lastName+" address: "+address;
return object;
}
您需要将每个 BDetails 对象的数据作为唯一行添加到 TableModel。
基本代码如下:
String[] columnNames = { "Username", "First Name", "Last Name", "Address" };
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
for( BDetails detail : bList)
{
Vector<String> row = new Vector<String>();
row.add(detail.getUserName());
row.add(detail.getFirstName());
row.add(detail.getLastName());
row.add(detail.getAddress();
model.addRow( row );
}
JTable table = new JTable( model );
JScrollPane scrollPane = new JScrollPane( table );
// add the scrollpane to the frame
阅读有关 How to Use Tables 的 Swing 教程部分,了解更多信息和工作示例。