当前位置: 技术问答>java相关
请教 JTable 的困难问题2
来源: 互联网 发布时间:2015-01-05
本文导语: 请教 JTable 的困难问题: 设置jTable的CellEditor为JTextField。 clickCountToStart 设置为0或1。用鼠标单击选中即出现可以编辑的光标,编辑完毕, 按Tab键或箭头键移到下一个Cell,却不出现可以编辑的光标(此 时...
请教 JTable 的困难问题:
设置jTable的CellEditor为JTextField。 clickCountToStart
设置为0或1。用鼠标单击选中即出现可以编辑的光标,编辑完毕,
按Tab键或箭头键移到下一个Cell,却不出现可以编辑的光标(此
时也能编辑)。
请问高手们如何处理 才能 出现 光标!
非常感谢!
设置jTable的CellEditor为JTextField。 clickCountToStart
设置为0或1。用鼠标单击选中即出现可以编辑的光标,编辑完毕,
按Tab键或箭头键移到下一个Cell,却不出现可以编辑的光标(此
时也能编辑)。
请问高手们如何处理 才能 出现 光标!
非常感谢!
|
//看看这个,我测试了一下,可以出现cursor了。
//最后的几行forcing the caret to show。
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 table = new JTable(3,4);
public TableTest(){
ListSelectionListener listener = new MySelectListener();
table.getSelectionModel().addListSelectionListener(listener);
table.getColumnModel().getSelectionModel().addListSelectionListener(listener);
setSize(300,400);
getContentPane().add(table, BorderLayout.CENTER);
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public static void main(String[] args){
TableTest tableTest1 = new TableTest();
tableTest1.show();
}
class MySelectListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
System.out.println("no cell are selected");
}
else {
int selectRow=table.getSelectedRow();
if(selectRow==-1) selectRow =0;//加这样一行时为了防止出现异常。
int selectCol=table.getSelectedColumn();
System.out.println("row"+selectRow+","+"col"+selectCol);
table.editCellAt(selectRow,selectCol);
Component comp=table.getEditorComponent();
//((JTextField)comp).getCaret().setVisible(true);
comp.requestFocus();
}
}
}
}
//最后的几行forcing the caret to show。
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 table = new JTable(3,4);
public TableTest(){
ListSelectionListener listener = new MySelectListener();
table.getSelectionModel().addListSelectionListener(listener);
table.getColumnModel().getSelectionModel().addListSelectionListener(listener);
setSize(300,400);
getContentPane().add(table, BorderLayout.CENTER);
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public static void main(String[] args){
TableTest tableTest1 = new TableTest();
tableTest1.show();
}
class MySelectListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
System.out.println("no cell are selected");
}
else {
int selectRow=table.getSelectedRow();
if(selectRow==-1) selectRow =0;//加这样一行时为了防止出现异常。
int selectCol=table.getSelectedColumn();
System.out.println("row"+selectRow+","+"col"+selectCol);
table.editCellAt(selectRow,selectCol);
Component comp=table.getEditorComponent();
//((JTextField)comp).getCaret().setVisible(true);
comp.requestFocus();
}
}
}
}
|
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();
}
}
}
}
这样可以在同一列中获得光标,可是不能用键盘换到不同列,我现在没时间,下午再看看问题出在哪儿