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

x
?
Solved

Scrolling certain columns in JTable

Posted on 2001-06-07
17
Medium Priority
?
775 Views
Last Modified: 2010-10-05
Im curious as to the feasibility of scrolling only certain columns of a JTable.
For instance say I have a JTable with 10 columns and I want the first 2 columns to stay in place (similar to headings) and the last column to stay in place as well. The remaining 7 I want to scroll since I only have room for 4. The JTable will almost always have a vertical scrollbar that will scroll the rows (vertically). But the horizontal scrollbar will keep some columns in place and scroll only certain columns while keeping others in place. (The scrolling ones will always be continous, obviously).

What would I need to do to accomplish this? Im pretty knowledgable about the JTable itself, however I have yet to customize it a whole lot to fit my needs with the exception of custom renderers and editors.

Im willing to give 2 - 300 point questions (600*4) for full working code that meets my needs (if someone has something like this tucked away and wants to make it available)
I havent been able to find any open source projects for this but Im willing to give the full 2 question points if someone can give me a link to one that can be used in a commercial project.
Otherwise just a general discussion on how someone may go about it will yield less points (150-300 points).
0
Comment
Question by:mbunkows
[X]
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
  • 11
  • 5
17 Comments
 

Expert Comment

by:sontnvn
ID: 6166387
Hi,
i don't know if you have had java web start.
it has very usefull about JTable that has feature same to your.
It has source code also.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6167677
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6167685
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:Ovi
ID: 6167690
It seems that I cannot post
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6167695
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6167707
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6167715
I think you must reject the answear since I cannot manage to post the code
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6167716
I think you must reject the answear since I cannot manage to post the code
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6167717
I think you must reject the answear since I cannot manage to post the code
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6167718
I think you must reject the answear since I can manage to post the code
0
 

Author Comment

by:mbunkows
ID: 6167962
Ovi:
Ack!
You can email me if you have something. (I rejected so it stays on the open questions in case someone else has something as well).
mjbunks@hotmail.com
I will still provide the points at this site.

sontnvn:
Im familiar with JWS. As far as I know it doesn't add anything to JTable unless you are talking about an example they have using JWS. If you have a link that would be great. (I will also check it out).

If this is easily possible this will save me a HUGE amount of time. Thank you guys. I look forward to seeing what you have!
0
 

Author Comment

by:mbunkows
ID: 6169273
I've played with the JScrollPane.setRowHeaderView() method trying to add another JTable as a rowheader. Its ugly and still no luck getting the header to be on the top level. (the main ColumnHeaderView doesnt cover the row headers and if I add a ColumnHeader to the RowHeaderView JTable it shows on the row below where it should be (like you might expect)).
Ive posted the code (simply for your amusement).
Thanks in advance for ANY help on this. Thanks!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RowTableTest {
     public static void main(String args[])  {
          JFrame f = new JFrame("Test");
          f.addWindowListener(new WindowAdapter()  {
               public void windowClosing(WindowEvent e) {
                    System.exit(0);
               }
          });
          JTable main= new JTable(getCells(15,5),getHeadings(5));
          main.setPreferredScrollableViewportSize(new Dimension(300,200));
          main.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
          JScrollPane mainSP = new JScrollPane(main,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
          JViewport viewport = mainSP.getViewport();

          JTable rowHead= new JTable(getCells(15,2),getHeadings(2));
          //rowHead.setPreferredSize(new Dimension(150,200));
          //rowHead.setSize(new Dimension(150,200));
          //rowHead.setPreferredScrollableViewportSize(new Dimension(275,200));
          //JScrollPane rowHeadSP = new JScrollPane(rowHead,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          mainSP.setRowHeaderView(rowHead);

          f.setContentPane(mainSP);
          f.setSize(450,250);
          f.setVisible(true);
     }
     public static String[] getHeadings(int cols)  {
          String[] headings = new String[cols];
          for (int i=0;i<cols;i++)  {
               headings[i] = "colheading" + i;
          }
          return headings;
     }
     
     public static String[][] getCells(int rows, int cols)  {
          String[][] cells = new String[rows][cols];
          for (int i=0;i<rows;i++)  {
               for (int j=0;j<cols;j++)  {
                    cells[i][j] = "row" + i + "col" + j;
               }
          }
          return cells;
     }
}

0
 

Author Comment

by:mbunkows
ID: 6169277
You have to manually resize the frame out to the right to see the main JTable. For some reason setting the preferredsize or actualsize doesnt work.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6181319
I've tried and the bad news is that you cannot lock both left and right parts of the table. If you are interested, I can send you the code which is loking columns from the left part of the table and give you the possibility to scroll the rest of them. For example you lock first 2 columns and scroll only the remaining 8.
0
 

Author Comment

by:mbunkows
ID: 6181686
That would be ok. I can move the last column over to the left.
Im interested to know how its done.
Ive noticed from other questions that sometimes you have to post code in two or three separate comments, maybe thats what was happening before.

If you could post the code that will lock X columns on the left, I'll give the full points.
You have no idea how much help this would be.

Thanks a lot!
0
 
LVL 9

Accepted Solution

by:
Ovi earned 1200 total points
ID: 6184550
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableColumnCustomizer extends JFrame  {
     class SharedModel extends AbstractTableModel {
          public int getRowCount() { return(10); }
          public int getColumnCount() { return(10); }
    public Object getValueAt(int row, int col) {
         return("(" + row + "," + col + ")");
    }
     }
 
  TableModel sharedModel = new SharedModel();
  JTable table = new JTable(sharedModel);
  JTable headerTable = new JTable(sharedModel);
  TableColumnModel tcm = table.getColumnModel();
  TableColumn[] lockedColumns =  { tcm.getColumn(0), tcm.getColumn(1), tcm.getColumn(2)};
 
     public TableColumnCustomizer() {
          init();
     }
 
  private void init() {
       setLocation(150, 100);
    setSize(500, 300);
    Container c = getContentPane();
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
       headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    headerTable.getTableHeader().setReorderingAllowed(false);
    int width = 0;
    for(int i=0; i<lockedColumns.length; i++) {
         width += lockedColumns[i].getPreferredWidth();
    }
    headerTable.setPreferredScrollableViewportSize(new Dimension(width +
    headerTable.getColumnModel().getColumnMargin(), 0));
    c.add(new ControlPanel(), BorderLayout.NORTH);
    c.add(new JScrollPane(table), BorderLayout.CENTER);
  }
 
  class ControlPanel extends JPanel {
       JCheckBox checkBox = new JCheckBox("Lock");
   
    public ControlPanel() {
         add(checkBox);
      checkBox.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent ae) {
                JScrollPane scroll = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, table);
          if(checkBox.isSelected()) {
               for(int i=0; i<lockedColumns.length; i++) {
                    tcm.removeColumn(lockedColumns[i]);
               }
            scroll.setRowHeaderView(headerTable);
            scroll.setCorner(JScrollPane.UPPER_LEFT_CORNER, headerTable.getTableHeader());
          } else {
               for(int i=0; i<lockedColumns.length; i++) {
                    tcm.addColumn(lockedColumns[i]);
                    int count = tcm.getColumnCount();
                    tcm.moveColumn(count - 1, i);
               }
               scroll.setRowHeaderView(null);
          }
          table.revalidate();
           }
      });
    }
  }
 
  public static void main(String[] args) {
       (new TableColumnCustomizer()).setVisible(true);
  }
}
0
 

Author Comment

by:mbunkows
ID: 6192542
Thanks for the example.
I was on the right track but I never figured out to set the corner to the TableHeader and how to handle the sizing.

Since a question can only be 300 points, I've posted another question here for Ovi:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20135658
Hopefully, Im not breaking any rules by doing this. Im sure someone will tell me if I am. It was a much needed solution to a growing problem. Thanks again Ovi.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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