2009-05-11 36 views
9

Để có một cửa sổ thanh tra tài sản Netbeans thích, tôi đang sử dụng lớp sau đây để giúp tôi đạt được điều này.Có com.l2fprod.common.propertysheet.PropertySheetPanel Để hiển thị Lớp được tổng hợp

com.l2fprod.common.propertysheet.PropertySheetPanel

Cho đến nay, nó hoạt động tốt cho các lớp học với tính chất đơn giản như String, int ...

Tuy nhiên, khi đến lớp hơi phức tạp với mối quan hệ phức hợp , mọi thứ trở nên phức tạp hơn.

Ví dụ: tôi có hai con vật (giao diện). Một là Cat (lớp đơn giản với tên và tuổi) và Dog (Một lớp đơn giản với tên và tuổi).

Không cần nỗ lực để hiển thị chúng thông qua cửa sổ GUI.

Tuy nhiên, khi đến lớp với mối quan hệ tổng hợp. Một Sở thú, có thể chứa nhiều động vật (Một lớp với danh sách mảng để giữ động vật), tôi có vấn đề để hiển thị tất cả các thuộc tính động vật trong một cửa sổ duy nhất.

Sau đây là buổi chụp màn hình

alt text http://yancheng.cheok.googlepages.com/object-inspector.png

mã nguồn một phần được đưa ra ở đây

ObjectInspectorJFrame objectInspectorJFrame0 = new ObjectInspectorJFrame(cat); 
    objectInspectorJFrame0.setVisible(true); 
    objectInspectorJFrame0.setState(java.awt.Frame.NORMAL); 

    ObjectInspectorJFrame objectInspectorJFrame1 = new ObjectInspectorJFrame(dog); 
    objectInspectorJFrame1.setVisible(true); 
    objectInspectorJFrame1.setState(java.awt.Frame.NORMAL); 

    // I wish to see all "animals" and their properties in this windows. :(
    // How? 
    ObjectInspectorJFrame objectInspectorJFrame2 = new ObjectInspectorJFrame(zoo); 
    objectInspectorJFrame2.setVisible(true); 
    objectInspectorJFrame2.setState(java.awt.Frame.NORMAL); 

mã nguồn hoàn chỉnh có thể được tải về từ

http://yancheng.cheok.googlepages.com/sandbox.zip

Tôi muốn trong cửa sổ "Zoo", nó có thể hiển thị tất cả các thuộc tính cho tất cả các động vật.

+0

Tôi rất quan tâm đến việc bạn tìm thấy giải pháp cho vấn đề này hay không. Tôi có chính xác cùng một vấn đề – I82Much

Trả lời

0

PropertySheetPanel là chỉ điền bảng đọc thuộc tính cho một Java Bean đã cho.

Bạn cần mở rộng hành vi PropertySheetPanel và điền các thuộc tính từ Bộ sưu tập đã cho. Lặp lại bộ sưu tập của bạn và sử dụng addProperty (Property) để điền bảng.

Bạn cũng có thể sử dụng instrospection hoặc beanutils lib để khám phá các yếu tố thu thập.

EDIT: Đã thêm ví dụ.

package com.stackoverflow.swing.PropertySheetPanel; 

import java.util.ArrayList; 
import java.util.Collection; 

import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

import com.l2fprod.common.propertysheet.DefaultProperty; 
import com.l2fprod.common.propertysheet.PropertySheetPanel; 

/** 
* An example that creates a l2fprod PropertySheetPanel that displays any 
* Collection. 
*/ 
public class CollectionPropertySheet<C> extends PropertySheetPanel { 

    // Choose some bean. An animal as example. 
    static class Animal { 
     private String name; 
     private String family; 

     public Animal(String name, String family) { 
      this.name = name; 
      this.family = family; 
     } 

     @Override public String toString() { 
      return name + " " + family; 
     } 
    } 

    /** 
    * @param simpleModel The input collection as data model. 
    */ 
    public CollectionPropertySheet(Collection<C> simpleModel) { 
     super(); 
     populateCollectionProperties(simpleModel); 
    } 

    private void populateCollectionProperties(Collection<C> collection) { 
     int index = 0; 
     for (C entry : collection) { 
      // Define property properties 
      DefaultProperty property = new DefaultProperty(); 
      property.setDisplayName(entry.getClass().getSimpleName() + "[" + index++ +"]"); 
      property.setValue(entry.toString()); 
      // Set any other properties ... 
      // and add. 
      addProperty(property); 
     } 
    } 

    // Start me here! 
    public static void main(String[] args) { 
     // Inside EDT 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override public void run() { 
       JFrame frame = new JFrame("A simple example..."); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new CollectionPropertySheet<Animal>(getAnimals())); 
       frame.pack(); 
       frame.setVisible(true); 
      } 

      private Collection<Animal> getAnimals() { 
       Collection<Animal> animals = new ArrayList<Animal>(); 
       animals.add(new Animal("Lion", "Felidae")); 
       animals.add(new Animal("Duck", "Anatidae")); 
       animals.add(new Animal("Cat", "Felidae")); 
       return animals; 
      } 
     }); 
    } 

} 
+0

Đây là ví dụ về lớp với các trường nguyên thủy. Tuy nhiên, tôi đang tìm câu trả lời cho lớp với trường tổng hợp. Đây là giải pháp của tôi cho lớp học với các trường nguyên thủy. http://jstock.cvs.sourceforge.net/viewvc/jstock/jstock/src/org/yccheok/jstock/gui/ObjectInspectorJPanel.java?view=markup –

Các vấn đề liên quan