Search code examples
javaswingxjxtable

Make a scrollable ColumnControl popup JXTable


Good aftermoon. I am having a interesting issue with the propeties of a JXtable.

The thing is that I have created the JXTable with too many columns, so I decided to let the user choose which column to display using the ColumnControlButton that comes by default with the JXTable in the SwingX library.

As I have too many columns, when I press the ColumnControl button, it doesn't show me all the columns that can be hided/displayed. The default ColumnControl doesn't comes with a scrollable property to manage this cases. I need some kind of code like the one is used with the JXTable.. I don't know, maybe something like .setHorizontalScrollEnabled(true), so I can see all the columns in the ColumnControl.

I appreciate in advance your answer guys...

By the way, heres the code that loads the table:

public static void load_resg() throws Exception{

     String[] titles = new String[80];     

     for(int i=0; i<80; i++){
         titles[i]=NbP[i];
     }

      modelResg=new DefaultTableModel(null, titles){

          @Override
             public boolean isCellEditable(int i, int i1) {
               return false; //To change body of generated methods, choose Tools | Templates.
             }
        };

      BufferedReader bw = new BufferedReader(new FileReader("Out\\Depth.txt"));
      String line; 

      while((line=bw.readLine())!=null){
          modelResg.addRow(line.split(";"));
      }

    bw.close();
    resg.setIntercellSpacing(new Dimension(5, 5));
    resg.setHighlighters(HighlighterFactory.createSimpleStriping());
    resg.setHorizontalScrollEnabled(true);
    resg.packSelected();
    resg.setSortable(true);
    resg.getColumnControl();
    resg.setColumnControlVisible(true);
    resg.setModel(modelResg);

}

Solution

  • You should be able to use the Table Column Manager which was designed to be used with a JTable. It will display a menu of all the columns with a JCheckBoxMenuItem so you can choose which columns to display.

    but it doesn't add a vertical scroll bar to the pop up, so it doesn't let me see all the columns

    When you have that many menu items you might then want to use the Menu Scroller. This will allow you control how many menu items are displayed at one time and will then add up/down buttons to scroll through the remaining menu items.

    In the showPopup(...) method from the TableColumnManager class I added the following line of code:

    MenuScroller.setScrollerFor(popup, 10);
    popup.show(header, r.x, r.height);
    

    to display 10 menu items at one time. Hoevering the mouse over the arrows will cause the menu to scroll.