从 JList Java Swing 中删除元素
Removing element from JList Java Swing
我有一个包含元素的 JList。当我 select 一个元素然后单击完成时,我希望删除该元素。问题是,第一次尝试时,它给了我应该的消息框,但没有从 JList 中删除该元素。第二次尝试后,我只得到空指针异常错误。
我需要一些帮助,不胜感激!
package gui;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import app.Order;
import java.awt.Font;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class CompleteOrder extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
public JFrame frmOrderManagement;
private JList list;
private Order[] ord_arr;
private ArrayList<Object> arr;
private JScrollPane scrollBar;
public CompleteOrder()
{
int orders_count = 2; // TODO functie ce returneaza numarul de inregistrari
app.Order x1 = new app.Order("Mihail Cioara", "AFV5G6HJ32","Corona 0.33",new app.Adress("Corona LTD", "Romania", "Bucuresti", "Macilor 66, Ap.5, Sc.A", "300351A"), new app.Adress("Corona LTD", "Romania", "Bucuresti", "Macilor 66, Ap.5, Sc.A", "300351A"), 1, 66,true, true, false, false);
app.Order x2 = new app.Order("Mihail Cioara", "bbbbbbbbbb","Corona 0.33",new app.Adress("Corona LTD", "Romania", "Bucuresti", "Macilor 66, Ap.5, Sc.A", "300351A"), new app.Adress("Corona LTD", "Romania", "Bucuresti", "Macilor 66, Ap.5, Sc.A", "300351A"), 1, 66,true, true, false, false);
ord_arr = new Order[orders_count]; // TODO functie ce returneaza un array de order
// for(int i=0;i<orders_count;i++)
// {
// ord_arr[i]=x1;
// }
ord_arr[0]=x1;
ord_arr[1]=x2;
arr = new ArrayList<Object>();
// for(int i=0;i<orders_count;i++)
// {
// arr.add(x1.JLiString());
// }
arr.add(x1.JLiString());
arr.add(x2.JLiString());
frmOrderManagement = new JFrame();
frmOrderManagement.setResizable(false);
frmOrderManagement.setTitle("Order Management");
frmOrderManagement.setBounds(100, 100, 643, 584);
frmOrderManagement.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmOrderManagement.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Complete/View Order/s");
lblNewLabel.setFont(new Font("Courier New", Font.BOLD, 24));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setBounds(140, 0, 331, 67);
frmOrderManagement.getContentPane().add(lblNewLabel);
list = new JList(arr.toArray());
list.setLayoutOrientation(JList.VERTICAL);
// list.setToolTipText("");
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollBar = new JScrollPane(list);
scrollBar.setVisible(true);
scrollBar.setBounds(10, 81, 600, 400);
frmOrderManagement.getContentPane().add(scrollBar);
scrollBar.add(list);
scrollBar.setViewportView(list);
JButton btnNewButton = new JButton("Complete");
btnNewButton.setBounds(10, 500, 109, 44);
frmOrderManagement.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(this);
JButton btnMenu = new JButton("Menu");
btnMenu.setBounds(518, 500, 109, 44);
frmOrderManagement.getContentPane().add(btnMenu);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "Complete")
{
String chosen = list.getSelectedValue().toString();
int index = list.getSelectedIndex();
// System.out.println("indexul: " + index);
// String cod = ord_arr[index].getCode();
// System.out.println(cod);
arr.remove(index);
list = new JList(arr.toArray());
scrollBar = new JScrollPane(list);
MessageBox x = new MessageBox(chosen + " A fost completat",900);
x.frmOrderManagement.setVisible(true);
}
}
}
摆脱 ArrayList 来保存数据。
使用 Swing 时,数据存储在模型中。在 JList
的情况下,您将使用实现 ListModel
的 class,通常是 DefaultListModel
。任何添加或删除都直接对 DefaultListModel
.
完成
阅读 Swing 教程中关于 How to Use Lists 的部分,了解如何动态地 add/remove 来自 DefaultListModel
项目的工作示例。
我有一个包含元素的 JList。当我 select 一个元素然后单击完成时,我希望删除该元素。问题是,第一次尝试时,它给了我应该的消息框,但没有从 JList 中删除该元素。第二次尝试后,我只得到空指针异常错误。
我需要一些帮助,不胜感激!
package gui;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import app.Order;
import java.awt.Font;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class CompleteOrder extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
public JFrame frmOrderManagement;
private JList list;
private Order[] ord_arr;
private ArrayList<Object> arr;
private JScrollPane scrollBar;
public CompleteOrder()
{
int orders_count = 2; // TODO functie ce returneaza numarul de inregistrari
app.Order x1 = new app.Order("Mihail Cioara", "AFV5G6HJ32","Corona 0.33",new app.Adress("Corona LTD", "Romania", "Bucuresti", "Macilor 66, Ap.5, Sc.A", "300351A"), new app.Adress("Corona LTD", "Romania", "Bucuresti", "Macilor 66, Ap.5, Sc.A", "300351A"), 1, 66,true, true, false, false);
app.Order x2 = new app.Order("Mihail Cioara", "bbbbbbbbbb","Corona 0.33",new app.Adress("Corona LTD", "Romania", "Bucuresti", "Macilor 66, Ap.5, Sc.A", "300351A"), new app.Adress("Corona LTD", "Romania", "Bucuresti", "Macilor 66, Ap.5, Sc.A", "300351A"), 1, 66,true, true, false, false);
ord_arr = new Order[orders_count]; // TODO functie ce returneaza un array de order
// for(int i=0;i<orders_count;i++)
// {
// ord_arr[i]=x1;
// }
ord_arr[0]=x1;
ord_arr[1]=x2;
arr = new ArrayList<Object>();
// for(int i=0;i<orders_count;i++)
// {
// arr.add(x1.JLiString());
// }
arr.add(x1.JLiString());
arr.add(x2.JLiString());
frmOrderManagement = new JFrame();
frmOrderManagement.setResizable(false);
frmOrderManagement.setTitle("Order Management");
frmOrderManagement.setBounds(100, 100, 643, 584);
frmOrderManagement.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmOrderManagement.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Complete/View Order/s");
lblNewLabel.setFont(new Font("Courier New", Font.BOLD, 24));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setBounds(140, 0, 331, 67);
frmOrderManagement.getContentPane().add(lblNewLabel);
list = new JList(arr.toArray());
list.setLayoutOrientation(JList.VERTICAL);
// list.setToolTipText("");
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollBar = new JScrollPane(list);
scrollBar.setVisible(true);
scrollBar.setBounds(10, 81, 600, 400);
frmOrderManagement.getContentPane().add(scrollBar);
scrollBar.add(list);
scrollBar.setViewportView(list);
JButton btnNewButton = new JButton("Complete");
btnNewButton.setBounds(10, 500, 109, 44);
frmOrderManagement.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(this);
JButton btnMenu = new JButton("Menu");
btnMenu.setBounds(518, 500, 109, 44);
frmOrderManagement.getContentPane().add(btnMenu);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "Complete")
{
String chosen = list.getSelectedValue().toString();
int index = list.getSelectedIndex();
// System.out.println("indexul: " + index);
// String cod = ord_arr[index].getCode();
// System.out.println(cod);
arr.remove(index);
list = new JList(arr.toArray());
scrollBar = new JScrollPane(list);
MessageBox x = new MessageBox(chosen + " A fost completat",900);
x.frmOrderManagement.setVisible(true);
}
}
}
摆脱 ArrayList 来保存数据。
使用 Swing 时,数据存储在模型中。在 JList
的情况下,您将使用实现 ListModel
的 class,通常是 DefaultListModel
。任何添加或删除都直接对 DefaultListModel
.
阅读 Swing 教程中关于 How to Use Lists 的部分,了解如何动态地 add/remove 来自 DefaultListModel
项目的工作示例。