简体   繁体   English

拆分并将选定的jList值移动到jTable行(SWING)

[英]Splitting and Moving selected jList value to jTable rows (SWING)

Hi I'm trying to make a program that will move a selected value from a list to a table. 嗨,我正在尝试制作一个程序,将选定的值从列表移动到表。 I can add the whole list value to one row, but what I wanna do is split the list value into a set of strings and set them to different rows in the table. 我可以将整个列表值添加到一行,但我想要做的是将列表值拆分为一组字符串并将它们设置为表中的不同行。

The list values will look like this: 列表值将如下所示:

123 Test St LA 1823 Yes
456 Test Dr NY 12345 No

The list gets values from text fields(Address,State,Zip,For Sale) and set all of it into just one line of string separated by spaces. 该列表从文本字段(地址,状态,Zip,待售)中获取值,并将其全部设置为由空格分隔的一行字符串。

In the table I have 4 Rows: 在表中我有4行:

|Address|State|Zip| For Sale|
|------ +-----+---+---------|
|       |     |   |         |  

So in order to do this I have to split the string value from the list by space, but since the address text field includes spaces (ie 123 Test St) unlike other text fields, I have to figure out a way to split the string from the list so that I can set the Address, State, Zip, and For Sale value. 所以为了做到这一点,我必须按空格分割列表中的字符串值,但由于地址文本字段包含空格(即123 Test St),与其他文本字段不同,我必须找出一种方法来分割字符串列表,以便我可以设置地址,州,邮编和待售价值。

Here's my code snippet: 这是我的代码片段:

 private void addListActionPerformed(java.awt.event.ActionEvent evt) {                                          

    home.setAddress(address.getText());
    home.setState(state.getText());
    home.setZip(zip.getText()); 
    home.setSale(forSale.getText());

    String concat = home.getAddress() + " " +
                    home.getState() + " " +
                    home.getZip() + " " +
                    home.getSale();


    addVal(concat);

        address.setText("");    
        state.setText("");
        zip.setText("");
        forSale.setText("");

}    

private void addVal(String str) {
    jList1.setModel(dm);
    dm.addElement(str);
}

    private void addToTableActionPerformed(java.awt.event.ActionEvent evt) {                                            
    String s = jList1.getSelectedValue();
    //Split and set values and add to table
}  

I could use: String[] spaces = s.split(" ") , but then I would need to handle the Address value since it includes multiple spaces as well, is there an easier way of doing this? 我可以使用: String[] spaces = s.split(" ") ,但是我需要处理Address值,因为它还包含多个空格,有没有更简单的方法呢? Any advice would be appreciated. 任何意见,将不胜感激。 Thanks! 谢谢!

  • Fill your JList with Home objects, not a concatenated String 使用Home对象填充JList,而不是连接的String
  • Use a custom list cell renderer to display a concatenated String derived from the Home objects in the list. 使用自定义列表单元格渲染器显示从列表中的Home对象派生的串联String。
  • Then when an item in the JList is selected, you've got a full Home object, not a String, and creating your JTable row should be trivial. 然后,当选择JList中的项目时,您有一个完整的Home对象,而不是String,并且创建JTable行应该是微不足道的。

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

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