JTable Row wächst/schrumpft nach Klicken

Romsl

Erfahrenes Mitglied
Hallo,

hier mal ein kleines Beispiel wie das realisiert werden könnte.

Java:
package de.tutorials.smooth;

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

/**
 * <code>SmoothTableExample</code>.
 * User: rro
 * Date: 19.07.2006
 * Time: 22:40:44
 *
 * @author Romsl
 * @version $Id$
 */
public class SmoothTableExample {

    public static void main(String[] args) {
        JFrame frame = new JFrame("SmoothTableExample");

        final JTable table = new JTable(new Object[][]{{"a", "a", "a"}, {"b", "b", "b"}, {"c", "c", "c"}}, new Object[]{"a", "b", "c"});

        table.addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e) {

                int row = table.rowAtPoint(e.getPoint());

                if (SwingUtilities.isLeftMouseButton(e)) {
                    new SmoothThread(table, row, SmoothAction.GROW);
                }
                else if (SwingUtilities.isRightMouseButton(e)) {
                    new SmoothThread(table, row, SmoothAction.SHRINK);
                }
            }
        });

        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setVisible(true);
    }

    static enum SmoothAction {GROW, SHRINK}

    static class SmoothThread extends Thread {

        private JTable table;
        private int row;
        private SmoothAction smoothAction;

        SmoothThread(JTable table, int row, SmoothAction smoothAction) {
            this.table = table;
            this.row = row;
            this.smoothAction = smoothAction;
            start();
        }

        public void run() {
            for (int i = 0; i < 20; i++) {

                if (SmoothAction.GROW.equals(smoothAction)) {
                    table.setRowHeight(row, table.getRowHeight(row) + 1);
                }
                else if (SmoothAction.SHRINK.equals(smoothAction)) {
                    table.setRowHeight(row, table.getRowHeight(row) - 1);
                }

                try {
                    sleep(50);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Gruß

Romsl
 
Zurück