Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TextField in JTable

 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks .
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Google for "java treetable".
 
Naga Malleshwararao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic