2 Replies - 213 Views - Last Post: 02 May 2019 - 06:43 PM Rate Topic: -----

#1 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 66
  • Joined: 05-June 12

ActionListener for switching columns in JTable by mouse dragging

Posted 26 April 2019 - 06:05 AM

Hallo again,

I have a JTable and a class of a customCellRenderer for cell colouring. While cells are being coloured, there is an ArrayList that keeps track of those coloured cells as integers.
My problem is that I want to be able switching columns by dragging with mouse and update this arrayList.

What is the proper ActionListener to use when I switch column positions?
Is This A Good Question/Topic? 0
  • +

Replies To: ActionListener for switching columns in JTable by mouse dragging

#2 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 66
  • Joined: 05-June 12

Re: ActionListener for switching columns in JTable by mouse dragging

Posted 26 April 2019 - 06:52 AM

OK just solved it.

http://www.java2s.co...leComponent.htm
Was This Post Helpful? 0
  • +
  • -

#3 veretimothy   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 01-April 19

Re: ActionListener for switching columns in JTable by mouse dragging

Posted 02 May 2019 - 06:43 PM

good

import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;

public class Main {
  public static void main(String[] argv) throws Exception {
    JTable table = new JTable();
    table.getColumnModel().addColumnModelListener(new MyTableColumnModelListener(table));
  }
}

class MyTableColumnModelListener implements TableColumnModelListener {
  JTable table;
  public MyTableColumnModelListener(JTable table) {
    this.table = table;
  }

  public void columnAdded(TableColumnModelEvent e) {
    int fromIndex = e.getFromIndex();
    int toIndex = e.getToIndex();
    System.out.println(fromIndex);
    System.out.println(toIndex);
  }

  public void columnRemoved(TableColumnModelEvent e) {
    int fromIndex = e.getFromIndex();
    int toIndex = e.getToIndex();
    System.out.println(fromIndex);
    System.out.println(toIndex);
  }

  public void columnMoved(TableColumnModelEvent e) {
    int fromIndex = e.getFromIndex();
    int toIndex = e.getToIndex();
    System.out.println(fromIndex);
    System.out.println(toIndex);
  }

  public void columnMarginChanged(ChangeEvent e) {
    System.out.println(e);
    
  }

  public void columnSelectionchanged(ListSelectionEvent e) {
    System.out.println(e);
  }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1