23 tháng 11 2006
22 tháng 11 2006
21 tháng 11 2006
Swing: How to show JPopupMenu on Empty JTable ?
Swing: How to show JPopupMenu on Empty JTable ?
At 7:03 PM on Sep 15, 2005, Santhosh Kumar T wrote:
It looks like as simple as writing a MouseListener implementation and register with JTable where the MouseListener implementation shows JPopupMenu.
But this doesn't works if there are no rows in JTable. Why so? The height of the JTable is zero when there are no rows, Thus the area where user clicks is not JTable, it is JViewPort.
This can be solved by overriding the method getScrollableTracksViewportHeight () of Scrollable interface. JTable already implements Scrollable interface. So we just override that method and make the table height same as that of view-port if its preferred size is smaller than view-port size
JTable table = new JTable(0, 5){
public boolean getScrollableTracksViewportHeight() {
return getPreferredSize().height < getParent().getHeight();
}
};
This is how JTextComponent handles this. You can checkout JTextComponent.java and see getScrollableTracksViewportHeight() implementation.
Any JComponent can be scrollable by adding to JScrollable. Then what is Scrollable interface is for ? Scrollable interface which gives some hints to perform better scrolling, such as how many pixels to move per mouse-click or page up or page down.
The demo contains two tables. and both tables have the same MouseListener registered. But only the bottom table shows JPopupMenu on right mouse click. The bottom table overrides getScrollableTracksViewportHeight() as explained above.
Select scroll new item
tbList.getSelectionModel().setSelectionInterval(
tbList.scrollCellToVisible(tbList.getRowCount() - 1,tbList.getRowCount() - 1);
tbList.getRowCount()-1, tbList.getRowCount()-1
);tbList.scrollCellToVisible(tbList.getRowCount() - 1,tbList.getRowCount() - 1);
TableModel Standard
class MyModel extends AbstractTableModel{
private String[] _columns = new String[]{"Id", "Name"};
private Class[] _classnames = new Class[]{Integer.class,String.class};
private Vector _data ;
public MyModel( Vector data ){
_data = data;
}
public int getRowCount() {
return _data.size();
}
public int getColumnCount() {
return _columns.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
MyObject obj = _data.get( rowIndex );
switch ( columnIndex ){
case 0:
return obj.getId();
case 1:
return obj.getName();
}
return null;
}
public String getColumnName(int col)
{
return _columns[col];
}
public Class getColumnClass(int col)
{
return _classnames[ col ];
}
public void addNewItem( MyObject obj ){
_data.add( obj );
this.fireTableRowsInserted( this.getRowCount(), this.getRowCount() );
this.fireTableDataChanged();
}
}
class MyObject{
private int _id;
private String _name;
public void setId( int id ){
_id = id;
}
public void setName ( String name ){
_name = name;
}
public int getId(){
return _id;
}
public String getName(){
return _name;
}
}
private String[] _columns = new String[]{"Id", "Name"};
private Class[] _classnames = new Class[]{Integer.class,String.class};
private Vector
public MyModel( Vector data ){
_data = data;
}
public int getRowCount() {
return _data.size();
}
public int getColumnCount() {
return _columns.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
MyObject obj = _data.get( rowIndex );
switch ( columnIndex ){
case 0:
return obj.getId();
case 1:
return obj.getName();
}
return null;
}
public String getColumnName(int col)
{
return _columns[col];
}
public Class getColumnClass(int col)
{
return _classnames[ col ];
}
public void addNewItem( MyObject obj ){
_data.add( obj );
this.fireTableRowsInserted( this.getRowCount(), this.getRowCount() );
this.fireTableDataChanged();
}
}
class MyObject{
private int _id;
private String _name;
public void setId( int id ){
_id = id;
}
public void setName ( String name ){
_name = name;
}
public int getId(){
return _id;
}
public String getName(){
return _name;
}
}
20 tháng 11 2006
Đăng ký:
Bài đăng (Atom)