JTable resizing the column width

Have a JTable whose column width's are set using
int width = ((String)getDataTbl().getColumnModel().getColumn(0).getHeaderValue()).length() + 32;
getDataTbl().getColumnModel().getColumn(0).setPreferredWidth(width);
int width1 = ((String)getDataTbl().getColumnModel().getColumn(1).getHeaderValue()).length()+207
getDataTbl().getColumnModel().getColumn(1).setPreferredWidth(width1);
Now depending on the data in column 1 the column width has to be increased if more than width1 and the scroll bar should apprear only in this case only?
How to do this?
Thanks.

maybe you can implement a interface ComponentListener as well as ComponentAdapter with your topmost Table.
for example:
toptable.addComponentListener(
new ComponentAdapter(){
public void componentResized(ComponentEvent e){
table1.setSize(....);
table2.setSize(....);
/*Optionally, you must also call function revalidate
anywhere;*/
);

Similar Messages

  • Is it possible to resize page column widths at runtime (by the user)?

    hi all,
    i have cretaed a page using the 2-columns narrow/wide layout.  this layout is perfect for what i want to do.
    however, the users have complained that they would like to be able to resize the columns, just like they are able to do with the Detailed Navigation column (using the small "<" and ">" arrows).
    is it posssible to add this functionality to my custom pages so that can also resize the columns?
    i searched for options in the properties, but didn't find anything.
    thanks,
    mm

    This was a great place for me to start
    Thank you
    I took profdant139's suggestions a bit further as my real goal was to have the "Document column" narrower. The goal to be able to see all the other document info that was usually hidden way out to the right
    To achieve my results I right clicked in the upper bar with the column names and chose from the drop down box "comments" column (as suggested). I then dragged it to the far right. This pushed all the other columns over.
    In the end of the day I ended up adding "Size" and "Date modified" columns as well to get my desired results.
    Remember one can move different columns around to be in the order you need by clicking on the column to get it active and then left click and drag it to where you want
    Another great thing is that by default (Using Mavericks) the date modified, opened,created columns have date and time. One can shrink them down to xx/xx/xx format by manually adjusting the column width like one does in Excel - click on column separator up top and drag
    Once all that is done, at least on my box, I closed it out and opened up a new finder window and everything was as I wanted.
    Thank you for the help  from the above helper to get me here

  • How can i set the column width in the jtable?

    how can i set the column width in the jtable?
    can anybody send me a simple example??

    TableColumn column = table.getColumnModel().getColumn( columnIndex );
    column.setWidth( desiredColumnWidth);
    column.setMinWidth( desiredMinColumnWidth);
    column.setMaxWidth( desiredMaxColumnWidth);

  • How to set the column width of JTable

    I want do remember the column width and write it to a file, when the window is started , the column width is read from the file and the table is set as that. I also want the column width is not fixed and can be changed by mouse.But I am always failded.
    How to do? would you please help me!
    Thanks a lots.

    To "remember" the column widths, add a mouse listener to the table header. Upon mouseReleased(), get the column widths and save them to your file. When the application starts next time, get the stored widths and set them using:
    table.getColumn("XXX").setPreferredWidth(...);
    By default, your columns are adjustable, but you can use:
    table.getTableHeader().setResizingAllowed(true);

  • How to resize the columns of a table?

    Hi there!
    I want to resize the column of a JTable(constructed via AbstractTableModel) to
    fit the exact width of the word in the header?
    Can anyone tell me how to do this thing?
    Regards,

    import java.awt.*;
    import javax.swing.table.*;
    public class ColumnResizeDemo extends javax.swing.JFrame {
        public ColumnResizeDemo() {
            initComponents();
            TableColumn column = null;
            for (int i = 0; i < jTable1.getColumnModel().getColumnCount(); i++) {
                column = jTable1.getColumnModel().getColumn(i);
                String hv = column.getHeaderValue().toString();
                JTableHeader th = jTable1.getTableHeader();
                FontMetrics fm = th.getFontMetrics(th.getFont());
                column.setPreferredWidth(fm.stringWidth(hv)+5);
        private void initComponents() {
            jLabel1 = new javax.swing.JLabel();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTable1 = new javax.swing.JTable();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Column Resize Demo");
            jLabel1.setText("Sets column width to fit exactly the corresponding header value");
            getContentPane().add(jLabel1, java.awt.BorderLayout.NORTH);
            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                    new Object [][] {
                        {null, null, null, null},
                        {null, null, null, null},
                        {null, null, null, null},
                        {null, null, null, null}
                    new String [] {
                "Title 1", "Title 0002", "Title 0000003", "Title 00000000000004"
            jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
            jScrollPane1.setViewportView(jTable1);
            getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
            pack();
        public static void main(String args[]) {
            new ColumnResizeDemo().setVisible(true);
        private javax.swing.JLabel jLabel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTable1;
    }

  • How  to resize the column header according to row data

    Hi,
    How can i resize the column header according to row date .. means if data is small header should be small and vice versa ,...
    Any help is appreciated .. Snippet will be more helpful .
    Thanks,
    Shekhar

    Oxley (binkley) blogged about this: http://binkley.blogspot.com/2006/01/getting-jtable-columns-widths-to-fit.html
    Christian Ullenboom | tutego

  • JTable:  resizing all columns as 1 column resizes

    I have a JTable with 480 columns inside of a JScrollPane (AUTO_RESIZE_ALL_COLUMNS needs to be off for the JTABLE to display correctly in the JScrollPane). When the user resizes one column I would like the rest of the columns to resize too, so that the column widths of each column are equal.
    Thank you in advance,
    Brent

    [...] I
    I have the JScrollPane listening to a histogram of the
    balances so the user can click on a month in the
    histogram and the JScrollPane (actually JViewport)
    will move to display the appropriate column in the
    JTable. If the column sizes are not uniform then I
    cannot be sure that I'm moving the JViewport to
    display the correct JTable column(s).
    I thing the GUI is a great design for what must be
    done. And thank you for your help and comments.That still sounds pretty klunky. I don't see any user navigating around a 480 column table. They click in your histogram to go to a column, they click somewhere or do keyboard navigation and the display scrolls them a few columns/rows away... they are now lost in a sea of cells and have to go back to the histogram again to try and get back, etc. Having delt with similar situations, there are better ways. Read the last two days dilbert cartoons carefully. :-)
    But anyway...
    You don't have to have all the columns the same width to make a column visible in the viewport. You can just get the appropriate column's rect by either of these methods:Rectangle r = myTable.getTableHeader().getHeaderRect( col );
    Rectangle r = myTable.getCellRect( row, col, true );And then tell the viewport to make sure that rect is visible:myViewport.scrollRectToVisible( r );Much easier than hacking around with custom column resizing logic.

  • How to optimize the column width in Table Control

    Hi all,
          When I am displaying fields in Table Control, the columns are displayed in full length, and I am unable to see all the fields at once.
    So as in grid , where we optimize the column width using layout, do we had any property for TC to do so.
    thanks for your help.
    Points would be awarded .
    Regards,
    Anil .

    Hi,
    In Se51, you can do this one, ust open your table control and resize the TC, there is no direct option to do this for entire TC, you need to do this field by field, and arrage the TC to adjust in a Single screen. In the field parameters, you have the field lenghts, there you can minimize the length of a field
    Regards
    Sudheer

  • How do I resize the column in numbers for the iPad

    Hi, I have numbers app in my iPad and I can't figure out how to resize the columns.  There are no column headers when I create a new spreadsheet, and I can't click between the columns and even when I turn wrap text off it doesn't resize just overlay.
    I hope there has to be a way that I just can't figure out as it seems like a very simple task that should be obvious.
    Karena

    Tap on any cell in a table to bring up the table options. You'll see two bars above and to the left of the table. There is a dot in a circle on the top left, and two toggles to increase/decrease the number of rows or columns. To change the width of a column, tap in the bar above the table in the column which you want to resize, and that column will become highlighted. From here you can select multiple columns using the handles to resize multiple columns at once. In the bar at the top, there are two little dashes - drag this left and right to resize the column.

  • JTable -  Horizontal Scrollbar - Column Width

    I wish to add a horizontal scrollbar(which for some stupid reason Java has not included) to my JTable which is inside a JScrollPane and also make sure that the width of the fourth column is longer and can fit the String that i wish to display. Now, i have searched the forum for solutions but none of them seem to work. The setting of the column width with the help of TableColumnModel does not seem to work and not to mention the fact that when i turn auto rezise off for the JTable, the JTable does give me a horizontal scrollbar but then it makes the columns so less in width that i have gray area left on the right hand side and the setColumnsToFit method does not work. I have five columns and here is the code, any help would be greatly appreciated Thanks:
    excepTable = new JTable(excepTableModel);
    excepTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    int width = 100;
    TableColumnModel tcm;
    TableColumn tc;
    for(int i = excepTable.getColumnCount(); i > 0; i--)
    tcm = (TableColumnModel)excepTable.getColumnModel();
    tc = tcm.getColumn(i);
    if(i == 3)
    width = 400;
    tc.setWidth(width);
    tc.setMinWidth(width);
    tc.setMaxWidth(width);
    tc.setPreferredWidth(width);
    width = 100;
    exScroller = new JScrollPane(excepTable);

    .I've set a horizontal scrollbar by using 'table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);You are welcome.
    i want the column widht to be adjusted according to the length of column names and record length.so please tell me how to do it?Search the forum. This question has been asked and answered before.

  • JTable sum of column widths table size prob

    Hi there,
    At the moment I've encountered a problem with JTable (it may be a feature?) that I can't seem to work around.
    The problem is that I've got a JTable with a custom background color for the top row of cells. The problem is that the column widths are editable and if the user makes the last column narrower, than the background color of the top row bleeds into the background panel.
    I'll try to make a small app displaying the problem, if anyone has ideas though or understands what I'm talking about I'd definitely appreciated any advice.
    Thanks,
    eknight

    The problem is that I've got a JTable with a custom background color for the top row of cells. The problem is that the column widths are editable and if the >>user makes the last column narrower, than the background color of the top row bleeds into the background panel.It is difficult to say what you mean by the color bleeding. Because if you are using/overriding the tablecellrender to achieve the background coloring, it should be taken care off automatically.
    Post a Short, compilable piece of code

  • 1. Resizing of column width. 2. Drag and drop of columns for resequencing

    QUERY : Has anyone some experience in implementation/development of following functionality in a JSP/Tomcat server based application:
    1. Resizing of column width for a result which has multiple columns. .2. Drag and drop of these columns or any other mechanism for re-sequencing the columns?
    I would like to know will JSF would be helpful in it.

    Amit,
    The column resizing and 'drag and drop ' are client side issues. They requires DHTML and Javascript.
    AJAX is what is required. (wiki AJAX for more info)
    I am working on an AJAX grid and combobox component for JSF that will do this and many more functions.
    Let me know if you want more information.
    [email protected]

  • In Sales Order changing the column width to see all item desciption?

    When in the sales order screen .
    Expand the column width of say the item description.
    How do I save the column width for all the sales documents  for the field item description in sales order ?

    Hi,
    The column width of the Document will be defaulted to the same size at which the user has saved the last document.
    So, if a user has saved the Sales Order with a big column width for the 'Item Description' then after saving and re-entering into the Sales Order, the user can see the same column width for all the Sales Order's.
    I hope the above helps.
    Cheers~
    Jitin Chawla

  • How to extract the column width in ALv report if its executed in background

    I am executing an ALV report in background , in front end i am getting data properly, in back end for some columns some of the digits are missing.For example if PO no is of 10 digits it will display only 8 becos column size is like that , how to extract coulmns in back ground.
    I have executed in background and checked the spool and  for some of the columns width is not sufficient to display comeplete data so please suggest how to extract the columns sizes if executed inj background for an ALV

    Hi Deepthi,
    you can try with the above mentioned suggestions ,if its worked its fine ,
    If not use Docking container instead of custom container, For ALV in back ground jobs, its suggest to use docking container instead of custom container , below you can find the declaration for docking container and code to use docking and custom container in your program for fore and back ground.
    or you can use docking container alone for both operations.
    Data : G_DOCK1 TYPE REF TO CL_GUI_DOCKING_CONTAINER,
    IF CCON IS INITIAL. (ccon is container name )
    *Check whether the program is run in batch or foreground
        IF CL_GUI_ALV_GRID=>OFFLINE( ) IS INITIAL.
    *Run in foreground
          CREATE OBJECT CCON
            EXPORTING
              CONTAINER_NAME = 'CON1'.
        CREATE OBJECT GRID1
            EXPORTING
              I_PARENT = parent_1.
    ELSE.
    *Run in background
          CREATE OBJECT GRID1
            EXPORTING
              I_PARENT = G_DOCK1.
        ENDIF.
      ENDIF.
    B&R,
    Saravana.S

  • Can't figure out how to maximize the space on the page by increasing the column width

    Trouble with pages app:
    can't figure out how to maximize the space on the page by increasing the column width
    I have 3 columns and they have spaces btw them that I cannot close the gap on.

    Whatever the version, you can click on the gutter figure and edit it.
    Click once on the column and a second time on the Gutter measurement.
    Peter

Maybe you are looking for