Solved

A question about JTables

Posted on 2000-03-11
5
287 Views
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

John
0
Comment
Question by:johnclarke
5 Comments
 
LVL 8

Expert Comment

by:diakov
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

setAutoResizeMode(SwingConstants.AUTO_RESIZE_OFF);

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

Cheers,
  Nik
0
 

Author Comment

by:johnclarke
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.

John
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2612260
0
 

Expert Comment

by:WilliamCHall
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.setRequestFocusEnabled(false);
        tblResults.setModel(new DefaultTableModel());
          if( tblResults.getModel() instanceof DefaultTableModel )
          {
            ((DefaultTableModel)tblResults.getModel()).setColumnIdentifiers(columnNames);
            int cols = tblResults.getModel().getColumnCount();
          }
        tblResults.setPreferredScrollableViewportSize(new Dimension(200, 200 ));

        //Add table to scroll pane

        JViewport jv = new JViewport();
        tblResults.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jv.setView(tblResults);
        if (tblResults != null) {
          scpResultsTable.setViewport(jv);
          scpResultsTable.getViewport().add(tblResults, null);
        }
      TableColumn column = tblResults.getColumnModel().getColumn(0);
      column.setPreferredWidth(160);
  }
0
 
LVL 3

Accepted Solution

by:
ovidiucraciun earned 200 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)


0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

832 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