2017-05-01 1 views
-2

Ok. 그래서 자바 GUI 스윙 어플 리케이션을 만들 필요가 있고, 4 jLists로 열에 의해 txt 파일에서 데이터를 전달하려고 해요. 예를 들어,이 행은 내 txt 파일에서 "John apples golden 15"이며 각 단어를 4 개의 개별 jLists에 넣어야합니다. 첫 번째 목록에 대한 내 코드는 다음과 같습니다, 지금 임은java gui .txt 파일 값을 여러 jLists에 전달

for (Object item : itemList) { 
      ; 
} 
에 붙어
try { 
      BufferedReader buf = new BufferedReader(new FileReader("dfata.txt")); 
      ArrayList<String> words = new ArrayList<>(); 
      String lineJustFetched = null; 
      String[] attributes; 

      while (true) { 
       lineJustFetched = buf.readLine(); 
       if (lineJustFetched == null) { 
        break; 
       } else { 
        attributes = lineJustFetched.split("\t"); 
        Item item = new Item(attributes[0], attributes[1], attributes[2], attributes[3], attributes[4]); 
        itemList.add(item); 
       } 
      } 

      buf.close(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     for (Object item : itemList) { 
      ; 
     } 
     jListItemType.setModel(new javax.swing.AbstractListModel<String>() { 
      String[] strings = {""}; 

      public int getSize() { 
       return strings.length; 
      } 

      public String getElementAt(int i) { 
       return strings[i]; 
      } 
     }); 
class Item { 

    String id; 
    String type; 
    String model; 
    String size; 
    String color; 

    public Item(String id,String type, String model, String size, String color) { 
     this.id = id; 
     this.type = type; 
     this.model = model; 
     this.size = size; 
     this.color = color; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public String getModel() { 
     return model; 
    } 

    public void setModel(String model) { 
     this.model = model; 
    } 

    public String getSize() { 
     return size; 
    } 

    public void setSize(String size) { 
     this.size = size; 
    } 

    public String getColor() { 
     return color; 
    } 

    public void setColor(String color) { 
     this.color = color; 
    } 
} 
+1

"이제 막 달라 붙었습니다 ..."- 어떤 방식으로 붙어 있었습니까? 중요한 세부 사항을 알려주십시오. –

+0

나는 그것을 사용하여 파일에서 jList로 구문 분석하는 방법을 모른다. –

답변

2

제안 :

  • 나는 당신이하는 AbstractListModel을 확장 할 필요를 볼 수 없습니다 . 이것은 단지 아무런 이익도없이 프로그램에 복잡성을 추가하는 것처럼 보입니다.
  • 대신 이유를 위해 - 각 루프는 항목을하지 개체 루프 있도록 귀하의 JList의 모델
  • 변경을 DefaultListModel<Item>을 사용하고, 그 루프 내에 DefaultListModel로 항목을 추가하지.
  • 더 좋은 점은 프로그램에 itemlist가 없어도 itemList를 직접 채우는 곳인 DefaultListModel<Item>을 직접 채울 수 있습니다.
  • 목록을 올바르게 표시하려면 toString() 메서드를 사용하거나 JList의 렌더러를 설정하십시오. 후자를 수행하려면 단순히 DefaultListCellRenderer를 확장하십시오.

다음 링크를 확인하시기 바랍니다 :

예를

private DefaultListModel<Item> itemModel = new DefaultListModel<>(); 
private JList<Item> itemList = new JList<>(itemModel); 

// elsewhere 
lineJustFetched = buf.readLine(); 
if (lineJustFetched == null) { 
    break; 
} else { 
    attributes = lineJustFetched.split("\t"); 
    Item item = new Item(attributes[0], attributes[1], attributes[2], attributes[3], attributes[4]); 
    itemModel.addElement(item); 
} 

DefaultListCellRenderer에 대한 자세한 내용은 내 대답 here을 참조하십시오.

+0

코드를 제공 할 수 있습니까? 애매한 것 같습니다. –

+1

@DanielVantas : 동봉 된 링크를 참조하십시오. 또한 명확하게 모호한 것은 무엇입니까, 당신을 혼란스럽게합니까? –

+0

@DanielVantas : 일부 코드가 추가되었습니다. 나는 당신의 유권자가 아닙니다. –