Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2009
    Posts
    88
    Rep Power
    0

    Default Add diffrent components in jtable

    how to add different component like jcombobox, jcheckbox, jformattedtextfield all components in single column in different rows
    like
    row column
    Row1 JCombobox
    Row2 JFormattedField
    Row3 JCheckBox
    ....
    .
    .
    .
    plz give sample code.
    so that i can understand.
    plz dont post link of
    below
    sun java forum
    i already observed i cant understand it.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    You need to override the getCellRenderer() and getCellEditors() to return the appropriate renderer/editor for the data in the given cell. How you do this is up to you.

    Here is a generic solution that may (or may not) help get you started:

    Java Code:
    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);
    	}
    }

Similar Threads

  1. Replies: 6
    Last Post: 11-12-2009, 11:50 AM
  2. log4j - multiple logs with diffrent contents
    By ld09 in forum Java Applets
    Replies: 0
    Last Post: 10-08-2009, 12:36 PM
  3. diffrent page redirection in struts.
    By rakesh_n_mehta in forum Web Frameworks
    Replies: 0
    Last Post: 12-11-2008, 07:34 AM
  4. Replies: 2
    Last Post: 05-15-2008, 11:15 AM
  5. Gui Components
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-28-2007, 05:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •