tags:

views:

132

answers:

3

How do you make a "property list" component in Swing? I mean the kind as in this or this image. Is it just a customized JTable component, or a custom component altogether?

+2  A: 

Hi, if you don't want to code it yourself, JideSoft has a component called PropertyPane. It's part of their Jide Grids package that costs $299.99.

I have't used that specific component, but I can say from experience that their components are very well polished and executed.

A webstart demo of all their components

hth

Koen

Koen Weyn
+1  A: 

Here is a basic example you might be able to further customize for your use:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TablePropertyEditor extends JFrame
{
    public TablePropertyEditor()
    {
     String[] columnNames = {"Type", "Value"};
     Object[][] data =
     {
      {"String", "I'm a string"},
      {"Date", new Date()},
      {"Integer", new Integer(123)},
      {"Double", new Double(123.45)},
      {"Boolean", Boolean.TRUE}
     };

     JTable table = new JTable(data, columnNames)
     {
      private Class editingClass;

      public TableCellRenderer getCellRenderer(int row, int column)
      {
       editingClass = null;
       int modelColumn = convertColumnIndexToModel(column);

       if (modelColumn == 1)
       {
        Class rowClass = getModel().getValueAt(row, modelColumn).getClass();
        return getDefaultRenderer( rowClass );
       }
       else
        return super.getCellRenderer(row, column);
      }

      public TableCellEditor getCellEditor(int row, int column)
      {
       editingClass = null;
       int modelColumn = convertColumnIndexToModel(column);

       if (modelColumn == 1)
       {
        editingClass = getModel().getValueAt(row, modelColumn).getClass();
        return getDefaultEditor( editingClass );
       }
       else
        return super.getCellEditor(row, column);
      }

      //  This method is also invoked by the editor when the value in the editor
      //  component is saved in the TableModel. The class was saved when the
      //  editor was invoked so the proper class can be created.

      public Class getColumnClass(int column)
      {
       return editingClass != null ? editingClass : super.getColumnClass(column);
      }
     };

     table.setPreferredScrollableViewportSize(table.getPreferredSize());
     JScrollPane scrollPane = new JScrollPane( table );
     getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
     TablePropertyEditor frame = new TablePropertyEditor();
     frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
     frame.pack();
     frame.setLocationRelativeTo( null );
     frame.setVisible(true);
    }
}
camickr
+1  A: 

It is definately a table based component, but creation of it is not trivial. I suggest you use already existing one. Here is a link to a free and very good one: http://www.l2fprod.com/common/

eugener
Thanks, this one is great!
Joonas Pulakka