aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes TextField in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "TextField in JTable" Watch "TextField in JTable" New topic
Author

TextField in JTable

Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Hi,

Is there a way to insert a textfield in a JTable?
If so, please guide me.

Thanks .
Jared Cope
Ranch Hand

Joined: Aug 18, 2004
Posts: 243
Hi,

My default, a textfield is used when ever you want to change the value in a table cell. Can you explain your scenario a little further?

Cheers, Jared.


SCJP 1.4 91%, SCJP 1.5 88%, SCJD B&S
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Hi,
My actual requirement is to add a textfield to a JTable, add that table to a JTree. In short, the JTree should have multiple columns with textfield as a part of it. Thought of implementing a TreeTable, but i dont know how to insert TextFields to it.

Any idea how to go about it?

Thanks.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Just mark that column as editable. As Jared said, the JTable will then automatically use a JTextField as default editor. (In fact it is also using JTextFields to render the values while you don't edit them.)

You can change the editor used by the JTable using the setCellEditor method.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Hi,
I have implemented the setEditable(). It works fine. But the hurdle now is to create a table in a JTree. Is it possible?

Awaiting for some solution.


Thank you.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Google for "java treetable".
Naga Malleshwararao
Greenhorn

Joined: Oct 22, 2006
Posts: 4
Try this code


In order to insert textfield in to cell of JTable first you have to implement TableCellRenderer interface.

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellRenderer;

class MISTextFieldRenderer extends JTextField implements TableCellRenderer {

public MISTextFieldRenderer(int size)
{
super(size);
try
{
setEditable(true);
}
catch(Exception exp)
{System.out.println(exp);}
}

public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column)
{
setText((String)value);
return this;
}
}

and then you have to set the table cell renderer as

JTable resourceAllocation;

TableColumn resourceColumn = resourceAllocation.getColumnModel().getColumn(0);

// Setting Cell Editor and Cell Renderer as ComboBox
MISComboBoxRenderer resourceName = new MISComboBoxRenderer();
resourceColumn.setCellRenderer(resourceName);
resourceColumn.setCellEditor(new DefaultCellEditor(resourceName));
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TextField in JTable