Results 1 to 6 of 6
  1. #1
    hiranya is offline Member
    Join Date
    Jun 2007
    Location
    Colombo, Sri Lanka
    Posts
    32
    Rep Power
    0

    Default Scrolling with JTables

    Hi everyone,

    I'm fairly new to building GUIs with Java. I'm trying to display the contents of a database table using a JTable object. The table has 13 columns. So I want the user to be able to scroll horizontally to see all the columns in the JTable object. Can anyone give me an example code segment that fits this scenario.

    (All the data retrieval tasks are working fine. I just want the user to be able to scroll. The code I have written displays all the 13 columns in the viewable area of the JTable by reducing the column widths.)

    Regards,
    Hiranya

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Mount the JTable in a JScrollPane. Since JTable implements the Scrollable interface a parent scrollPane will have access to all the information it needs to properly show the table You can specify the preferred viewport size with the JTable setPreferredViewportSize method.
    Java Code:
    JTable table = yourTable
    // provide a size hint
    Dimension d = table.getPreferredSize();
    d.width = as you like
    d.height = ...
    table.setPreferredScrollableViewportSize(d);
    getContentPane().add(new JScrollPane(table));

  3. #3
    hiranya is offline Member
    Join Date
    Jun 2007
    Location
    Colombo, Sri Lanka
    Posts
    32
    Rep Power
    0

    Default

    Thanks....
    I would also like to know how to make the contents in a JTable read only.

    Regards,
    Hiranya

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

  5. #5
    hiranya is offline Member
    Join Date
    Jun 2007
    Location
    Colombo, Sri Lanka
    Posts
    32
    Rep Power
    0

    Default

    Is there a way to programmatically select a row in a JTable? I want the first row of the table to be selected by default when the table loads.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Yes. You can do it through the tables SelectionModel or you can use the JTable convenience method
    Java Code:
    changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
    See api for use of the boolean flags.

Similar Threads

  1. MouseMotionListener 'scrolling'
    By Thez in forum Java 2D
    Replies: 3
    Last Post: 03-12-2009, 11:48 AM
  2. Replies: 1
    Last Post: 03-28-2008, 10:26 AM
  3. Applet with scrolling status line
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-10-2008, 02:53 PM
  4. AWT Table Scrolling
    By albert_kam in forum AWT / Swing
    Replies: 0
    Last Post: 01-03-2008, 11:37 AM
  5. JScrollPane not scrolling
    By Riftwalker in forum Advanced Java
    Replies: 2
    Last Post: 07-17-2007, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •