Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-23-2009, 05:31 AM
Member
 
Join Date: Oct 2009
Posts: 53
Rep Power: 0
anilkumar_vist is an unknown quantity at this point
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-23-2009, 06:03 AM
Senior Member
 
Join Date: Jul 2009
Posts: 280
Rep Power: 1
camickr is on a distinguished road
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:

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);
	}
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The code isnt working unless I add print statements at diffrent points!:confused: Addez New To Java 6 11-12-2009 11:50 AM
log4j - multiple logs with diffrent contents ld09 Java Applets 0 10-08-2009 12:36 PM
diffrent page redirection in struts. rakesh_n_mehta Web Frameworks 0 12-11-2008 07:34 AM
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help salmanpirzada1 Advanced Java 2 05-15-2008 11:15 AM
Gui Components Marty New To Java 1 05-28-2007 05:04 AM


All times are GMT +2. The time now is 09:27 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org