Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Add KeyListener to tabbedPane(Tab)

Add KeyListener to tabbedPane(Tab)

Scheduled Pinned Locked Moved Java
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chdboy
    wrote on last edited by
    #1

    I want a key Listener on a tabbed pane (Tab) so when I click on a tab I can do something. I tried

    public class Mainframe extends JFrame implements ActionListener,KeyListener
    {
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addKeyListener(this);
    tabbedPane.addTab("Tab 1 ",panel1);
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
    tabbedPane.addTab("New Tab", panel2);
    tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
    @Override
    public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

    }
    
    @Override
    public void keyPressed(KeyEvent e)
    {
    	// TODO Auto-generated method stub
    	if(e.getKeyCode() == KeyEvent.VK\_1)
    		JOptionPane.showMessageDialog(null, "You have selected ","Tab Clicked",JOptionPane.PLAIN\_MESSAGE);
    }
    
    @Override
    public void keyReleased(KeyEvent e) {
    	// TODO Auto-generated method stub
    	
    }
    

    }

    It does nothing.

    L 1 Reply Last reply
    0
    • C chdboy

      I want a key Listener on a tabbed pane (Tab) so when I click on a tab I can do something. I tried

      public class Mainframe extends JFrame implements ActionListener,KeyListener
      {
      JTabbedPane tabbedPane = new JTabbedPane();
      tabbedPane.addKeyListener(this);
      tabbedPane.addTab("Tab 1 ",panel1);
      tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
      tabbedPane.addTab("New Tab", panel2);
      tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
      @Override
      public void keyTyped(KeyEvent e) {
      // TODO Auto-generated method stub

      }
      
      @Override
      public void keyPressed(KeyEvent e)
      {
      	// TODO Auto-generated method stub
      	if(e.getKeyCode() == KeyEvent.VK\_1)
      		JOptionPane.showMessageDialog(null, "You have selected ","Tab Clicked",JOptionPane.PLAIN\_MESSAGE);
      }
      
      @Override
      public void keyReleased(KeyEvent e) {
      	// TODO Auto-generated method stub
      	
      }
      

      }

      It does nothing.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Does http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html[^] help to explain how tabs operate?

      Veni, vidi, abiit domum

      C 1 Reply Last reply
      0
      • L Lost User

        Does http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html[^] help to explain how tabs operate?

        Veni, vidi, abiit domum

        C Offline
        C Offline
        chdboy
        wrote on last edited by
        #3

        Change Listener works fine .

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups