gpt4 book ai didi

java - 如何在没有ListModel的情况下刷新JList

转载 作者:行者123 更新时间:2023-11-30 04:11:26 24 4
gpt4 key购买 nike

我会使用列表模型,但我还没有找到一种方法来轻松地将组件从我的 ListArray 添加到列表模型中。有没有一种方法,当按下添加按钮(将元素添加到 JList 中)时,除了将字符串添加到文本文件(就像我设置的那样)之外,它还会刷新 JList。

感谢您的帮助。

package movieinfo;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.commons.io.FileUtils;

import com.json.parsers.JSONParser;
import com.json.parsers.JsonParserFactory;

public class Swinggui {
private static JButton enter;
private static JTextField movietext;
private static JTextArea movieinfo;
private static JList listofmovies;// converts moviestowatch into gui
// element.
private static File textfilemovie; // file which movies marked for watching
// are saved
private static java.util.List<String> moviestowatch; // arraylist which is
// populated by
// textfilemovie
// than printed to
// GUI element
private static JsonParserFactory factory;
private static JSONParser parser;
@SuppressWarnings("rawtypes")
private static Map jsonData;
private static ListSelectionListener setSearch;
private static JButton add;

public static void main(String[] args) throws IOException {
yourMovies();
gui();
jsonAndButtons();

}

public static void gui() {
JFrame maingui = new JFrame("Gui");
maingui.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL;
enter = new JButton("Get Info");
c.gridx = 2;
c.gridy = 1;
maingui.add(enter, c);
add = new JButton("add");
c.gridx = 5;
c.gridy = 6;
maingui.add(add, c);
movieinfo = new JTextArea(5, 20);
movieinfo.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2,
Color.red));
movietext = new JTextField(18);
c.gridx = 1;
c.gridy = 1;
maingui.add(movietext, c);
final JScrollPane scrolll = new JScrollPane(movieinfo);
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 2;
maingui.add(scrolll, c);
final JLabel titlee = new JLabel("Enter movie name below!");
c.gridx = 1;
c.gridy = 0;
maingui.add(titlee, c);
final JLabel info = new JLabel("Info");
c.gridx = 1;
c.gridy = 3;
maingui.add(titlee, c);
final JLabel watchlist = new JLabel("Watchlist");
c.gridx = 5;
c.gridy = 1;
maingui.add(watchlist, c);
maingui.setResizable(false);
maingui.setVisible(true);
listofmovies = new JList(moviestowatch.toArray());
c.gridx = 4;
c.gridy = 3;
maingui.add(new JScrollPane(listofmovies), c);
movieinfo.setLineWrap(true);
movieinfo.setWrapStyleWord(true);
movieinfo.setEditable(false);
scrolll.getPreferredSize();
listofmovies.addListSelectionListener(setSearch);
maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
maingui.pack();

}

public static void jsonAndButtons() {
enter.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.out.println(apicall.getMovieInfo(movietext.getText().replaceAll(" ", "%20")));
factory = JsonParserFactory.getInstance();
parser = factory.newJsonParser();
jsonData = parser.parseJson(apicall.getMovieInfo(movietext
.getText().replaceAll(" ", "%20")));
String Title = (String) jsonData.get("Title");
String Year = (String) jsonData.get("Year");
String Plot = (String) jsonData.get("Plot");
movieinfo.setText("Title: " + Title + "\nYear: " + Year
+ "\nPlot: " + Plot);

}

});
add.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent arg0) {
try {
FileUtils.writeStringToFile( new File(
org.apache.commons.io.FileUtils.getUserDirectory()
+ "/yourmovies.txt"), "\n" + movietext.getText(), true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
moviestowatch = FileUtils.readLines(textfilemovie);
listofmovies = new JList(moviestowatch.toArray());

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

});

}

public static void yourMovies() throws IOException {
textfilemovie = new File(
org.apache.commons.io.FileUtils.getUserDirectory()
+ "/yourmovies.txt");
textfilemovie.createNewFile();
moviestowatch = FileUtils.readLines(textfilemovie);
setSearch = new ListSelectionListener() {

public void valueChanged(ListSelectionEvent arg0) {
factory = JsonParserFactory.getInstance();
parser = factory.newJsonParser();
jsonData = parser.parseJson(apicall.getMovieInfo(((String) listofmovies
.getSelectedValue()).replaceAll(" ", "%20")));
String Title = (String) jsonData.get("Title");
String Year = (String) jsonData.get("Year");
String Plot = (String) jsonData.get("Plot");
movieinfo.setText("Title: " + Title + "\nYear: " + Year
+ "\nPlot: " + Plot);
}
};
}
}

最佳答案

  1. 阅读 Oracle 教程 How to use List - Creating a Model工作代码示例

  2. 创建 DefaultListModel 作为局部变量,然后每次更改都将仅在模型中进行

  3. 存在错误,也许是功能,Swing GUI 卡住几秒钟,在瞬间删除超过 999 个项目的情况下(已经可见 JList),无法找到此问题的答案

  4. 对已经可见的 Swing GUI 的所有更新都必须在 EDT 上完成,更多内容请阅读 Oracle 教程 Concurency in Swing - Event DIspatch Thread

  5. listofmovies = new JList(moviestowatch.toArray());JList API中的short_cut,该构造函数创建DefaultTableModel,对此数组的任何进一步更改都不会被访问 JListDefaultListModel

关于java - 如何在没有ListModel的情况下刷新JList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19517589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com