Display a horizontal scrollbar in a JTable as to fit column width

hi all,
I am using JTable to display records.I've set a horizontal scrollbar by using 'table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);',but although i got a horizontal scrol bar the column width is constant for all the columns and not getting adjusted according to the lenght of the column names. As the column names and the records are of different size in the database 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?
regards...

.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.

Similar Messages

  • Horizontal scrolling in a JTable with fixed-size columns

    Hello,
    I have come to a roadblock concerning the following issue. Perhaps someone can share their ideas...
    I have a JTable with three fixed-size columns. I implemented a JScrollPane with vertical and horizontal scrolling. However, the text inserted into the last column is too long to fit in the specified column width. I would like to be able to scroll the table so that I can view all of the text in the last column. Right now, the text cuts off and ends with three dots (...). Is there a way in which a horizontal scroll bar can be implemented to view all the text in the last column?
    Here is a portion of my code:
    num_of_rows_Table2 = (int) Math.pow(3, num_of_vars);
    String[] colunmNames_Table2 = {"Decimal Identifier", "Ternary Identifier", "Cancellation of nonimplicants"};
    dataModel2 = new DefaultTableModel(colunmNames_Table2, num_of_rows_Table2);
    SpaceOfTermsTable = new JTable(dataModel2);
    for (int z = 0; z < num_of_rows_Table2; z++)
    SpaceOfTermsTable.setRowHeight(z, row_height);
    SpaceOfTermsTable.setBounds(new Rectangle(x1_Table2, y1_Table2, width_of_pane2, height_pane2));
    SpaceOfTermsTable.getTableHeader().setReorderingAllowed(false);
    for (int i = 0; i < 2; i++)
    TableColumn col = SpaceOfTermsTable.getColumnModel().getColumn(i);
    col.setMinWidth(colwidth_Table2);
    col.setMaxWidth(colwidth_Table2);
    col.setPreferredWidth(colwidth_Table2);
    TableColumn lastcol = SpaceOfTermsTable.getColumnModel().getColumn(2); lastcol.setMinWidth(lastcol_Table2);
    lastcol.setMaxWidth(lastcol_Table2);
    lastcol.setPreferredWidth(lastcol_Table2);
    SpaceOfTermsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    scrollPanel2 = new JScrollPane(SpaceOfTermsTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPanel2.setAutoscrolls(true);
    scrollPanel2.setBounds(new Rectangle(x1_Table2, y1_Table2, width_of_pane2, height_pane2));
    Thanks in advance for all of the help!

    Hi, I hava a similar problem than yours - can you tell me how you sorted out yer's?

  • JTable resizing VS Horizontal scrollbar

    Heila',
    I have to show the content of a text file in a JTable; the JTable has 2 columns: the first contains the line number and the second column contains the text of the line.
    In order to allow the user to view and scroll the whole text line I added an horizontal scrollbar to the JTable.
    Here is the relevant code:
    gridVis.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    JPanGriglia = new JScrollPane(gridVis, JScrollPane.VERTICAL_SCROLLBAR_NEVER,
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    JPanGriglia.getViewport().putClientProperty("EnableWindowBlit", Boolean.TRUE);     
    The problem is that if I set the AutoResizeMode to JTable.AUTO_RESIZE_OFF then I can see the horz scrollbar but when I resize the JFrame containing the JTable the JTable doesn't resize itself.
    Viceversa if I comment the statement:
    gridVis.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    then the JTable resizes correctly but the scrollbar disappears.
    Does exists the way to obtain both this features ?(correct resizing AND horizontal scroll bar)
    Thanks
    Ciao

    Try overriding getScrollableTracksViewportWidth() in JTable. The method returns a boolean value indicating whether the table should resize when its viewport is resized. If you setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS) and add a check to getScrollableTracksViewportWidth() such that it returns true only if viewport.getExtentSize().width is greater than some minimum width, then your table will resize automatically unless it is scaled below the minimum width, in which case a scrollbar will appear. If you're going to do this you'll need your JTable subclass to store a pointer to its scroll pane's viewport so it can get the extent size.

  • Problem with the HORIZONTAL Scrollbar

    Someone (I thanks him) has explain me how to have a horizontal scrollbar in a JTable (I use setHorizontalScrollBarPolicy(...)).
    So, now I have my Horizontal scrollbar but ONLY de bar and NOT the "button" in the bar. So, I am not able to scroll (and for a scrollbar it looks stupid). I have tried also setHorizontalScrollBar(...) but that didn't change anything.

    1. Try calling setEnabled(true); on the scrollbar
    2. Check that you need to scroll. If efffectively the knob (as the API docs call it) is larger than the scrollbar, it probably won't be shown.

  • Horizontal Scrollbar in Datablock

    Hello guys and good morning,
    I would like to ask you for a problem i have in displaying a horizontal scrollbar across a datablock. I have a datablock with 40 columns, from which the 9 are unabled because they are not changed or edited, so i want to scroll across the rest 31 columns. Any idea?
    Thanx a lot in advance,
    Best Regards,
    Bill Kepaptsoglou.

    Horizontal scrollbars are not attached to a block but to a canvas.
    if you need some spreadtable requierement, add a stacked canvas to your content canvas, and set the CANVAS item property of these items to this canvas.
    If the size of the stacked canvas is smaller to fit every items (and it probably should), you will see a horizontal scrollbar displayed at runtime.
    Francois

  • Horizontal scrollbar for plot legend

    Hello,
    The plots in my plot legend for a graph can have quite long names so it would be great having a horizontal scrollbar.
    Quote from here
    >>To add a scroll bar to a plot legend, right-click the plot legend and select Visible Items»Horizontal Scrollbar or Visible Items»Vertical Scrollbar from the shortcut menu.
    I can see the entry horizontal scrollbar, but it is always grayed out, what's wrong?
    Thanks a lot for your help!
    Cheers,
    Rico

    I think you are misinterpreting the use of the scrollbar.  If you have a lot of plots on your graph, then you can use the scrollabr to not show all the plots at once and this allows you to scroll through the list.  It does not allow you to make the plot legend smaller to hide long names and scoll across.
    So, if you resize the plot to be horizontal, then you can display the horizontal scrollbar:
    If you resize it vertically, then you can display the vertical scrollbar:
    Message Edited by Matthew Kelton on 05-09-2008 12:09 PM
    Attachments:
    Plot legend horizontal.png ‏2 KB
    Plot legend vertical.png ‏2 KB

  • Horizontal scrollbars behaving oddly

    This is not a complete show-stopping bug but Logic Express (9) recently started having issues with horizontal scrollbars -- they center themselves. Occasionally they work as they are supposed to, but most of the time they immediately return to the center, which is really irritating after a while. I'll be scrolled all the way to the end of the timeline for example, and I decide I want to scroll back left slightly; I click on the scrollbar and any kind of drag makes it jump back to the center. It acts like the mouse cursor was there, too.
    Reinstallation and trashing preferences did not fix the problem.

    Same problem here, but it seems also related to the screen in use, at least in my setup. I'm running Logic on my MacBook with a native screen resolution of 1200x800. I have a second screen attached above it with a resolution of 1920x1080 which I use for the main Arranger screen, the MacBook screen itself is mainly used for displaying the mixer and AU's.
    When the arranger screen is on my larger display the horizontal scrollbar is flying all over the place when I try to move it, but when I have the arranger window on my MacBook's screen it behaves normally. Seems like if Logic gets confused by the different widths of the screens, like if it is behaving to have 1200 pixels to scroll on while there are 1920.
    Whatever happens, it's kind of annoying...

  • Css: background image that doesn't force a horizontal scrollbar

    I am looking to create a small pattern (an image) that will
    always align to the left and right of my content. All of my content
    is situated in a 760px wide div that is centered with margin:0px
    auto. My problem is that if I create another div to hold the
    additional image, that any browser would probably display a
    horizontal scrollbar if the image and the content together exceed
    the width of the users viewport.
    I thought about doing it this way, but I am not sure if this
    will work... My content div is wrapped inside another div called
    with class whitediv. This whitediv is 100% of the users viewport
    wide. Could I assign a background image to this div, which will
    center, and therefore lurk behind my content div?
    Any suggestions?
    Thanks, dl33

    Bernd,
    It worked perfectly--thank you VERY much for your help!

  • ADF selectmanylistbox with horizontal scrollbar

    <af:selectManyListbox value="#{tree.lstValus}" size="10" inlineStyle="width:260px">
    I have that, if I have more then values 10. a vertical scrollbar will be shown.But in case a value is larger then the selectmanylistbox no horizontal scrollbar shows up.
    If i remove the width, the selectmanylistbox horizontal size is based on length of longest value. But I dont want to remove the width, as it affects my page and style of my page.
    I just need to know how can i add horizontal scrollbar to af:selectmanylistbox

    Hi,
    don' t think you can add this. What's your JDeveloper release ?
    Frank

  • Horizontal Scrollbar AND Always Vissible Columns in Dynpro Table

    Good Day to you all .
    We are required to modify tha layout of a Table in MSS.
    The request is to find a way to have the 3 first columns always vissible, and when we use a Horizontal Scrollbar to have the rest of the columns move , but leave the first columns intact.
    Any suggestions are highly appreciated.
    Thank you .

    make sure the Transparent Container of the Table has " stretched horizontally" option unchecked. it will solve the issue.

  • 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.

  • Displaying Horizontal Scrollbar in h:SelectOneListbox

    I am using myfaces1.1.4.
    I have a popup which will search for branches based on selected countries.So basically it will show branch name and adress in a h:selectOneListbox.I have vertical scroll bar with this, but i am trying to generate the horizontal scroll bar which is not a default with this component, i think i need to customize that component.I tried to play around with style attribute.But it is not working.
    This is the following code i am using:
    <h:selectOneListbox value="#{backingBean.branchID}"
    id="branchResults" size="5" required="false"
    style="WIDTH: 510px; HEIGHT: 80px; FONT-FAMILY: Courier New;">
    <f:selectItems id="branchNames"
    value="#{backingBean.branchMatches}" />
    I appreciate if you can look in to this.
    Thanks.

    Horizontal scrollbars are not attached to a block but to a canvas.
    if you need some spreadtable requierement, add a stacked canvas to your content canvas, and set the CANVAS item property of these items to this canvas.
    If the size of the stacked canvas is smaller to fit every items (and it probably should), you will see a horizontal scrollbar displayed at runtime.
    Francois

  • How to make JTable use horizontal scrollbar

    My application uses a table to which data is added dynamically(both columns and rows). My problem is that when number of columns increase i want JTable to use horizontal scrollbar instead of resizing columns(which i achieve by setting resize mode to OFF state), But due to this if number of columns are less then there's alot of empty space left after showing columns(coz JTable cannot resize cols to fit Viewport area). To achieve both criteria i used following solution
    1. Set Auto resizing to OFF.
    2. Check if width of table is less than viewport width and if so then set auto
    auto resizing ON :) which will fit columns in viewport thereby occupying empty space.
    But this solution doesn't seem to be straight and simple to me. Is anyone aware of a simple solution to this problem.
    Dimension dm_Dm = new Dimension();
        RSBrowser.setModal(false);
        browseTable_Jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        // reqd o.w. JTable resizes column widths instead  of using horizontal
        // scrollbar
        RSBrowser.setVisible(true);
        browseTable_Jt.getSize(dm_Dm);
        // if table width is less than total viewport size then fill the extra
        // space by setting suto resize property to ON.
        if ( browseTable_Jt.getPreferredScrollableViewportSize().getWidth() >
             dm_Dm.getWidth() )
            browseTable_Jt.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
            browseTable_Jt.doLayout();
        RSBrowser.setModal(true);

    hi,
    with your problem statement it seems you should use javax.swing.JScrollPane like the one below
    JScrollPane tableScroller = new JScrollPane(yourTable);
    tableScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    tableScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);hope this helps you,
    regards,
    Afroze.

  • Jtable Horizontal ScrollBar problem

    Hi,
    I am having a Jtabel inside a JscrollPanel with horizontal and vertical scrollpane.
    My table model contains say 20 columns.But i am showing only 10 columns hiding the other 10.when i click on the table header i will show the rest of 10.so now the total columns are 20.but when i scroll thehorizantal bar to the see the other 10 coulmns the header for the latest 10 coulmns is not showing properly.
    Is there anybody to help me to sort out this problem.
    Thanks in advance.
    sree

    Hi,
    how ican saw Scrollbar with out Jtable.AUTO_RESIZR_OFF.That i already did.That not the actual problem.any how i solved this problem finally.It is something with TableHeaderRenderer..Any how thanks for ur help.
    sree

  • JTable horizontal scrollbars problems.

    When I create JTable component with colons that width more than ScrollPane object one, no horizontal bar is created and all colomns shrank so i can't view what is written in cells.
    If somebody know why horizontal bar is not created?
    Some problem with ScrollPane or I need do some trics?
    Thanx:))

    JPanel contentPane;
    XYLayout xYLayout1 = new XYLayout();
    JScrollPane jScrollPane1 = new JScrollPane();
    String[] cols = {"Name", "Type", "Weignt", "Height", "EyaColor", "EyaSkin"};
    String[][] rows = {{"dklfgjfghjfdjfghj","dkffghjfghjfgjjjd","dkffghjfghjfgjjjd,h,ffgdf","dfgdlhjklhjkhjlfgdfg","dfghjlhjlhjkhgjkdfg","dfghdfhgjklhjkgfkgfhjhdfgh"},
    {"dklfgjfghjfdjfghj","dkffghjfghjfgjjjd","dfgffgdf","dfgdfgdfg","dfgdfg","dfghdfhdfgh"},
    {"dklfgjfghjfdjfghj","dkffghjfghjfgjjjd","dfgffgdf","dfgdfgdfg","dfgdfg","dfghdfhdfgh"},
    JTable jTable1 = new JTable(rows,cols);
    //setIconImage(Toolkit.getDefaultToolkit().createImage(TestTableFrame.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(639, 339));
    this.setTitle("Test Table");
    contentPane.add(jScrollPane1, new XYConstraints(21, 29, 570, 279));
    jScrollPane1.getViewport().add(jTable1, null);

Maybe you are looking for