• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

urgent help needed!!!!!!

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}

}
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Michael but u didn tell me abt 2nd question why selected is printed twice???
thanks to you.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic