关于从JList获取值

时间:2017-02-20 10:25:29

标签: java swing user-interface jtable jlist

我试图通过Jlist从数据库中获取值;但是当我选择一个Jlist的值时,没有值返回和" Jtable"变得空洞而不是标题。这是我的UI代码。 谢谢你的帮助...

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());
}

}

1 个答案:

答案 0 :(得分:0)

我不明白你想要什么,但我修改了你的代码以这种方式运行它: enter image description here

显然我已经全部硬编码以运行你的程序。如果您的问题是左侧而不是类别的名称出现对象“com.mypackage.Category@12AAAF”的哈希码,则必须修改Category类并覆盖toString方法。

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

如果不是您的问题,请添加描述它的评论。