Resizing columns in a JTable

How can I know when a JComponent is valid? i.e. I need to execute some code after a table has resized its columns, but I can't use the method SwingUtilities.invokeLater. Is there any method that informs when the component has finished painting?
Thank you very much in advance for your help.

Well, I don't know about that "finished painting" thing, but components do fire events when they are resized.
You need to register a ComponentListener to be notified of resize events ...

Similar Messages

  • Resize column width for JTable without column headers?

    Hi,
    I find that for me to resize columns of a JTable by using
    mouse dragging, I MUST have column headers and then drag
    column headers to resize them. Is my understanding correct?
    Actually, I need to have a table without header columns.
    How can I use mouse to resize column width by dragging?
    The background for this request is that I am putting a
    complex table as another table's column header and I hope to
    be able to resizing the complex table by mouse dragging.
    Thanks very much,
    David

    Hi,
    I think you have mistake there. Try
    <style type="text/css">
    table.apexir_WORKSHEET_DATA td {
    white-space:nowrap !important;
    td[headers="DESCR"] {
    width:300px !important;
    max-width:300px !important;
    #apexir_DESCR {
    width:10px;
    max-width:300px !important;
    </style>And you can try add
    table.apexir_WORKSHEET_DATA {
    width: 100% !important;
    table-layout: fixed !important;
    }Regards,
    Jari
    My Blog: http://dbswh.webhop.net/htmldb/f?p=BLOG:HOME:0
    Twitter: http://www.twitter.com/jariolai

  • Resizing columns in a JTable in a JScrollpane

    I have a JTable in a JScrollpane and I after I load the table, I resize the columns to fit the data (works well).
    The problem is that I want the scrollpane to stay the same size and let the table scroll horizontally inside the viewport. The actual result is the scrollpane stretches to the width of the table causing the user to scroll the panel rather than the scrollpane.
    I've tried setting the scrollbar policy but that didn't work.

    I am passing the JTable to the constructor of the JScrollPane.
    I have a JTable in a JScrollPane that sits on a JPanel which is in a JScrollPane itself.
    After I load the data into the JTable, I resize all of the columns to fit the data.
    The problem is that the JTable resizes wider than original size causing the whole panel to scroll horizontally. I would rather have the JScrollPane that the JTable is in scroll.

  • JTable user resize columns with grid?

    Is it possible to allow a user to resize columns not just through the table header, but with the vertical grid lines?

    There is no method that allows you to toggle this
    feature on/off if thats what you are asking.Yes, thats what I was asking.
    If you want to implement this type of functionality
    yourself then take a look at the source code for
    JTableHeader to see how its done.Thanks, I'll look into that - though it would be cool if that was already a feature of JTable.

  • Resize cells in a JTable.

    I'd like to give some columns, in a jTable, an autoresize mode. It means fitting well with the content (being able to read the longest cell without having to resize the cell manualy by resizing the header).
    Some tables of other applications (like the Excel ones) hava this ability.
    Do S.O. have any idea of doing this?
    Thanks.

    extend a new class from javax.swing.table.DefaultTableCellRenderer
    and use setCellRenderer(...) on your JTable

  • Looking for component: flexible rows like columns in a JTable

    Hello,
    I need a swing component to display several rows which can be moved up and down, the height resized, interchanged with the mouse like the columns in a JTable. But there is no need for columns. I think there is no standard swing component.
    Any ideas, resource hints?
    Thanks, Ulrich

    One more piece of advice. It is not very easy to get "pre-written custom components". Most developers do things to meet their own needs and these may not be exactly what you are looking for. The best thing to do is to try to develop this yourself. It will give you loads of experience and in later programmes you may write, based on the experience you obtained from the "pain-staking" development process you'll know how to go round these problems quicker and may be able to help others also.
    So just start writing some stuff. You may end up finishing sooner than you think. And remember forum members are always ready to help with problems (accompanied by minimal code examples of the actual problem).
    ICE

  • Setting the Size of a Column in a JTable

    Hi there,
    does anybody knows a way how to set the size of a specified column in a JTable without changinge the size of the other columns?
    thx anyway
    Errraddicator

    You've got to change the JTable's column resizing policy to prevent the other columns resizing:
    JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF)Otherwise all the columns will be adjusted to fit into the width of the table - changing one column would have to result in at least one other column changing!
    Hope this helps.

  • JTextFields above column headers in JTable

    I am looking for a way to put a single row above (for filtering purposes) the table header in a JTable. Right now I am using JTextFields above my table for the filters, but as the columns are resized or moved so would the text fields have to be. So I am thinking putting a row above the table header would be appropriate.
    Any suggestions or examples in this area?

    Hello
    I've been working on something similar (filters above a JXTable) and i managed to get it working. So i'm putting it on this thread if someone find it with google :)
    The code for moving / resizing columns works fine, but the code for removing/adding columns doesn't work very well because it only allows one removed column at a time, or that you add the columns in the correct order.
    My solution is a bit different. I put a listener on the column property "visible", associated with the filter component above the column. That way, when i remove (hide) a column, the filter component removes itself from the panel, and when i reactivate the column, the component adds itself to the panel.
    Because the property change event is fired after the columnadded / columnmoved event, i have to use invokeLater thread to actually move the component at its right position.
    Here's the code for the moved/resized listener:
    import java.awt.Component;
    import java.awt.Dimension;
    import javax.swing.JPanel;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.TableColumnModelEvent;
    import javax.swing.event.TableColumnModelListener;
    import javax.swing.table.TableColumnModel;
    import fr.dga.gesma.catalyse.ihm.beans.FilterPanel;
    public class ExternalHeaderColumnModelListener implements TableColumnModelListener {
        private JPanel filterRow;
        private JTable table;
        public ExternalHeaderColumnModelListener(JPanel header, JTable table) {
            filterRow = header;
            this.table = table;
        public void columnAdded(TableColumnModelEvent e) {
        public void columnRemoved(TableColumnModelEvent e) {
        public void columnMarginChanged(ChangeEvent e)
            TableColumnModel tcm = table.getColumnModel();
            int columns = tcm.getColumnCount();
            for (int i = 0; i < columns; i ++)
                FilterPanel filtre = (FilterPanel)filterRow.getComponent( i );
                Dimension d = filtre.getPreferredSize();
                d.width = tcm.getColumn(i).getWidth();
                filtre.setPreferredSize( d );
            SwingUtilities.invokeLater(new Runnable()
                public void run()
                    filterRow.revalidate();
        public void columnMoved(TableColumnModelEvent e)
            final int toIndex = e.getToIndex();
            final int fromIndex = e.getFromIndex();
            // Delaying so the component exists when we move it
            SwingUtilities.invokeLater(new Runnable()
                public void run()
                    Component moved = filterRow.getComponent(fromIndex);
                    if (moved != null) {
                        filterRow.remove(fromIndex);
                        filterRow.add(moved, toIndex);
                        filterRow.validate();
        public void columnSelectionChanged(ListSelectionEvent e) {}and the code for the filter component (listener part) :
        private Container parent = null;
        // Taken from ColumnControlButton (JXTable component)
        // This listener is added to the TableColumn associated to this filter component
        protected PropertyChangeListener createPropertyChangeListener() {
            return new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    if ("visible".equals(evt.getPropertyName())) {
                        updateFromColumnVisible((Boolean) evt.getNewValue());
         * Hides or shows the filter component. This
         * is called on change of tablecolumn's <code>visible</code> property.
         * @param visible column visible state to synch to.
        private void updateFromColumnVisible(boolean visible) {
            if (visible) {
                if (parent != null) {
                    parent.add(this);
                    parent.validate();
            } else {
                parent = this.getParent();
                if (parent != null) {
                    parent.remove(this);
                    parent.validate();
        }Hope this helps someone :)

  • Set column width in JTable

    I have two columns in a JTable and I am trying to set the column width for the first.
    I am trying to this by
    getDataTbl().getColumnModel().getColumn(0).setHeaderValue("Index");
    getDataTbl().getColumnModel().getColumn(1).setHeaderValue(" Data ");
    int width = ((String)getDataTbl().getColumnModel().getColumn(0).getHeaderValue()).length();
    getDataTbl().getColumnModel().getColumn(0).setPreferredWidth(width);
    but it doesn't reset the width. I want the first column width to be smaller than the second which should be much larger width.
    Any suggessions to this?
    Thanks.

    Hi there
    Use this JTable method
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);This should solve your resize problem
    ICE

  • Resizing row header of jtable

    hi,
    Is there any way to resize row header column of a jtable just as other columns of jtable. i searched in forums and google but couldnt find any article with resizing row header.
    thanx

    If you want help in the future I suggest you remain a little more patient. It's the weekend. People don't hang around waiting for you to post a question. We answer questions if and when we have time and we know the answer.
    The general answer is yes, of course, it can be done. But we can't give you a specifice answer since you question is so general.
    For example, take a look at my code in this posting:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=639189
    I made the following changes:
    //          addColumn( new TableColumn() );
              TableColumn column = new TableColumn();
              column.setHeaderValue(" ");
              addColumn( column );And then I overrode the following method of the table:
    public void columnMarginChanged(ChangeEvent e)
         super.columnMarginChanged(e);
         TableColumn resizingColumn = getTableHeader().getResizingColumn();
         if (resizingColumn != null)
              Dimension d = getPreferredSize();
              d.width = resizingColumn.getWidth();
              setPreferredScrollableViewportSize(d);
    }and finally
    scrollPane.setRowHeaderView( lineTable );
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, lineTable.getTableHeader()); // newOf course we have no idea what component you are using for your row header so we can't give a detailed solution, but I suspect you would need to do something like the following:
    a) add a mouseListener to your component
    b) calculate the change in size of the component using the mouse location changes
    c) set the preferred size of your component
    d) notify the scroll pane of the change in size. I would look at the setPreferredScrollableViewportSize method of JTable to see what it does.

  • How to get all the values in one column of a JTable

    How to get all the values in one column of a JTable as a Collection of String.
    I don;t want to write a for loop to say getValueAt(row, 1) eg for 2nd column.

    I don;t want to write a for loop to say getValueAt(row, 1) eg for 2nd column. You could always write a custom TableModel that stores the data in the format you want it. It would probably be about 50 lines of code. Or you could write a loop in 3 lines of code. I'll let you decide which approach you want to take.

  • How to resize column width in report?

    hi all,
    i read somewhere here that in order to resize column width in a report, you need to go to report attributes, then edit the column you wish to resize and then go to the CSS Style and place something like "width=600px". while this works very well in Internet Explorer, it somehow has no effect in Firefox 3.0. is there a way to make it work both browsers?
    thanks
    allen

    Hi,
    Sorry, I misunderstood. There are several methods you could use:
    1 - Use the span tags around the data itself - this would require adding the tags into the sql statement itself
    2 - Use javascript to set the column widths by adding a style to the TD tags for each cell
    3 - Create a new Report Template and use COL tags immediately after the TABLE tag in the Before Rows section to specify the widths of all columns
    Option 3 may be the easiest?
    Andy

  • Resizing columns and making multiple selections in Pages 5.0

    Using Pages 5.0 (using OS X), I am no longer able to resize columns in a table by dragging a selection handle as I could in the old Pages (2009, I think it was). Dragging now adds the adjacent column to the selection. Anyone know how to resize the columns by dragging?
    Also, in the previous Pages, I could select multiple noncontiguous words by holding down the command key. Doesn't work anymore. Does anyone know how to do that?

    Apple has removed 90+ features from Pages5:
    http://www.freeforum101.com/iworktipsntrick/viewforum.php?f=22&mforum=iworktipsn trick
    Pages '09 should still be in your Applications/iWork folder:
    http://www.freeforum101.com/iworktipsntrick/viewtopic.php?t=432&mforum=iworktips ntrick
    Trash/Archive Pages 5 after exporting any Pages 5 files back to Pages '09 format.
    Then rate/review Pages 5 in the App Store.
    Peter

  • Resize columns in Pages for iPad?

    Is there a way to resize columns in a multi-column document in Pages for iPad? I want a narrow left column and a wider right column. I can kinda get the effect by using a table, but it'd be easier to just adjust the column widths. Is this possible, and if so, how?

    Hi Adarga,
    I'm not seeing a way to resize the columns and the Help doesn't mention it either. I'm just able to change the indents and "margins" within the columns, but the columns stay the same size.
    ivan

  • How can I add custom right-click-menu to column headers in JTable?

    Can anyone point me to a topic on how to customize a popup menu for column headers in JTable? Specifically, I want to add things like "auto-size column" and "hide column".
    Thanks,
    Matt

    Right-click on your table.  Then go to Advanced->Runtime Shortcut Menu->Edit.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

Maybe you are looking for