• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TextField in JTable

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Thanks .
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Google for "java treetable".
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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));
 
reply
    Bookmark Topic Watch Topic
  • New Topic