Solved

Scrolling certain columns in JTable

Posted on 2001-06-07
17
729 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 300 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now