0

i'm writing a piece of code to display images in JXTable retrieved from a database everything works fines .I have fixed the row height of that table , now i want to change the row height dynamically using mouse.

How to acheive this .

I know setRowHeight(100) will be useful how can i pass value dynamically to that .

Thanks inadvance

mKorbel
  • 109,525
  • 20
  • 134
  • 319
maverick
  • 171
  • 1
  • 9
  • @MadProgrammer: not sure what you mean, but JTable can easily cope with different heights for each row by using `JTable.setRowHeight(int, int)` which sets a height for one specific row –  Apr 22 '14 at 06:01
  • @a_horse_with_no_name ... Maybe I'm thinking of `JList`... – MadProgrammer Apr 22 '14 at 06:17
  • @a_horse_with_no_name... your right but i want to change it using mouse and it should be like the way we are changing the Jtable/Jxtable column resizing – maverick Apr 22 '14 at 07:28

1 Answers1

0

Here is some code I found in the web some time ago:

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;

public class TableRowResizer extends MouseInputAdapter
{
    public static Cursor resizeCursor = Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);

    private int mouseYOffset, resizingRow;
    private Cursor otherCursor = resizeCursor;
    private JTable table;

    public TableRowResizer(JTable table){
        this.table = table;
        table.addMouseListener(this);
        table.addMouseMotionListener(this);
    }

    private int getResizingRow(Point p){
        return getResizingRow(p, table.rowAtPoint(p));
    }

    private int getResizingRow(Point p, int row){
        if(row == -1){
            return -1;
        }
        int col = table.columnAtPoint(p);
        if(col==-1)
            return -1;
        Rectangle r = table.getCellRect(row, col, true);
        r.grow(0, -3);
        if(r.contains(p))
            return -1;

        int midPoint = r.y + r.height / 2;
        int rowIndex = (p.y < midPoint) ? row - 1 : row;

        return rowIndex;
    }

    public void mousePressed(MouseEvent e){
        Point p = e.getPoint();

        resizingRow = getResizingRow(p);
        mouseYOffset = p.y - table.getRowHeight(resizingRow);
    }

    private void swapCursor(){
        Cursor tmp = table.getCursor();
        table.setCursor(otherCursor);
        otherCursor = tmp;
    }

    public void mouseMoved(MouseEvent e){
        if((getResizingRow(e.getPoint())>=0)
           != (table.getCursor() == resizeCursor)){
            swapCursor();
        }
    }

    public void mouseDragged(MouseEvent e){
        int mouseY = e.getY();

        if(resizingRow >= 0){
            int newHeight = mouseY - mouseYOffset;
            if(newHeight > 0)
            {
                table.setRowHeight(resizingRow, newHeight);
            }
        }
    }
}

You add it to you table by using:

new TableRowResizer( table );
camickr
  • 321,443
  • 19
  • 166
  • 288