
Опытный
 
Профиль
Группа: Участник
Сообщений: 358
Регистрация: 17.5.2007
Где: Молдова, Кишинев
Репутация: 1 Всего: 1
|
| Код | private JTable getJTable() { if (jTable == null) { jTable = new JTable(); jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); jTable.setRowSelectionAllowed(true); jTable.setColumnSelectionAllowed(true); jTable.setCellSelectionEnabled(true); jTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); jTable.setShowGrid(true); jTable.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); Enter=0; new TableFindAction().install(jTable); jTable.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent e) { int c=0; if((jTable.getSelectedColumn()%3)==2){ c=jTable.getSelectedColumn(); } if((jTable.getSelectedColumn()%3)!=2){ if((jTable.getSelectedColumn()%3)==0){ c=jTable.getSelectedColumn()+2; } if((jTable.getSelectedColumn()%3)==1){ c=jTable.getSelectedColumn()+1; } } jTable.editCellAt(jTable.getSelectedRow(),c ); // if(c==2){ Component editor = jTable.getEditorComponent(); editor.requestFocus(); // fix! if (editor instanceof JTextField) { ((JTextField)editor).selectAll(); // а это так, бонус... ((JTextField)editor).setText(""); } } // } }); jTable.addKeyListener(new java.awt.event.KeyListener() { public void keyTyped(java.awt.event.KeyEvent e) { } public void keyPressed(java.awt.event.KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { int i = jTable.getSelectedColumn(); if(i%3==0){ if(i!=0){ jTable.changeSelection(jTable.getSelectedRow(), jTable.getSelectedColumn(), false, false); jTable.requestFocus(); }else{ jTable.changeSelection(jTable.getSelectedRow(), 3, false, false); jTable.requestFocus(); } }else if((i%3)==1){ if(i!=1){ jTable.changeSelection(jTable.getSelectedRow(), i-1, false, false); jTable.requestFocus(); }else{ jTable.changeSelection(jTable.getSelectedRow(), 3, false, false); jTable.requestFocus(); } }else if((i%3)==2){ if(i!=2){ jTable.changeSelection(jTable.getSelectedRow(), i-2, false, false); jTable.requestFocus(); }else{ jTable.changeSelection(jTable.getSelectedRow(), (jTable.getColumnCount()-1), true, true); jTable.requestFocus(); } } }else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { int row= jTable.getSelectedRow(); if((row+1)==jTable.getRowCount()){ int i = jTable.getSelectedColumn(); // System.out.println("col="+i); // System.out.println("row="+row); // System.out.println("wsego row="+jTable.getRowCount()); if(i%3==0){ // System.out.println("Kol="+i); if(i==(jTable.getColumnCount()-3)){ jTable.changeSelection(jTable.getSelectedRow(), (jTable.getColumnCount()-1), false, false); jTable.requestFocus(); }else{ jTable.changeSelection(0, i+4, false, false); jTable.requestFocus(); } }else if((i%3)==1){ // System.out.println("Kol="+i); if(i==(jTable.getColumnCount()-2)){ jTable.changeSelection(jTable.getSelectedRow(), (jTable.getColumnCount()-1), false, false); jTable.requestFocus(); }else{ jTable.changeSelection(0, i+3, false, false); jTable.requestFocus(); } }else if((i%3)==2){ // System.out.println("Kol="+i); // System.out.println("jTable.getColumnCount()="+jTable.getColumnCount()); if(i==(jTable.getColumnCount()-1)){ jTable.changeSelection(jTable.getSelectedRow(), 1, false, false); // System.out.println("Kol="+i); // jTable.requestFocus(); }else{ jTable.changeSelection(0, i+2, false, false); // jTable.requestFocus(); } } }else{ int i = jTable.getSelectedColumn(); // System.out.println("col="+i); // System.out.println("row="+row); // System.out.println("wsego row="+jTable.getRowCount()); if(i%3==0){ // System.out.println("Kol="+i); if(i==(jTable.getColumnCount()-3)){ jTable.changeSelection(jTable.getSelectedRow(), (jTable.getColumnCount()-1), false, false); jTable.requestFocus(); }else{ jTable.changeSelection(jTable.getSelectedRow(), i+1, false, false); jTable.requestFocus(); } }else if((i%3)==1){ // System.out.println("Kol="+i); if(i==(jTable.getColumnCount()-2)){ jTable.changeSelection(jTable.getSelectedRow(), (jTable.getColumnCount()-1), false, false); jTable.requestFocus(); }else{ jTable.changeSelection(jTable.getSelectedRow(), i, false, false); jTable.requestFocus(); } }else if((i%3)==2){ // System.out.println("Kol="+i); // System.out.println("jTable.getColumnCount()="+jTable.getColumnCount()); if(i==(jTable.getColumnCount()-1)){ jTable.changeSelection(jTable.getSelectedRow(), 1, false, false); // jTable.requestFocus(); }else{ jTable.changeSelection(jTable.getSelectedRow(), i+2, false, false); // jTable.requestFocus(); } } } } else if(e.getKeyCode() >= KeyEvent.VK_0 && e.getKeyCode() <= KeyEvent.VK_9 || e.getKeyCode() >= KeyEvent.VK_NUMPAD0 && e.getKeyCode() <= KeyEvent.VK_NUMPAD9) { // javax.swing.text.jTextComponent int ColumnSel=0; if((jTable.getSelectedColumn()%3)==2){ ColumnSel=jTable.getSelectedColumn(); }else if((jTable.getSelectedColumn()%3)==0){ ColumnSel=jTable.getSelectedColumn()+2; }else if((jTable.getSelectedColumn()%3)==1){ ColumnSel=jTable.getSelectedColumn()+1; } if(!jTable.isEditing() && !e.isActionKey() && !e.isControlDown() && !e.isAltDown() && e.getKeyCode()!=KeyEvent.VK_SHIFT) { int rowIndexStart = jTable.getSelectedRow(); int colIndexStart =jTable. getSelectedColumn(); if(rowIndexStart==-1 || colIndexStart==-1) return; jTable.editCellAt(rowIndexStart,ColumnSel); Component c = jTable.getEditorComponent(); if(c instanceof JTextComponent) ((JTextComponent)c).setText(""); } } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { int row=jTable.getSelectedRow(); if((row+1==jTable.getRowCount()) && jTable.getColumnCount()>=jTable.getSelectedColumn()+3){ int col=jTable.getSelectedColumn(); jTable.clearSelection(); jTable.changeSelection(0,col+3,false,false); // System.out.println("jTable.getSelectedRow()="+jTable.getSelectedRow()); // System.out.println("jTable.getSelectedColumn()="+jTable.getSelectedColumn()); jTable.requestFocus(); // ObnovCena(jTable,0,0); } } else if (e.getKeyCode() == KeyEvent.VK_UP) { int row=jTable.getSelectedRow(); if((row==0) && jTable.getSelectedColumn()>=3){ int col=jTable.getSelectedColumn(); jTable.clearSelection(); jTable.changeSelection(jTable.getRowCount()-1,col-3, false,false); // System.out.println("jTable.getSelectedRow()="+jTable.getSelectedRow()); jTable.requestFocus(); // ObnovCena(jTable,jTable.getRowCount()-1,col-3); // jTable.changeSelection(jTable.getRowCount()-1,col-3, false, false); } } }
public void keyReleased(java.awt.event.KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_TAB){ if(jTabbedPane1.getSelectedIndex()+1!=jTabbedPane1.getTabCount()){ jTabbedPane1.setSelectedIndex(jTabbedPane1.getSelectedIndex()+1); jTabbedPane1.requestFocus(); }else{ jTabbedPane1.setSelectedIndex(0); jTabbedPane1.requestFocus(); } } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { ObnovCena(jTable,jTable.getSelectedRow(),jTable.getSelectedColumn()); }else if (e.getKeyCode() == KeyEvent.VK_UP) { ObnovCena(jTable,jTable.getSelectedRow(),jTable.getSelectedColumn()); }else if (e.getKeyCode() == KeyEvent.VK_ENTER) { ObnovCena(jTable,jTable.getSelectedRow(),jTable.getSelectedColumn()); } } }); jTable.addFocusListener(new java.awt.event.FocusListener() { public void focusGained(java.awt.event.FocusEvent e) { // System.out.println("FocusGained!! "); if(jTable.getSelectedRow()<0){ return; } ObnovCena(jTable,jTable.getSelectedRow(),jTable.getSelectedColumn()); } public void focusLost(java.awt.event.FocusEvent e) { } }); for(int i=0;i<jTable.getColumnCount();i++){ TableColumn col = jTable.getColumnModel().getColumn(i); // System.out.println("Kol="+i); if(i%3==0){ // System.out.println("Kol="+i); int width = 550; col.setPreferredWidth(width); }else if((i%3)==1){ // System.out.println("Kol="+i); int width = 150; col.setPreferredWidth(width); }else if((i%3)==2){ // System.out.println("Kol="+i); int width = 150; col.setPreferredWidth(width); } } } return jTable; }
|
Вот полный код jTable. у меня сложная таблица. данные идут не в вниз, а в право. ну и соответственно некот. кнопки перенастраиваю... Это сообщение отредактировал(а) skif18 - 8.12.2009, 18:36
|