简体   繁体   English

关于从JList获取值

[英]About getting values from JList

I am trying to get values from a database via Jlist; 我试图通过Jlist从数据库中获取值; but when I select a value of Jlist, no values return and the "Jtable" becomes empty instead of titles. 但是,当我选择Jlist的值时,不会返回任何值,并且“ Jtable”将变为空而不是标题。 That's my code for UI. 那是我的UI代码。 Thanks for your help... 谢谢你的帮助...

package ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import model.Category;
import model.Person;
import service.AddressBookService;

import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JSplitPane;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.AbstractListModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

public class UserInterfaceMain extends JFrame {

private JPanel contentPane;
private JPanel panel;
private JButton btnNew;
private JSplitPane splitPane;
private JList list;
private JTable table;
private JScrollPane scrollPane;
private List<Category> categories = new ArrayList<>();
private List<Person> personList = new ArrayList<>();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                UserInterfaceMain frame = new UserInterfaceMain();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public UserInterfaceMain() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 624, 395);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout(0, 0));

    panel = new JPanel();
    contentPane.add(panel, BorderLayout.NORTH);
    panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    btnNew = new JButton("NEW");
    panel.add(btnNew);

    splitPane = new JSplitPane();
    contentPane.add(splitPane, BorderLayout.CENTER);

    list = new JList();
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent arg0) {
            do_list_valueChanged(arg0);
        }
    });
    splitPane.setLeftComponent(list);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    scrollPane = new JScrollPane();
    contentPane.add(scrollPane, BorderLayout.EAST);

    table = new JTable();
    splitPane.setRightComponent(table);
    scrollPane.setViewportView(table);

    loadCategories();
}

public void loadCategories() {

    categories = new AddressBookService().getAllCategories();
    DefaultListModel<Category> listModel = new DefaultListModel<>();
    for (int i = 0; i < categories.size(); i++) {
        listModel.addElement(categories.get(i));
        //listModel.addElement(categories.get(i).getName());
    }
    list.setModel(listModel);
}

public void loadPersonList() {

    String[] columns = new String[] { "NAME", "LAST NAME", "E-MAIL", "CITY" };
    Object[][] personData = new Object[personList.size()][];

    for (int i = 0; i < personData.length; i++) {
        personData[i] = new Object[] { personList.get(i).getName(), personList.get(i).getLastName(),
                personList.get(i).getEmail(), personList.get(i).getCity() };
    }

    TableModel tableModel = new DefaultTableModel(personData, columns);
    table.setModel(tableModel);
}

protected void do_list_valueChanged(ListSelectionEvent arg0) {
    personList = new AddressBookService().getPersonsForTable(((Category)list.getSelectedValue()).getId());
    loadPersonList();
    System.out.println(personList.size());
}

} }

I don't understand exactly what you want, but I modified your code to run it in this way: 我不确定您到底想要什么,但是我修改了您的代码以这种方式运行它: 在此处输入图片说明

Obviously I have all hardcoded in order to run your program. 显然,为了运行您的程序,我已经全部进行了硬编码。 If your problem is that in the left side instead of the names of the categories appears the hash code of the object "com.mypackage.Category@12AAAF", you must modify your Category class and override the toString method. 如果您的问题是对象的哈希码出现在左侧而不是类别的名称“ com.mypackage.Category@12AAAF”,则必须修改Category类并重写toString方法。

@Override
public String toString() {
    return getName();
}

If it isn't your problem, please add a comment describing it. 如果不是您的问题,请添加描述它的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM