JTable and JTree Code Samples
I have been working on contributing to the Java Tutorials Community Project. I've put together some code samples as potential tutorial subjects.
- JTable - ColumnsDemo
- JTable - ColorEntireRowDemo


- JTable - MultiLineRows


- JTable - FrozenColumns


- JTree - DefaultTreeModelDemo


- JTree - CustomEditorDemo


The tutorials project has forums where you can comment on these proposed topics, propose new topics and to request revisions to any of the current Java tutorials. We need your help to create the kind of content that can help reduce the Java learning curve and help create better software.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
Geertjan Wielenga replied on Fri, 2008/05/23 - 4:59am
Alex Ruiz replied on Fri, 2008/05/23 - 1:04pm
srikanth gunday replied on Fri, 2009/01/16 - 3:21pm
can I search on the JTable, if user want to search "Hello" , so if the Hello presnets in any cell it has to Highlight.
example : Hello World , it has to Highlight "Hello".
Thanks
Srikanth
Collin Fagan replied on Fri, 2009/01/16 - 6:01pm
@srikanthgsk - This should be exactly what you want.
http://wiki.java.net/bin/view/Projects/FlexibleSearchTreeDemo
srikanth gunday replied on Mon, 2009/01/19 - 10:58am
cfagan, I need for Table, so I am using getTableCellRendererComponent but no sucess,
If you have any example can you post it.
Thanks
Srikanth
srikanth gunday replied on Mon, 2009/01/19 - 11:13am
in response to:
Collin Fagan
cfagan, I got it I solved it.below is code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.DefaultHighlighter;
public class TableCellRendererBug extends JFrame {
private boolean DEBUG = false;
public TableCellRendererBug() {
super("TableCellRendererBug");
Object[][] data = { { "Jay Four Four Jay", "Four" }, { "Jay", "Jay" }, };
String[] columnNames = { "Column 1", "Column 2" };
JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(200, 50));
// Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
table.setDefaultRenderer(Object.class, new CellHighlightRenderer());
System.out.println(table.getColumnClass(0).toString());
// Add the scroll pane to this window.
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class CellHighlightRenderer extends JTextField implements TableCellRenderer {
public DefaultHighlighter high = new DefaultHighlighter();
public DefaultHighlighter.DefaultHighlightPainter highlight_painter = new DefaultHighlighter.DefaultHighlightPainter(
new Color(198, 198, 250));
public CellHighlightRenderer() {
setBorder(BorderFactory.createEmptyBorder());
setHighlighter(high);
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
setFont(table.getFont());
setValue(value);
int len = getText().length();
int first = (row & 1) + 1;
int last = (row & 1) + first + 1;
int pos =0;
String pattern="Jay";
//if (len > last) {
while ((pos = value.toString().indexOf(pattern, pos)) >= 0) {
try {
//high.addHighlight(first, last, highlight_painter);
high.addHighlight(pos, pos + pattern.length(), highlight_painter);
pos += pattern.length();
} catch (Exception e) {
e.printStackTrace();
}
}
return this;
}
protected void setValue(Object value) {
setText((value == null) ? "" : value.toString());
}
}
public static void main(String[] args) {
TableCellRendererBug frame = new TableCellRendererBug();
frame.pack();
frame.setVisible(true);
}
}
Thanks
Srikanth
Collin Fagan replied on Mon, 2009/01/19 - 11:38am
hookfi john replied on Sun, 2009/05/31 - 7:54am
Carla Brian replied on Thu, 2012/07/26 - 6:30pm