Horizontal Scroll in JTable

Hello!
I have a JTable inside a JScrollPane when the table has horizontal scroll it looks strange. When the user clicks in arrows of horizontal scroll the headers of table don't follow the scroll.
What am I doing wrong?
Thanks,
Pati

That's my code
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Teste extends JFrame{
     public Teste()
     this.setSize(320,270);
     JTable table = new JTable(10,15);
     JScrollPane scroll = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
     table.getColumnModel().getColumn(0).setPreferredWidth(150);
     table.getTableHeader().setPreferredSize(new Dimension(150,15));
     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
     scroll.setBounds(10,10,300,250);
     this.getContentPane().add(scroll);
     this.setVisible(true);
     public static void main(String args[]){
          Teste obj = new Teste();          
The problem is in line:
table.getTableHeader().setPreferredSize(new Dimension(150,15));
When I remove it, the table works well.

Similar Messages

  • No horizontal scroll bar on the Jtable. In order to see long column.....

    No horizontal scroll bar on the tJable.
    In order to see long cell data we have to eliminate other columns of the table.
    I need an horizontal scroll bar on the JTable if the data in a cell is longer.
    Could somebody help me out?
    Thanks in advance.

    in the jtable use following method :
    myTable.setAutoResizeMode(this.AUTO_RESIZE_OFF);
    KR,
    Jan

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

  • Horizontal scroll Bars not coming in JTable

    Hi,
    If a user streches any column of the jtable to the right such that its values are not visible properly. Then should the horizontal scroll bar come in the scrollpane in which table is there.??
    If Yes then how can I do it?
    I have seen when no of rows becomes more than the display area of the table then vertical scrollbar are coming automatically. but not Horizontal scroll bars.
    Please help

    sorry for my previous mistype, here is the right solutions to your problem.
    As default the JTable will auto resize your column width, if you want horizon scrollbar
    you must do following below steps:
    JTable tableview = new JTable (model) ;
    tableview.setAutoResizeMode (JTable.AUTO_RESIZE_OFF) ;
    JScrollPane scrollpane = new JScrollPane (tableview,
    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED) ;
    JPanel p ;
    p.add (scrollpane) ;
    This can works for you !

  • JTabel Horizontal scroll bar problem

    Hi,
    I want a horizontal scroll bar added to my table
    hence I do this
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    but the problem is If I increase the size of frame
    the cells do not resize hence there is a gap after
    the last column.
    How do I remove this gap

    Please reduce the container size in the screen thru SE51 that automatically create the scroll
    or check whether in the attributes of the screen "Hold scroll positio" not to be checked

  • Horizontal Scroll Bar does not show

    I have a method which creates a table with values and displays it in a dialog. However, the horizontal scroll bars do not appear, even though there is are columns that are not seen in the view. Here is my code:
         private void setTable()
              Object columns[] = headerLST.toArray(),
                        rows[][] = new Object[rowLST.size()][headerLST.size()];     
              for(int x = 0; x < rowLST.size(); x++)
                   java.util.List list1 = (java.util.List)rowLST.get(x);
                   for(int y = 0; y < list1.size(); y++)
                        rows[x][y] = (String)list1.get(y);     
              DefaultTableModel tValues = new DefaultTableModel(rows, columns);
    JTable tableA = new JTable(tValues);
         tableA.setRowHeight(27);
                   tableA.setRowSelectionAllowed(false);
                   tableA.setColumnSelectionAllowed(false);
                   tableA.setFont(new Font("Dialog", Font.BOLD, 12));
              int minColWidth = 120, maxColWidth = 200;
              for(int y = 0; y < tableA.getColumnCount(); y++)
                   TableColumn tbc = tableA.getColumnModel().getColumn(y);
                   tbc.setPreferredWidth(minColWidth);     
                   tbc.setMinWidth(minColWidth);
                   tbc.setMaxWidth(maxColWidth);
              scrollA = new JScrollPane(tableA);
              this.getContentPane().add(scrollA, BorderLayout.CENTER);
    Can someone tell me what I am missing?
    Thanks
    Augustine

    You need this
    tableA.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    otherwise it will always try and fit the table to the space available

  • Horizontal scroll bar at top in JScrollPane

    Hi,
    How can i get the horizontal scroll bar to be on the top instead of bottom
    here is my code
    TableModel tableModel = new TableModel(totalData,
    columnHeader);
    JTable table = new JTable(tableModel);
    JScrollPane scrollPane = new JScrollPane(table);
    Ashish

    Hi Ash
    I dont know whether this is feasible please sorry if i am wrong.
    Create a JPanel and add scroll bar to the left and top.
    I have posted a Q in the subject JDialog Esc Key. please answer the Q for me.
    Lakshmi.

  • Horizontal scroll bar jumps

    Hi
    When you execute the following code , you will see a table , drag or extend the column size of column D such that , you get a horizontal scroll pane
    Now when you click on first,second or third column ( i.e row selection ) no problem
    but if u select the fourth column i.e D , then the horizontal scroller jumps to right
    any clue where this is done , i dont want this effect , is there a way to disable it ?
    Regards
    kmm
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.table.*;
    import java.awt.Component.*;
    public class Table
    public static void main(String[] args)
    JFrame frame = new JFrame("Table");
    frame.addWindowListener( new WindowAdapter() {
    public void windowClosing(WindowEvent e)
    Window win = e.getWindow();
    win.setVisible(false);
    win.dispose();
    System.exit(0);
    JTable table = new JTable( 6,4 )
    public TableCellRenderer getCellRenderer(int row, int column) {
    TableColumn tableColumn = getColumnModel().getColumn(column);
    TableCellRenderer renderer = tableColumn.getCellRenderer();
    if (renderer == null) {
    Class c = getColumnClass(column);
    if( c.equals(Object.class) )
    Object o = getValueAt(row,column);
    if( o != null )
    c = getValueAt(row,column).getClass();
    renderer = getDefaultRenderer(c);
    return renderer;
    public TableCellEditor getCellEditor(int row, int column) {
    TableColumn tableColumn = getColumnModel().getColumn(column);
    TableCellEditor editor = tableColumn.getCellEditor();
    if (editor == null) {
    Class c = getColumnClass(column);
    if( c.equals(Object.class) )
    Object o = getValueAt(row,column);
    if( o != null )
    c = getValueAt(row,column).getClass();
    editor = getDefaultEditor(c);
    return editor;
    // Buttons
    table.setValueAt( new JButton("Button"), 0, 0 );
    table.setValueAt( new JButton("Button"), 0, 1 );
    // Combobox
    JComboBox combo = new JComboBox( new String[] {"First", "Second", "Third"} );
    table.setValueAt( combo, 1, 1 );
    // Labels
    table.setValueAt( new JLabel("Label"), 1, 0 );
    JLabel label = new JLabel( "Label", new ImageIcon( table.getClass().getResource("new.gif") ),
    JLabel.LEFT);
    table.setValueAt( label, 1, 2 );
    // Scrollbar
    //table.setValueAt( new JScrollBar(JScrollBar.HORIZONTAL), 2,1 );
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    // RadioButtons
    JRadioButton b1 = new JRadioButton( "Group1 Button1" );
    JRadioButton b2 = new JRadioButton( "Group1 Button2" );
    JRadioButton b3 = new JRadioButton( "Group1 Button3" );
    ButtonGroup g1 = new ButtonGroup();
    g1.add( b1 );
    g1.add( b2 );
    g1.add( b3 );
    table.setValueAt( b1, 3, 0 );
    table.setValueAt( b2, 3, 1 );
    table.setValueAt( b3, 3, 2 );
    //checkbox
    JCheckBox c1 = new JCheckBox( "Check box1 " );
    JCheckBox c2 = new JCheckBox( "Check box2" );
    JCheckBox c3 = new JCheckBox( "Check box3" );
    table.setValueAt( c1, 4, 0 );
    table.setValueAt( c2, 4, 1 );
    table.setValueAt( c3,4, 2 );
    table.setDefaultRenderer( JComponent.class, new JComponentCellRenderer() );
    //table.setDefaultEditor( JComponent.class, new JComponentCellEditor() );
    JScrollPane sp = new JScrollPane(table);
    frame.getContentPane().add( sp );
    frame.pack();
    frame.show();
    class JComponentCellRenderer implements TableCellRenderer
    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column) {
    return (JComponent)value;
    }

    It is done by JTable in changeSelection(..) to turn it off:
    table.setAutoscrolls(false);

  • Horizontal scrolling faster than Vertical scrolling

    I have a jscrollpane, containing a jtable as its component view. When i drag the vertical scroll bar, the painting is slower than when i drag the horizontal scrollbar. I've tried using a profiler to understand, but i didn't.
    Can anybody help me ?

    Horizontal scrolling:
    .java.awt.EventQueue.dispatchEvent(AWTEvent) 100%
    ...java.awt.Component.dispatchEvent(AWTEvent) 100%
    ...java.awt.event.InvocationEvent.dispatch() 0%
    Vertical scrolling:
    .java.awt.EventQueue.dispatchEvent(AWTEvent) 100
    ...java.awt.event.InvocationEvent.dispatch() 77%
    ...java.awt.Component.dispatchEvent(AWTEvent) 22%
    the invocatonEvent.dispatch() method call (through several methods) javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(JComponent, Image, Graphics, int, int, int, int).
    In horizontal scroll (fast), this method calls sun.java2d.SunGraphics2D.drawImage(Image, int, int, ImageObserver)
    In vertical scroll (slow), this method calls javax.swing.JComponent.paintToOffscreen(Graphics, int, int, int, int, int, int)
    So i would say that the horizontal scroll calls repaint AFTER some offscreen image buffer is calculated while, the vertical scroll fails to do that.

  • Getting a horizontal scroll bar in the DHTML_SHUTTLE

    Hello,
    We are currently using Carl's DHTML_SHUTTLE procedure to display some shuttles.
    I have a problem where the data is longer than the width of the shuttle and was wondering if it is possible to implement a horizontal scroll bar so that the user can see it?
    Thanks,
    Paul

    alisadri wrote:
    Hi there,
    I have created a JScrollPane object for a JTable. Using the code below, I am trying to create a horizontal scroll pane for the JTable object but the horizontal
    bar doesn't move at all. this is the code. Also when I create the horizontal bar as "AS_NEEDED" the horizontal bar does not appear. The vertical bar works
    properly. Can someone help me with this please.
    This is the Code:
    loadTableScrollPane = new JScrollPane(getLoadTable()); //"getLoadTable()" return a JTable object.
    loadTableScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    loadTableScrollPane.setViewportView(getLoadTable());
    loadTableScrollPane.setBounds(new Rectangle(5, 149, 1135, 232));
    Just a wild guess ... because you do need to post a real example - but get rid of the setBounds and setPreferredSize and use pack() on the frame.

  • Showing Horizontal scroll bar in JTabel

    I am creating a table as follows:
    private ResultSetTableModel tableModel;
    // create TableModel for results of query
    tableModel = new ResultSetTableModel( JDBC_DRIVER, DATABASE_URL, USERNAME, PASSWORD, DEFAULT_QUERY );
    // create JTable delegate for tableModel
    JTable resultTable = new JTable( tableModel );
    sp = new JScrollPane( resultTable);
    where
    ResultSetTable Model is a class which extends AbstractTableModel
    When dispalying the table I can only see the vertical scroll, How can the horizontal scroll be shown??
    Thanks.

    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=623692

  • Making a single column horizontally scrollable for JTable

    Hello Everyone and thank you for reading my post.
    I have been trying for the last 3 days to make a single column horizontally scrollable on my JTable but for some reason I can not get it to work.
    I simplyfied my example to 2 columns and 5 rows where
    the rows in the first column have long text and it will have a horizontal scroll bar to scroll to the end of the text, similiar to split in Excel.
    The second column it doesn't matter
    Thanks again and have a great time,
    Al

    Friend you use table cell renderer for your problem.
    The solution would be like:
    1. Write cell renderer that displays textfield contained in scrollpane.
    2. Apply cell renderer to separate column where you require horizontal scroller.

  • Can't see the horizontal scroll bar in the scroll pane.

    Hi there,
    I have created a JScrollPane object for a JTable. Using the code below, I am trying to create a horizontal scroll pane for the JTable object but the horizontal
    bar doesn't move at all. this is the code. Also when I create the horizontal bar as "AS_NEEDED" the horizontal bar does not appear. The vertical bar works
    properly. Can someone help me with this please.
    This is the Code:
    loadTableScrollPane = new JScrollPane(getLoadTable()); //"getLoadTable()" return a JTable object.
    loadTableScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    loadTableScrollPane.setViewportView(getLoadTable());
    loadTableScrollPane.setBounds(new Rectangle(5, 149, 1135, 232));

    alisadri wrote:
    Hi there,
    I have created a JScrollPane object for a JTable. Using the code below, I am trying to create a horizontal scroll pane for the JTable object but the horizontal
    bar doesn't move at all. this is the code. Also when I create the horizontal bar as "AS_NEEDED" the horizontal bar does not appear. The vertical bar works
    properly. Can someone help me with this please.
    This is the Code:
    loadTableScrollPane = new JScrollPane(getLoadTable()); //"getLoadTable()" return a JTable object.
    loadTableScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    loadTableScrollPane.setViewportView(getLoadTable());
    loadTableScrollPane.setBounds(new Rectangle(5, 149, 1135, 232));
    Just a wild guess ... because you do need to post a real example - but get rid of the setBounds and setPreferredSize and use pack() on the frame.

  • Help with code for inserting horizontal scroll bar in photo gallery in Business Catalyst site?

    Hi,
    I am using Business Catalyst and Dreamweaver to create a trial site.
    I am not sure if this is the correct forum to post this request but anyway I have inserted a photo gallery module at the bottom of the sidebar in the homepage of my test site.
    Can anyone advise on whether jquery or any other code can be inserted into the page or module code that will replace the "next" hyperlink below the first 4 photos with a horizontal scroll bar at bottom of the gallery so users can just scroll through all 12 images ?
    Kind Regards, Matt.
    http://craftime-bs6.businesscatalyst.com/

    Alyssa,
    Have you tried putting this rule back as it was originally:
    /* Submenu that is showing with class designation MenuBarSubmenuVisible, we set left to auto so it comes onto the screen below its parent menu item */
    ul.MenuBarHorizontal ul.MenuBarSubmenuVisible
        left: auto; /*was 9px*/
        color: #EF9CCF;
        background-color: #FFF;
    That is, changing your 9px back to auto.
    And giving  us a link (as you did) is much better than printing out the code for us! Thanks!
    Beth

  • How to get the Horizontal Scroll Bar for a Table?

    Hi All,
    As per my requirement, I am displaying several records in a Screen in a Tabular Format. But here I have to show 21 Columns in that table which is too high. I am able to display it but due to it I am getting a Horizontal scroll bar for the whole screen since all the columns are not getting displayed in the normal window screen space. But its looking too odd since once I am scrolling it to right the columns are getting displayed but the above Header Bar and Global buttons are not displaying, they are bound to the normal screen space.
    Is there a way to have a Horizontal scroll bar only for that table instead of the entire screen so that on scrolling that bar only the table rows will beshifted ant got displayed?
    With Thanks
    Kumar Gautam

    try this approach.
    include a raw text item before and table item.
    include the appropriate HTML tags in raw text item to enable horizontal scroll
    --Prasanna                                                                                                                                                                                                                                                                                                                                   

Maybe you are looking for

  • Mystical PL/SQL Error(?!)

    Greetings! I've a procedure and a trigger. The problem is when I call the proc from inside the trigger I get an error message saying that Error: ORA-00604: error occurred at recursive SQL level 1 ORA-20001: Unhandled error occured! ORA-06512: at "ITE

  • How to do payment for vendor bill booked wrongly deducted with TDS ?

    Dear all, How to cheque payment for a vendor in which tds is wrongly deducted(tds won't be deduted for the bill) for closed period ? How to solve this issue ? Jeyakanthan

  • Spry dataset ie9 display anomaly

    I have a well-formed XML data set with 6 fields per record derived from an Excel spreadsheet which displays exactly as expected in Firefox using a Spry dataset constructed by Dreamweaver CS5.. In IE9, however, an extra blank field is inserted somewhe

  • Not Opening Web Page

    I used to be able to ead a web page, www.tamubyx.org. Now Safari or FireFox won't load it. It says the server is too busy or something like that. However, another roommate can access the page. There is not a problem with the web page itself. How do I

  • Help with Elements dating of photos

    I had an old copy of Elements on my old PC. I then switched to an Apple Mac & purchased the full Photoshop. Unfortunately, I was frustrated there was no organising calendar, which was a wonderful way of easily locating photos. Therefore, I have recen