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