1

I have a table each cell contains a image. if the image size is more than row height, I have added JScrollPane. Now when I run the application, scroll is visible in each cell but I'm not able to scroll it.

How can I do it?

Below is the code I'm using to add scroll pane.

Any sample code will be appreciated.

     private final JTable table = new JTable(model)

    {

    @Override public Component prepareRenderer(TableCellRenderer tcr, int      row, int column) 

    {
         Component c = super.prepareRenderer(tcr, row, column);
         if(isRowSelected(row))              
         {
             c.setForeground(getSelectionForeground());
             c.setBackground(getSelectionBackground());             
         }             
       else{
             c.setForeground(getForeground());
             c.setBackground((row%2==0)?evenColor:getBackground());
         }
         JScrollPane _pane=new JScrollPane(c);
         table.setRowHeight(100);
         return _pane;
     }
};
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rama Krishna
  • 120
  • 8
  • 1
    Seems like the making of an unusable GUI, when you expect the user to scroll in order to see the content of table cells. – Andrew Thompson Apr 01 '15 at 06:03
  • may be following links will helpful to you. http://stackoverflow.com/questions/6388453/adding-a-jscrollpane-component-to-a-jtable-column http://stackoverflow.com/questions/9395554/scrollable-cells-in-jtable – anand kulkarni Apr 01 '15 at 06:04

2 Answers2

0
  1. Right click on your jscrollpane
  2. Properties
  3. HOrizontal scrollbarpolicy = always
  4. Vertical scrollbarpolicy = always

To be able to scroll Right Click on Jtable Autocreatecolumnfrommodel =false(deactivaded)

Isbelmont
  • 21
  • 1
  • 7
0

In order to be able to set the focus on the cell (so you can use the inside scroll pane), you can get it through "edit mode":

  1. Create a custom cell editor:

         public class MyCustomCellEditor extends AbstractCellEditor implements TableCellEditor{
         @Override
         public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int column) {
    
                     JScrollPane your_custom_panel=new JScrollPane();
                     return your_custom_panel; // your _pane
         }
    
         @Override
         public Object getCellEditorValue() {
             return null;
         }
    
  2. Set it on your table:

         table.setDefaultEditor(MyCustomCellValueType.class,new MyCustomCellEditor());
    

Now you only need to manage how to enter in "edit mode" and you should be able to scroll inside the cell.

alcan
  • 31
  • 1
  • 8