简体   繁体   English

如何将2个JList添加到一个JScrollPane中?

[英]How to add 2 JLists to one JScrollPane?

Imagine a group of people with names and ages . 想象一下一群有名字年龄的人 I would like the user of my application to be able to scroll through and select members of this group by name , whilst displaying their ages to the right. 我想我的应用程序的用户能够通过滚动和按名称选择该组的成员,同时显示他们的年龄的权利。 So I essentially want two JLists added to one JScrollPane, with the two JLists scrolling in sync (scrolling down brings up both the next name and age). 因此,我本质上希望将两个JList添加到一个JScrollPane中,并使两个JList同步滚动(向下滚动会显示姓氏和年龄)。 It's worth saying that the JList containing the ages needn't be selected as well as the name, the crucial point is that it must be synced up to scroll with the names. 值得一提的是,不需要选择包含年龄的JList以及名称,关键是必须同步它才能与名称一起滚动。

I've tried a couple of ideas and have ended up with the following code: 我尝试了一些想法,并得到了以下代码:

public class Main {



    public static void main(String args[]) {
        Main main = new Main();
        main.go();
    }

    private void go() {

        String[] words = {"David", "Stephanie", "Mark", "Michelle", "Simon", "Rupert",
                "Mitchell", "Charles", "Owen"};
        String[] numbers = {"43","12","15","65","12","76","33","45","34"};

        JFrame frame = new JFrame();
        JPanel background = new JPanel();

        JList<String> listOne = new JList<String>(words);
        JList<String> listTwo = new JList<String>(numbers);

        JPanel listPanel = new JPanel();
        listPanel.add(listOne);
        listPanel.add(listTwo);

        JScrollPane scrollPane = new JScrollPane(listPanel);

        background.add(scrollPane);
        frame.getContentPane().add(background);
        frame.setVisible(true);
        frame.pack();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}

If anyone can suggest a better approach then feel free but otherwise my question is simply how can I implement said feature? 如果有人可以提出一种更好的方法,那就放心了,否则我的问题仅仅是我如何实现上述功能?

Thanks in advance! 提前致谢!

So I essentially want two JLists added to one JScrollPane, with the two JLists scrolling in sync (scrolling down brings up both the next name and age). 因此,我本质上希望将两个JList添加到一个JScrollPane中,并使两个JList同步滚动(向下滚动会显示姓氏和年龄)。

Yours is an XY Problem as you don't really want this. 您是一个XY问题,因为您实际上并不希望这样做。 Instead you should use the component built to display multi-columned tabular data -- a JTable. 相反,您应该使用为显示多列表格数据而构建的组件-JTable。 I think that part of your problem is with the faulty parallel array structure of your data. 我认为问题的一部分在于数据的并行数组结构有问题。 Instead create a class to hold a String and number and then create a collection or array of items of this class. 而是创建一个包含字符串和数字的类,然后创建此类的项目的集合或数组。

eg, 例如,

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class Main2 extends JPanel {
   private static final String[] COLUMNS = {"Name", "Age"};
   private Person[] people = {
         new Person("David", 43),
         new Person("Stephanie", 12),
         new Person("Mark", 15),
         new Person("Michelle", 65)
   };

   public Main2() {
      DefaultTableModel tableModel = new DefaultTableModel(COLUMNS, 0);
      for (Person person : people) {
         Object[] row = {person.getName(), person.getAge()};
         tableModel.addRow(row);
      }

      add(new JScrollPane(new JTable(tableModel)));
   }

   private static void createAndShowGui() {
      Main2 mainPanel = new Main2();

      JFrame frame = new JFrame("Main2");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class Person {
   private String name;
   private int age; // although Date for birthdate would be better
   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }
   public String getName() {
      return name;
   }
   public int getAge() {
      return age;
   }


}

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

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