How to get a text box *inside* a table header in Swing

M

Marcos

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;
}

}

}
 
V

Vova Reznik

You have TableCellRenderer (to show JTextField).
To edit it you need TableCellEditor
 
V

Vova Reznik

How about
Google java editable table header


one from the list
http://www.java2s.com/ExampleCode/Swing-Components/EditableHeaderTableExample.htm
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;
}

}

}
 
M

Marcos

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...
 
R

Rhino

Marcos said:
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...
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/
 
M

Marcos

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
 
Joined
Sep 21, 2007
Messages
1
Reaction score
0
displaying a textbox on a table header for filtering along with sorting on tableHead

Dear Marcos,

Did u find any solution to your problem? I am trying since many days but could get it to work and then came across your post.

Please let me know if you could go any further.

Thanks ,
Ziby
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top