Hi
Ok, first off, I've simplified the code down to the bare basics and
added it verbatim at the end of this code, so anyone can just cut and
paste it, and run it.
The problem is that I want to have a JTextField at the top of each
column in a table. When the user clicks on this box, they can type in
some text, and as they do so, it filters out the rows starting with
that text.
I've got all the filtering code working, BUT what I can't do is click
on the darn text box and get focus on it!!
Nothing happens when I click on the text box.
Any ideas how I can enter text into the JTextFields kick of my
filtering code?
The added problem is that, although I'm using the default table header
provided by the JScrollPane in this case, I also want it to work with
ANY table header. For instance, I have another TableModel for sorting,
such that when you click, you get an ascending sort etc. However, when
I tried combining it with this code, clicking ONLY did the sorting -
even when I clicked on the text box. I figure this is because the text
box is INSIDE the header, but I couldn't figure out how to get focus on
the text box.
Really stuck on this so guidance most gratefully received.
Thanks
Marcos
_____ COMPLETE CODE BELOW ____
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.table.TableCellRenderer;
import java.awt.Component;
public class TextBoxInAHeader
{
public void run()
{
JFrame frame = new JFrame("Text Box in a Header");
JTable table = new JTable(5, 5);
JScrollPane scrollPane = new JScrollPane(table);
table.getTableHeader().setDefaultRenderer(new
MyHeaderRenderer(table.getTableHeader().getDefaultRenderer()));
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
TextBoxInAHeader textBoxInAHeader = new TextBoxInAHeader();
textBoxInAHeader.run();
}
private class MyHeaderRenderer implements TableCellRenderer
{
private TableCellRenderer originalCellRenderer;
public MyHeaderRenderer(TableCellRenderer tableCellRenderer)
{
this.originalCellRenderer = tableCellRenderer;
}
public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component c =
originalCellRenderer.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(new JTextField(column));
panel.add(c);
return panel;
}
}
}
Vova Reznik - 17 Feb 2006 18:24 GMT
You have TableCellRenderer (to show JTextField).
To edit it you need TableCellEditor
> Hi
>
[quoted text clipped - 89 lines]
>
> }
Vova Reznik - 17 Feb 2006 19:32 GMT
How about
Google java editable table header
one from the list
http://www.java2s.com/ExampleCode/Swing-Components/EditableHeaderTableExample.htm
> Hi
>
[quoted text clipped - 89 lines]
>
> }
Marcos - 17 Feb 2006 19:48 GMT
Erm, thanks - unfortuantely, neither answers the question.
The first response shows no detail whatsoever
The second shows an example on editable headers - I'll looking to have
a text box in the header, so that I can combine it with any table. ANY
table. As I said above, I have a situation where I've got a table with
sortable headers. When I click that, the sorting works, but the text
box doesn't...
Rhino - 17 Feb 2006 21:08 GMT
> Erm, thanks - unfortuantely, neither answers the question.
>
[quoted text clipped - 5 lines]
> sortable headers. When I click that, the sorting works, but the text
> box doesn't...
I found this article very helpful when I was learning about renderers and
editors; perhaps it will help you too:
http://www-128.ibm.com/developerworks/library/j-jtable/
--
Rhino
Marcos - 20 Feb 2006 09:19 GMT
Thanks for the response
What I really need is for someone to take up my example code and
explain how to do it from there, or if not, why my approach is wrong.
The key word is examples!
Thanks
Marcos