Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2010, 04:09 PM
Member
 
Join Date: Feb 2010
Posts: 1
Rep Power: 0
iceT18 is on a distinguished road
Default JTable multiline
Hi,

I want to make a new line in a cell when i press the ENTER key (multiline).

------------------
| line | a |
| line | a |
------------------
| b | b |
------------------

For a new table line i have a button.

this is my code:
Code:
private static class TextTableCellRenderer extends JTextArea implements TableCellRenderer {
 
        protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
 
        public TextTableCellRenderer() {
            super();
            setWrapStyleWord(true);
            setLineWrap(true);
            setBorder(noFocusBorder);
        }
 
        public Component getTableCellRendererComponent(JTable table,
                Object value,
                boolean isSelected,
                boolean hasFocus,
                int row,
                int column) {
            if (isSelected) {
                super.setForeground(table.getSelectionForeground());
                super.setBackground(table.getSelectionBackground());
            } else {
                super.setForeground(table.getForeground());
                super.setBackground(table.getBackground());
            }
 
            setFont(table.getFont());
            if (hasFocus) {
                setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
                if (!isSelected && table.isCellEditable(row, column)) {
                    Color col;
                    col = UIManager.getColor("Table.focusCellForeground");
                    if (col != null) {
                        super.setForeground(col);
                    }
                    col = UIManager.getColor("Table.focusCellBackground");
                    if (col != null) {
                        super.setBackground(col);
                    }
                }
            } else {
                setBorder(noFocusBorder);
            }
 
            setEnabled(table.isEnabled());
            setValue(table, row, column, value);
            return this;
        }
 
        protected void setValue(JTable table, int row, int column, Object value) {
            if (value != null) {
                String text = value.toString();
                setText(text);
 
                View view = getUI().getRootView(this);
                view.setSize((float) table.getColumnModel().getColumn(column).getWidth() - 3, -1);
                float y = view.getPreferredSpan(View.Y_AXIS);
                int h = (int) Math.ceil(y + 3);
 
                if (table.getRowHeight(row) != h) {
                    table.setRowHeight(row, h);
                }
            } else {
                setText("");
            }
        }
    }
thanks
iceT
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-11-2010, 01:02 AM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 1,028
Rep Power: 3
Steve11235 is on a distinguished road
Default
If you look at the api for DefaultTableCellRenderer, you will see that it is a JLabel with certain methods stubbed out for performance reasons. You can make your own TableCellRenderer using any Component by stubbing out the same methods.TableCellEditor is a subclass of Renderer that allows user input with a little code to handle saving the contents when editing is stopped.

Look at the multi-line text JComponents offered by Swing. Try making your own Renderer from the interface (it only has the one method) by extending a JTextArea and implementing the interface. Check the api for the methods to stub off.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write multiline String in a JLabel JavaBean AWT / Swing 4 12-14-2009 05:09 AM
Creating multiline item in JComboBox javabud AWT / Swing 2 07-17-2009 03:50 PM
Multiline Regex zriggle New To Java 1 02-26-2009 04:49 AM
To get multiline tooltip diva_garg New To Java 6 08-20-2008 07:57 AM
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help salmanpirzada1 Advanced Java 2 05-15-2008 10:15 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 05:59 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org