Galera, estou com um problema ao capturar eventos de teclado.
Preciso navegar pela minha JTabbedPane com o ctrl + tab, por exemplo: o usuário mantém o ctrl pressionado e quando ele aperta o tab eu mudo a tab selecionada, já tentei muita coisa e não consegui.
Conforme você pode ver, Ctrl+Tab só funciona com JTabbedPane se o look & feel for Windows e se você estiver com um tab selecionado. Senão ele irá só mover o foco para o próximo controle (assim como ocorre em outros look & feels).
importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;classTester{publicstaticvoidmain(String[]args){finalJFramef=newJFrame();finalJTabbedPanetabbedPane=newJTabbedPane();finalJPanelpanel1=newJPanel(false),panel2=newJPanel(false),panel3=newJPanel(false),panel4=newJPanel(false),panel5=newJPanel(false),panel6=newJPanel(false),panel7=newJPanel(false),panel8=newJPanel(false),panel9=newJPanel(false);// WindowListener for cosing progam.f.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventevt){evt.getWindow().dispose();System.exit(0);}});tabbedPane.add("Panel 1",panel1);tabbedPane.add("Panel 2",panel2);tabbedPane.add("Panel 3",panel3);tabbedPane.add("Panel 4",panel4);tabbedPane.add("Panel 5",panel5);tabbedPane.add("Panel 6",panel6);tabbedPane.add("Panel 7",panel7);tabbedPane.add("Panel 8",panel8);tabbedPane.add("Panel 9",panel9);f.getContentPane().add(tabbedPane,BorderLayout.CENTER);//--------------------// The remaining code simply allows for a change in the LaF....//--------------------JPanelLaFPanel=newJPanel();finalJButtonwindowsLaF=newJButton("Windows"),motifLaF=newJButton("Motif"),metalLaF=newJButton("Metal");LaFPanel.add(windowsLaF);LaFPanel.add(motifLaF);LaFPanel.add(metalLaF);ActionListenerLaFListener=newActionListener(){publicvoidactionPerformed(ActionEventevt){Objectsrc=evt.getSource();try{if(src==windowsLaF)UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");elseif(src==motifLaF)UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");elseUIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");SwingUtilities.updateComponentTreeUI(f);}catch(Exceptione){System.err.println("*** ERROR IN CHANGING LAF: "+e);}}};windowsLaF.addActionListener(LaFListener);motifLaF.addActionListener(LaFListener);metalLaF.addActionListener(LaFListener);f.getContentPane().add(LaFPanel,BorderLayout.SOUTH);f.setResizable(false);f.setBounds(40,40,330,200);f.setVisible(true);}}
_Leon
Eu pensei capturar o evento ao pressionar o ctrl + tab e apenas acionar um myTabbedPane.setSelectedIndex(tabNumber)…
O maior problema é capturar o ctrl + tab, não estou conseguindo…
_Leon
Pronto fiz funcionar com qualquer look and feel
Da uma olhada:
importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;classTester{privatestaticbooleanctrlKey;privatestaticbooleantabKey;publicstaticvoidmain(String[]args){finalJFramef=newJFrame();finalJTabbedPanetabbedPane=newJTabbedPane();finalJPanelpanel1=newJPanel(false),panel2=newJPanel(false),panel3=newJPanel(false),panel4=newJPanel(false),panel5=newJPanel(false),panel6=newJPanel(false),panel7=newJPanel(false),panel8=newJPanel(false),panel9=newJPanel(false);// WindowListener for cosing progam.f.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventevt){evt.getWindow().dispose();System.exit(0);}});tabbedPane.add("Panel 1",panel1);tabbedPane.add("Panel 2",panel2);tabbedPane.add("Panel 3",panel3);tabbedPane.add("Panel 4",panel4);tabbedPane.add("Panel 5",panel5);tabbedPane.add("Panel 6",panel6);tabbedPane.add("Panel 7",panel7);tabbedPane.add("Panel 8",panel8);tabbedPane.add("Panel 9",panel9);f.getContentPane().add(tabbedPane,BorderLayout.CENTER);//--------------------// The remaining code simply allows for a change in the LaF....//--------------------JPanelLaFPanel=newJPanel();finalJButtonwindowsLaF=newJButton("Windows"),motifLaF=newJButton("Motif"),metalLaF=newJButton("Metal");LaFPanel.add(windowsLaF);LaFPanel.add(motifLaF);LaFPanel.add(metalLaF);ActionListenerLaFListener=newActionListener(){publicvoidactionPerformed(ActionEventevt){Objectsrc=evt.getSource();try{if(src==windowsLaF)UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");elseif(src==motifLaF)UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");elseUIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");SwingUtilities.updateComponentTreeUI(f);}catch(Exceptione){System.err.println("*** ERROR IN CHANGING LAF: "+e);}}};windowsLaF.addActionListener(LaFListener);motifLaF.addActionListener(LaFListener);metalLaF.addActionListener(LaFListener);f.getContentPane().add(LaFPanel,BorderLayout.SOUTH);f.setResizable(false);f.setBounds(40,40,330,200);f.setVisible(true);addTabControlListener(tabbedPane);}publicstaticvoidaddTabControlListener(finalJTabbedPaneparent){Toolkit.getDefaultToolkit().addAWTEventListener(newAWTEventListener(){publicvoideventDispatched(AWTEventevent){KeyEventke=(KeyEvent)event;if(ke.getID()==KeyEvent.KEY_RELEASED){if(ke.getKeyCode()==17){ctrlKey=false;parent.grabFocus();}elseif(ke.getKeyCode()==KeyEvent.VK_TAB){tabKey=false;parent.grabFocus();}}elseif(ke.getID()==KeyEvent.KEY_PRESSED){if(ke.getKeyCode()==17){ctrlKey=true;parent.grabFocus();}elseif(ke.getKeyCode()==KeyEvent.VK_TAB){tabKey=true;parent.grabFocus();if(ctrlKey){if(parent.getSelectedIndex()!=8){parent.setSelectedIndex(parent.getSelectedIndex()+1);parent.grabFocus();tabKey=false;}else{parent.setSelectedIndex(0);}}}}elseif(ke.getID()==KeyEvent.KEY_TYPED){if(ke.getKeyCode()==17){ctrlKey=true;parent.grabFocus();}elseif(ke.getKeyCode()==KeyEvent.VK_TAB){tabKey=true;parent.grabFocus();}}}},AWTEvent.KEY_EVENT_MASK);}}