The Artima Developer Community
Sponsored Link

Java Answers Forum
JTable cell editing and event handling

5 replies on 1 page. Most recent reply: Oct 8, 2003 10:22 PM by Desmond Cheng

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
max

Posts: 3
Nickname: java3x
Registered: Sep, 2003

JTable cell editing and event handling Posted: Sep 24, 2003 11:20 PM
Reply to this message Reply
Advertisement
I need to change JTable cell editing behaviour.
I use full row selection mode.
By default the cells are edited by double-clicking on them but i want to start editing using a single click only if the cell is already selected.
In other words: if the row is selected i want to start editing by single clicking on a cell but if i click on a cell of unselected row i dont want the editor starts. Can youi help me? Thanks


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: JTable cell editing and event handling Posted: Sep 25, 2003 3:01 AM
Reply to this message Reply
Add mouseListner to your table and call editCellAt(row,column) in the single click of mouse checking the row is selected or not

max

Posts: 3
Nickname: java3x
Registered: Sep, 2003

Re: JTable cell editing and event handling Posted: Sep 25, 2003 5:51 AM
Reply to this message Reply
it doesn't works

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: JTable cell editing and event handling Posted: Sep 25, 2003 5:56 AM
Reply to this message Reply
It will work..
if i would have time i should have wrote teh code for you..
how you are adding the mouseListner??

Can you please paste that piece of code...
so that i can figure what is wrong (if I am capable of ??? :-) )

max

Posts: 3
Nickname: java3x
Registered: Sep, 2003

Re: JTable cell editing and event handling Posted: Sep 30, 2003 4:44 AM
Reply to this message Reply
here's the code

myTable.addMouseListener(new myDlg_myTable_mouseAdapter(this));

void myTable_mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JTable tabella = ( (JTable) e.getSource());
int riga = tabella.getSelectedRow();

.... // do something
}
}
}

if the cell is editable, when the mouse click is handled the function getClickCount() returns 1

if i set the number of clicks needed to start edit to 1 (writing my own cell editor), always i click on a cell the cell is edited.

i can't handle the mouse click before the cell enter in edit mode

Desmond Cheng

Posts: 2
Nickname: siufu
Registered: Sep, 2003

Re: JTable cell editing and event handling Posted: Oct 8, 2003 10:22 PM
Reply to this message Reply
I have tried to control edit activation in the CellEditor.
final JTable t = new JTable();
t.setCellSelectionEnabled(false);
t.setColumnSelectionAllowed(false);
t.setRowSelectionAllowed(true);
t.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
t.setDefaultEditor(Object.class, 
new DefaultCellEditor(new JTextField())
{
  public boolean isCellEditable(EventObject e)
  {
    if (e instanceof MouseEvent)
    {
      int count = ((MouseEvent) e).getClickCount();
      Point p = ((MouseEvent) e).getPoint();
 
      int previousRow = t.getSelectedRow();
      int previousColumn = t.getSelectedColumn();
      int selectedRow = t.rowAtPoint(p);
      int selectedColumn = t.columnAtPoint(p);
 
      System.out.println("Count = " + count);
      System.out.println("Previous; Row = " + previousRow + " , Column = " + previousColumn);
      System.out.println("Selected; Row = " + selectedRow + " , Column = " + selectedColumn);
 
      return (selectedRow == previousRow || count == 2);
    }
 
    // Check keyboard input event?
 
    return false;
  }
});

Flat View: This topic has 5 replies on 1 page
Topic: LinkedList Previous Topic   Next Topic Topic: any more ideas are welcome?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use