我学会了使用Java swing 2-3周,并且在处理应用程序时遇到问题。我在JTable
中添加了JFrame
,并删除了所有keylisteners
,mouselisteners
,mousemotionlisteners
,并设置了inputmap
并映射了一个新的动作这是空的。
我添加了mouseDrag
事件以选择多行。
我在mouseClicked
上添加了一个left-click
事件,该事件用于选择一行,如果我双击它会在选择行间隔之前清除选择,这会编辑单元格。
我还在mouseRelease
上添加了right click
事件,该事件创建并显示了一个弹出菜单。单击任何项目或在弹出菜单之外后,与之相关的是所选行的末尾将突出显示边框。
即使我另外left click
行,也会发生相同的情况。该行的单元格总是突出显示,如果我拖动鼠标,该单元格将出现在最后一行的末尾。在此单元格突出显示之前,我的keyListeners
和keyBindings
可以正常工作,但是在此之后,当我按一个键时,该单元格将更改为编辑模式,我不想发生的事情celleditorcomponent
keylistener
阻止键入除数字以外的字符,但是在按下键后,如果单元格突出显示,它将键入所有按下的字符,但我只想写数字。同样,textfield
的单元格编辑器组件无法获得焦点,并且textfield
插入号也不可见。
因此,我想通过双击来编辑单元格,并且我不想像其他操作一样通过任何按键来编辑单元格。正如我提到的那样,在显示弹出菜单并单击其中一项或菜单外之后,出现了这些问题。
我尝试了 setSurrenderFocusOnKeyStroke()
方法。第一次按下键后,Textfield成为焦点,除数字外,其他任何字符均不能写入,但必须键入第一个字符。
要进行测试,我对“ S”击键(“ s”的下半键无效)进行了空操作,当我按“ s”时,它起作用了。高亮显示的单元格不会更改为编辑模式,但是我找不到简单地将所有键都输入到输入地图中的情况。
我也尝试添加了一个键侦听器,该侦听器在键入时写入了源事件的源。在显示弹出菜单之前,我默认使用system.out.println方法键入一个char和打印表的属性。在我再次显示弹出菜单后,我再次键入并打印了相同的内容,但是有一些不同。
首次打印 javax.swing.JTable [,0,0,1069x630,alignmentX = 0.0,alignmentY = 0.0,border =,flags = 251658600,maximumSize =,minimumSize =,preferredSize =,autoCreateColumnsFromModel = true,autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS,cellSelectionEnabled = false strong> editingColumn = -1,editingRow = -1 ,gridColor = javax.swing.plaf.ColorUIResource [r = 122,g = 138,b = 153],preferredViewportSize = java。 awt.Dimension [width = 450,height = 400],rowHeight = 30,rowMargin = 1,rowSelectionAllowed = true,selectionBackground = javax.swing.plaf.ColorUIResource [r = 184,g = 207,b = 229],selectionForeground = sun.swing.PrintColorUIResource [r = 51,g = 51,b = 51],showHorizontalLines = true,showVerticalLines = true]
第二次打印 javax.swing.JTable [,0,0,1069x630, 无效 ,alignmentX = 0.0,alignmentY = 0.0,border =,flags = 251658600,maximumSize =,minimumSize =, preferredSize =,autoCreateColumnsFromModel = true,autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS,cellSelectionEnabled = false, editingColumn = 2,editingRow = 0 ,gridColor = javax.swing.plaf.ColorUIResource [r = 122 ,g = 138,b = 153],preferredViewportSize = java.awt.Dimension [width = 450,height = 400],rowHeight = 30,rowMargin = 1,rowSelectionAllowed = true,selectionBackground = javax.swing.plaf.ColorUIResource [r = 184,g = 207,b = 229],selectionForeground = sun.swing.PrintColorUIResource [r = 51,g = 51,b = 51],showHorizontalLines = true,showVerticalLines = true]
我们可以理解,编辑行和列字段会在键侦听器的方法范围之前更改。
*我尝试将左行的编辑行和列设置为-1,并且所有内容都没有改变,但效果仍然如此,但是打印方式却如此。* javax.swing.JTable [,0,0,1069x630,alignmentX = 0.0,alignmentY = 0.0,border =,flags = 251658600,maximumSize =,minimumSize =,preferredSize =,autoCreateColumnsFromModel = true,autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS,cellSelectionEnabled = false strong> editingColumn = -1,editingRow = -1 ,gridColor = javax.swing.plaf.ColorUIResource [r = 122,g = 138,b = 153],preferredViewportSize = java。 awt.Dimension [width = 450,height = 400],rowHeight = 30,rowMargin = 1,rowSelectionAllowed = true,selectionBackground = javax.swing.plaf.ColorUIResource [r = 184,g = 207,b = 229],selectionForeground = sun.swing.PrintColorUIResource [r = 51,g = 51,b = 51],showHorizontalLines = true,showVerticalLines = true]
我猜大概是这个问题。根据表的默认设置,具有焦点的单元格将等待击键更改为编辑模式。当我启动程序时,所有单元格都没有被聚焦。在我右键单击后,弹出菜单得到焦点。然后,隐藏弹出菜单,表格再次获得焦点,而所选行的最后一个单元格奇怪地获得焦点。但是我检查了焦点侦听器,并且有任何焦点侦听器,所以我不知道想法的触发原因。
---------------------------------- VISUAL EXPLANATION OF MY PROBLEM.- ------------------------------------ 如您在图片中看到的,开始时如果我按“ a”,则它不像图片中的第4位。但是在执行第二步和第三步之后,任何键都会激活编辑。如果您看下面的代码,您将看到miktarTextTypedAction这是第三列的侦听器。通常,这不允许写数字以外的字符,但是此规则在执行步骤后也不起作用。
// CELL EDITOR COMPONENT KEYLISTENER
KeyAdapter miktarTextTypedAction = new KeyAdapter() {
public void keyTyped(KeyEvent e) {
JTextField tf = (JTextField)e.getSource();
if(tf==tf3&&e.getKeyChar()==e.VK_ENTER)
b0.doClick();
char c=e.getKeyChar();
int caretpos = tf.getCaretPosition();
String text=tf.getText();
if((c=='\b'||c==127||Character.isDigit(c))) {
int in=tf.getText().indexOf('.');
if(in!=-1 && caretpos>in && text.length()-in==3)
e.consume();
}
else if(c!='.'||caretpos<text.length()-2||text.contains(".")){
e.consume();
}
}
};
// CHANGED TABLE INPUTMAP ACTIONMAP
tbl.setInputMap(tbl.WHEN_FOCUSED,new InputMap());
tbl.setInputMap(tbl.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, new InputMap());
tbl.setSurrendersFocusOnKeystroke(true);
ActionMap myActionMap = new ActionMap();
tbl.setActionMap(myActionMap);
tbl.getInputMap(tbl.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("DELETE"),"deleteSelectedRows");
tbl.getActionMap().put("deleteSelectedRows", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
deleteSelectedRows.actionPerformed(e);
}
});
tbl.getInputMap(tbl.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("S"),"empty");
tbl.getActionMap().put("empty", emptyAction);
// TABLE MOUSE LISTENER
// POPUPMENU IS CREATED EVERY TIME ON RIGHT CLICK FOR NOW
tbl.addMouseListener(new java.awt.event.MouseAdapter() {
int lastClicked=0;
public void mouseClicked(MouseEvent e) {
if(e.getButton()==e.BUTTON1) {
tbl.requestFocus();
if(e.isShiftDown()) {
int rowIndex=tbl.rowAtPoint(e.getPoint());
if(tbl.isRowSelected(rowIndex)) {
tbl.removeRowSelectionInterval(lastClicked, rowIndex);
}
else {
tbl.addRowSelectionInterval(lastClicked, rowIndex);
}
lastClicked=rowIndex;
}
else if(e.isControlDown()) {
int rowIndex=tbl.rowAtPoint(e.getPoint());
if(tbl.isRowSelected(rowIndex)) {
tbl.removeRowSelectionInterval(rowIndex, rowIndex);
}
else {
tbl.addRowSelectionInterval(rowIndex, rowIndex);
}
lastClicked=rowIndex;
}
else {
int count=e.getClickCount();
Point p=new Point(e.getX(),e.getY());
int column = tbl.columnAtPoint(p);
int row=tbl.rowAtPoint(p);
lastClicked=row;
CellEditor gce = tbl.getCellEditor();
if(count==1&&gce==null) {
tbl.clearSelection();
tbl.setRowSelectionInterval(row, row);
}
else if(count==2) {
tbl.clearSelection();
tbl.setRowSelectionInterval(row, row);
tbl.editCellAt(row, column);
JTextField jtf = (JTextField)tbl.getEditorComponent();
jtf.getCaret().setVisible(true);
jtf.setHorizontalAlignment(jtf.CENTER);
jtf.requestFocus();
}
else if(gce!=null) {
int editingRow=tbl.getEditingRow();
int editingColumn=tbl.getEditingColumn();
CEL.oldVal=(String)tbl.getValueAt(editingRow, editingColumn);
CEL.editingRow=editingRow;
CEL.editingColumn=editingColumn;
gce.stopCellEditing();
}
}
}
}
public void mouseReleased(MouseEvent e) {
if(e.getButton()==e.BUTTON3) {
tbl.requestFocus();
TableCellEditor tce = tbl.getCellEditor();
if(tce!=null)
tce.cancelCellEditing();
int row =tbl.rowAtPoint(e.getPoint());
JPopupMenu pum= new JPopupMenu();
JMenuItem i0= new JMenuItem("Secili Verileri Sil");
JMenuItem i1= new JMenuItem("Faturaya Yapilan Odemeleri Listele");
JMenuItem i2= new JMenuItem("Gun Bazinda Bilgi Tablosu Çıkart");
JMenuItem i3= new JMenuItem("Ay Bazinda Bilgi Tablosu Çıkart");
JMenuItem i4= new JMenuItem("Yil Bazinda Bilgi Tablosu Çıkart");
int[] selectedRows = tbl.getSelectedRows();
pum.add(i0);
if(!tbl.isRowSelected(row)||selectedRows.length<=1) {
tbl.clearSelection();
tbl.addColumnSelectionInterval(0, tbl.getColumnCount()-1);
tbl.addRowSelectionInterval(row, row);
pum.add(i1);
pum.add(i2);
pum.add(i3);
pum.add(i4);
}
i0.addActionListener(deleteSelectedRows);
i1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TabloForm2 tf2 = new TabloForm2((String)tbl.getValueAt(row, 0),(String)tbl.getValueAt(row, 1),(String)tbl.getValueAt(row,2));
}
});
i2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
i3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
i4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
for(Object ob:i0.getActionListeners())
pum.show(tbl, e.getX(), e.getY());
}
trd.dragStartedRow=-1;
}
});