This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

urgent help needed!!!!!!  RSS feed

 
reehan ishaque
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks!
i have these 2 problems in JTable.
1)
how to set editing of cells disabled, by default it is enabled but i want to disable it.i didn find any method. i have used cancelCellEditing() of DefaultCellEditor and stopCellEditing() but it didn work.

2)
when i select any row or cell, listSelectionEvent
is fired. but in this code it is being fired twice . selected is printed twice. please tell me abt it why it is doin so.
check the code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
public class TableTest extends JFrame implements ListSelectionListener
{
String data[][] = {

{"1","first1", "last1"},
{"2","first2", "last2"},
{"3","first3","last3"}
};

String columns[] = {"#","first name","last name"};
JTable t;
DefaultTableModel dt;

ListSelectionModel l;

TableTest()
{


dt = new DefaultTableModel(data,columns);
t = new JTable(dt);


l = t.getSelectionModel();
l.addListSelectionListener(this);

JScrollPane pane = new JScrollPane(t);
Container c = getContentPane();
c.add(pane,BorderLayout.CENTER);

setVisible(true);
setSize(200,200);
}

public void valueChanged(ListSelectionEvent e)
{
System.out.println("selected");
}
public static void main(String args[])
{
new TableTest();
}

}
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi reehan,

1)
how to set editing of cells disabled, by default it is enabled but i want to disable it.i didn find any method. i have used cancelCellEditing() of DefaultCellEditor and stopCellEditing() but it didn work.

You need to subclass either DefaultTableModel and override the isCellEditable(int row, int column) method to return false or subclass AbstractTableModel and implement getRowCount(), getColumnCount() and getValueAt(int row, int column) and use that for your table model.
Hope this helps,
Michael Morris
 
reehan ishaque
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Michael but u didn tell me abt 2nd question why selected is printed twice???
thanks to you.
 
Thomas Suer
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Reehan!
One event is fired when the user presses the mouse button and starts his selection. For every selection change he makes (by dragging the mouse) during he still presses the mouse button another event is fired. And finally when he releases the mouse button a last event is fired.
To find out if you got the last event use the method 'getValueIsAdjusting()' which returns false if the selection is finished. So all you have to do is to implement an if statement that recognizes this value:
public void valueChanged(ListSelectionEvent e)
{
if (e.getValueIsAdjusting())
{
System.out.println("selection finished");
}
else
{
System.out.println("selecting...");
}
}
Hope that helps
Thomas
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!