简体   繁体   中英

About getting values from JList

I am trying to get values from a database via Jlist; but when I select a value of Jlist, no values return and the "Jtable" becomes empty instead of titles. That's my code for 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.

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

If it isn't your problem, please add a comment describing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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