Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


A question about JTables

Posted on 2000-03-11
Medium Priority
Last Modified: 2008-03-03
Is there anyway to add a JTable to an Applet so that if there are a lot of items on the table it adds a horizontal scroll bar and does not resize all of the columns so that it fits the applets area.  

Also is there any way that I can lock a column so that it stays on the screen even when the user scrolls to columns that are not on the view.  

I would be grateful for all help offered regarding this.

Thanks in advance

Question by:johnclarke
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 2608009
The Applet is a Panel. Add to this panel a JScrollPane, then add to the JSCrollPane the JTable.

On the JTable object call the


I hink this should do it. Tell me if this does not work for your code.


Author Comment

ID: 2611500
I would like all of the columns to be resized to the width of the largest item in that column.  

I would also like to be able to lock one of the columns so that it is always visible in the view even when I have scrolled all the way to the end of the JTable.

I would be grateful for all advice offered regarding this.

Thanks in advance.


Expert Comment

ID: 2621161
I've created the following method for creating a table that creates scrollbars automatically if the columns become too wide for the scrollpane.  I believe the tblResults.setAutoResizeMode(JTable.AUTO_RESIZE_OFF) line causes this type of action.  As for your second question, I've never done such a thing.

  public void CreateTable() {
      Object[] columnNames= {"Year", "Income", "Expenses", "Cashflow"};

        //Instantiate table
        Table tblResults = new JTable();
        tblResults.setModel(new DefaultTableModel());
          if( tblResults.getModel() instanceof DefaultTableModel )
            int cols = tblResults.getModel().getColumnCount();
        tblResults.setPreferredScrollableViewportSize(new Dimension(200, 200 ));

        //Add table to scroll pane

        JViewport jv = new JViewport();
        if (tblResults != null) {
          scpResultsTable.getViewport().add(tblResults, null);
      TableColumn column = tblResults.getColumnModel().getColumn(0);

Accepted Solution

ovidiucraciun earned 800 total points
ID: 2723037
I'll answer you to the second question.
It is not posible with a single call ;)
You must build an workaround: When the user srolls you must change the original position the "locked" column
in that way it will be visible.

see this:
public void moveColumn(int column,
                       int targetColumn)


Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question