Collin Fagan is a Sr. Software Engineer at NAVTEQ working on next generation map building software. He holds a BS in Computer Science from the Rochester Institute of Technology. Collin has worked in the Map, Medical, Produce Warehousing and Telecommunications industries on a wide assortment of projects ranging from customized Linux thin clients to highly concurrent telecommunications control systems. Collin has posted 13 posts at DZone. View Full User Profile

JTable and JTree Code Samples

05.23.2008
| 16354 views |
  • submit to reddit

I have been working on contributing to the Java Tutorials Community Project. I've put together some code samples as potential tutorial subjects.

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.

Published at DZone with permission of its author, Collin Fagan.

(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

Great job Colin!

Alex Ruiz replied on Fri, 2008/05/23 - 1:04pm

Very nice and useful! Thanks Colin!

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

Hi

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

Sorry I rushed through answering your question. I think your render approach is on the right track. Be careful how you update the search criteria. The JTable will need to repaint every time it changes.

hookfi john replied on Sun, 2009/05/31 - 7:54am

Carla Brian replied on Thu, 2012/07/26 - 6:30pm

Thank your for these sources. I am new with this one. I need to look more to familiarize with this one. - Mercy Ministries

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.