如何重命名 JList 中的 Object[] 列表
How to rename list of Object[] in a JList
这里是初学者。
我正在开发一个应用程序来向用户显示有关不同公司的信息。我通过 API 以不同的格式获取信息,有些具有使反序列化更容易的字段,而其他响应仅包含列列表和行列表>。我重新创建了一个最小的 JFrame 来显示我的问题:
我的问题是关于后者的。我遍历列表并将每一行添加到 JList,一旦选择,该列表中包含的信息将显示在 JLabels 中。
我的问题是,不是让整个列表包含在 JList 中显示的每个对象中,有没有办法只显示每个列表中的某些值?例如,日期和人?我没有带 getter 和 setter 的 class,因为 API 响应没有字段。
这是 window 的代码:
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Window extends JFrame {
private JPanel contentPane;
public JList list1;
private JButton buttonSearch;
private JTextField searchField;
public JLabel nameValue;
public JLabel roleValue;
DefaultListModel listModel = new DefaultListModel();
public String passingValue;
public String passingValue2;
public Window() {
buttonSearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
clearList();
URL apiURL = new URL("http://api.cloudquote.io/sage/getInsiderActions.json?
T=APIKEYHERE&symbol=GOOG");
String readLine = null;
HttpURLConnection conn = (HttpURLConnection) apiURL.openConnection();
conn.setRequestMethod("GET");
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
StringBuffer response = new StringBuffer();
while ((readLine = in.readLine()) != null) {
response.append(readLine);
}
in.close();
String jsonResponse = response.toString();
Deserializer actions = new Gson().fromJson(jsonResponse, Deserializer.class);
Object[] newList = actions.getRows().toArray();
for (Object object : newList) {
listModel.addElement(object);
}
list1.setModel(listModel);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
list1.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
nameValue.setText("");
roleValue.setText("");
String stringTest = String.valueOf(list1.getSelectedValue());
List<String> myList = new ArrayList<String>(Arrays.asList(stringTest.split(",")));
passingValue = myList.get(0);
passingValue2 = myList.get(2);
nameValue.setText(passingValue);
roleValue.setText(passingValue2);
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("Testing");
frame.setContentPane(new Window().contentPane);
frame.setPreferredSize(new Dimension(750, 500));
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void clearList() {
list1.removeAll();
}
}
这是我的反序列化 Class 来解析 JSON 响应:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Deserializer {
@SerializedName("rows")
@Expose
private List<List<String>> rows = null;
@SerializedName("columns")
@Expose
private List<String> columns = null;
public List<List<String>> getRows() {
return rows;
}
public void setRows(List<List<String>> rows) {
this.rows = rows;
}
public List<String> getColumns() {
return columns;
}
public void setColumns(List<String> columns) {
this.columns = columns;
}
@Override
public String toString() {
return "Deserializer{" +
"rows=" + rows +
'}';
}
}
最后是 JSON 响应的样子:
{
"rows": [
[
"CapitalG LP",
1678225,
"",
"C",
"2020-02-13",
830211,
0,
"A",
"I",
830211,
"https://www.sec.gov/Archives/edgar/data/1535527/000089924320004527/xslF345X03/doc4.xml"
]],
"columns": [
"Insider",
"CIK",
"Role",
"Transaction Code",
"Transaction Date",
"Shares",
"Price Range",
"Aquired/Disposal",
"D/I Own",
"Total Holdings",
"Filing"
]
}
解决此问题的最佳方法是什么?
非常感谢。
最好的方法是在 JTable
(而不是 JList)中显示数据。
JSON 响应给出以下值:
- header 和
- 每行数据。
创建 JTable
的代码类似于:
Vector<String> columnNames = new Vector<String>();
for (String columnName; action.getColumns())
{
columnNames.add( columnName );
}
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
for (List<String> listRow; action.getRows())
{
String[] row = listRow.toArray( new String[listRow.size()] );
model.addRow( row );
}
JTable table = new JTable( model );
frame.add( new JScrollPane( table ) );
如果您创建一个 DTO 然后重写 class.
的 toString 方法,就非常容易了
示例
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@NoArgsConstructor
public class CatDTO {
private String id;
private String url;
private int width;
private int height;
@Override
public String toString() {
return getId() + " -> " + getUrl();
}
}
这里是初学者。
我正在开发一个应用程序来向用户显示有关不同公司的信息。我通过 API 以不同的格式获取信息,有些具有使反序列化更容易的字段,而其他响应仅包含列列表和行列表>。我重新创建了一个最小的 JFrame 来显示我的问题:
我的问题是关于后者的。我遍历列表并将每一行添加到 JList,一旦选择,该列表中包含的信息将显示在 JLabels 中。
我的问题是,不是让整个列表包含在 JList 中显示的每个对象中,有没有办法只显示每个列表中的某些值?例如,日期和人?我没有带 getter 和 setter 的 class,因为 API 响应没有字段。
这是 window 的代码:
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Window extends JFrame {
private JPanel contentPane;
public JList list1;
private JButton buttonSearch;
private JTextField searchField;
public JLabel nameValue;
public JLabel roleValue;
DefaultListModel listModel = new DefaultListModel();
public String passingValue;
public String passingValue2;
public Window() {
buttonSearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
clearList();
URL apiURL = new URL("http://api.cloudquote.io/sage/getInsiderActions.json?
T=APIKEYHERE&symbol=GOOG");
String readLine = null;
HttpURLConnection conn = (HttpURLConnection) apiURL.openConnection();
conn.setRequestMethod("GET");
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
StringBuffer response = new StringBuffer();
while ((readLine = in.readLine()) != null) {
response.append(readLine);
}
in.close();
String jsonResponse = response.toString();
Deserializer actions = new Gson().fromJson(jsonResponse, Deserializer.class);
Object[] newList = actions.getRows().toArray();
for (Object object : newList) {
listModel.addElement(object);
}
list1.setModel(listModel);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
list1.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
nameValue.setText("");
roleValue.setText("");
String stringTest = String.valueOf(list1.getSelectedValue());
List<String> myList = new ArrayList<String>(Arrays.asList(stringTest.split(",")));
passingValue = myList.get(0);
passingValue2 = myList.get(2);
nameValue.setText(passingValue);
roleValue.setText(passingValue2);
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("Testing");
frame.setContentPane(new Window().contentPane);
frame.setPreferredSize(new Dimension(750, 500));
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void clearList() {
list1.removeAll();
}
}
这是我的反序列化 Class 来解析 JSON 响应:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Deserializer {
@SerializedName("rows")
@Expose
private List<List<String>> rows = null;
@SerializedName("columns")
@Expose
private List<String> columns = null;
public List<List<String>> getRows() {
return rows;
}
public void setRows(List<List<String>> rows) {
this.rows = rows;
}
public List<String> getColumns() {
return columns;
}
public void setColumns(List<String> columns) {
this.columns = columns;
}
@Override
public String toString() {
return "Deserializer{" +
"rows=" + rows +
'}';
}
}
最后是 JSON 响应的样子:
{
"rows": [
[
"CapitalG LP",
1678225,
"",
"C",
"2020-02-13",
830211,
0,
"A",
"I",
830211,
"https://www.sec.gov/Archives/edgar/data/1535527/000089924320004527/xslF345X03/doc4.xml"
]],
"columns": [
"Insider",
"CIK",
"Role",
"Transaction Code",
"Transaction Date",
"Shares",
"Price Range",
"Aquired/Disposal",
"D/I Own",
"Total Holdings",
"Filing"
]
}
解决此问题的最佳方法是什么? 非常感谢。
最好的方法是在 JTable
(而不是 JList)中显示数据。
JSON 响应给出以下值:
- header 和
- 每行数据。
创建 JTable
的代码类似于:
Vector<String> columnNames = new Vector<String>();
for (String columnName; action.getColumns())
{
columnNames.add( columnName );
}
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
for (List<String> listRow; action.getRows())
{
String[] row = listRow.toArray( new String[listRow.size()] );
model.addRow( row );
}
JTable table = new JTable( model );
frame.add( new JScrollPane( table ) );
如果您创建一个 DTO 然后重写 class.
的 toString 方法,就非常容易了示例
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@NoArgsConstructor
public class CatDTO {
private String id;
private String url;
private int width;
private int height;
@Override
public String toString() {
return getId() + " -> " + getUrl();
}
}