Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Jtable Cell and ENTER key

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
i have a table with three columns and three rows
if i have selected 0,0 cell and now i press enter
next cell that gets selected is 1,0... but
what i want is 0,1.. although i try to do this
by editCellAt.. i am not able to do this..
i have the code below....
can anyone tell how to override this behaviour
Thanks
vinaya

import javax.swing.table.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
class testTable extends JPanel
{
JTable table;
DefaultTableModel tableModel;
int selectedRow=0;
int selectedCol=0;

String columnNames[]=
{
"Name","Type","Value"
};

String arr[]=
{
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8"
};

Object data[][]=
{
{
"abc","1","123"
},
{
"xyz","2","789"
},
{
"pqr","3","456"
}
};

JComboBox combo=new JComboBox(arr);


public testTable()
{
tableModel=new DefaultTableModel(data,columnNames);

table=new JTable(tableModel);


table.setPreferredScrollableViewportSize(new Dimension(200,200));

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

table.addKeyListener(new TableKeyListener());
//table.addMouseListener(new TableMouseListener());

table.scrollRectToVisible(table.getCellRect(table.getSelectedRow
(),0,true));
ListSelectionModel rowSM = table.getSelectionModel();

rowSM.addListSelectionListener(new RowSelectionListener());

ListSelectionModel colSM = table.getColumnModel().getSelectionModel();

colSM.addListSelectionListener(new ColumnSelectionListener());

//setting cell editor to textEditor for column with Name
TableColumn typeColumn= table.getColumnModel().getColumn(0);
typeColumn.setCellEditor(new DefaultCellEditor(new JTextField()));

//setting cell editor to textEditor for column with Value
typeColumn= table.getColumnModel().getColumn(2);
typeColumn.setCellEditor(new DefaultCellEditor(new JTextField()));

//setting cell editor to combo for column with Type
typeColumn= table.getColumnModel().getColumn(1);
typeColumn.setCellEditor(new DefaultCellEditor(combo));

add(table);
}


void tabKeyfunc(int row,int col)
{
;
}

void enterKeyfunc(int row,int col)
{

if (col < 2)
{
col++;

}else{
if (table.getRowCount() < row)
{
row++;
col=0;
}
}

System.out.println("row::"+row+"col::"+col);
table.setRowSelectionInterval(row,row);
table.editCellAt(row,col,null);
}

class TableKeyListener implements KeyListener
{
public void keyPressed(KeyEvent e)
{
int keyVal= e.getKeyCode();

switch (keyVal)
{
case KeyEvent.VK_ENTER:
enterKeyfunc(selectedRow,selectedCol);
break;

case KeyEvent.VK_TAB:
tabKeyfunc(selectedRow,selectedCol);
break;
}//end of switch

}

public void keyTyped(KeyEvent e)
{
}

public void keyReleased(KeyEvent evt)
{
}
}

class RowSelectionListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();

if (!lsm.isSelectionEmpty())
{
selectedRow = lsm.getMinSelectionIndex();
}
}
}

class ColumnSelectionListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();

if (!lsm.isSelectionEmpty()){
selectedCol = lsm.getMinSelectionIndex();
}
}
}


public static void main(String args[])
{
testTable tab=new testTable();

JFrame test=new JFrame("Test Table");

test.getContentPane().add(tab);

test.show();
test.pack();
}

}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic