当前位置: 技术问答>java相关
180分 请教
来源: 互联网 发布时间:2015-01-06
本文导语: 请教 JTable 的困难问题: 设置jTable的CellEditor为JTextField。 clickCountToStart 设置为0或1。用鼠标单击选中即出现可以编辑的光标,编辑完毕, 按Tab键或箭头键移到下一个Cell,却不出现可以编辑的光标(此 时也能编辑)...
请教 JTable 的困难问题:
设置jTable的CellEditor为JTextField。 clickCountToStart
设置为0或1。用鼠标单击选中即出现可以编辑的光标,编辑完毕,
按Tab键或箭头键移到下一个Cell,却不出现可以编辑的光标(此
时也能编辑)。
请问高手们如何处理 才能 出现 光标!
非常感谢!
http://www.csdn.net/expert/topic/128/128006.shtm 有50分
|
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;
public class TableTest extends JFrame
{
JTable jTable1 = new JTable(3,4);
public TableTest()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
TableTest tableTest1 = new TableTest();
tableTest1.show();
}
protected void processWindowEvent(WindowEvent e){
if(e.getID()==e.WINDOW_CLOSING){
System.exit(0);
}
super.processWindowEvent(e);
}
private void jbInit() throws Exception
{
this.setSize(300,400);
this.getContentPane().add(jTable1, BorderLayout.CENTER);
ListSelectionListener listener = new MySelectionListner();
jTable1.getSelectionModel().addListSelectionListener(listener);
jTable1.getColumnModel().getSelectionModel().addListSelectionListener(listener);
}
class MySelectionListner implements ListSelectionListener{
int lastRow,lastCol;
public void valueChanged(ListSelectionEvent e){
/* if(e.getValueIsAdjusting())
{
System.out.println("value is adjusting!!");
return ;
} */
int row = jTable1.getSelectedRow();
int col = jTable1.getSelectedColumn();
if(lastRow==row && lastCol==col)
{
System.out.println("The same Cell");
return;
}
jTable1.getCellEditor(lastRow,lastCol).cancelCellEditing();
System.out.println("selected row is :"+row+" selected column " + col);
if(row>=0&&col>=0)
{
jTable1.editCellAt(row,col);
TableCellEditor cellEditor = jTable1.getCellEditor(row,col);
Component component = cellEditor.getTableCellEditorComponent
(jTable1,jTable1.getValueAt(row,col),true,row,col);
component.requestFocus();
}
}
}
}
这样可以在同一列中获得光标,可是不能用键盘换到不同列,我现在没时间,下午再看看问题出在哪儿
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;
public class TableTest extends JFrame
{
JTable jTable1 = new JTable(3,4);
public TableTest()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
TableTest tableTest1 = new TableTest();
tableTest1.show();
}
protected void processWindowEvent(WindowEvent e){
if(e.getID()==e.WINDOW_CLOSING){
System.exit(0);
}
super.processWindowEvent(e);
}
private void jbInit() throws Exception
{
this.setSize(300,400);
this.getContentPane().add(jTable1, BorderLayout.CENTER);
ListSelectionListener listener = new MySelectionListner();
jTable1.getSelectionModel().addListSelectionListener(listener);
jTable1.getColumnModel().getSelectionModel().addListSelectionListener(listener);
}
class MySelectionListner implements ListSelectionListener{
int lastRow,lastCol;
public void valueChanged(ListSelectionEvent e){
/* if(e.getValueIsAdjusting())
{
System.out.println("value is adjusting!!");
return ;
} */
int row = jTable1.getSelectedRow();
int col = jTable1.getSelectedColumn();
if(lastRow==row && lastCol==col)
{
System.out.println("The same Cell");
return;
}
jTable1.getCellEditor(lastRow,lastCol).cancelCellEditing();
System.out.println("selected row is :"+row+" selected column " + col);
if(row>=0&&col>=0)
{
jTable1.editCellAt(row,col);
TableCellEditor cellEditor = jTable1.getCellEditor(row,col);
Component component = cellEditor.getTableCellEditorComponent
(jTable1,jTable1.getValueAt(row,col),true,row,col);
component.requestFocus();
}
}
}
}
这样可以在同一列中获得光标,可是不能用键盘换到不同列,我现在没时间,下午再看看问题出在哪儿
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。