Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2005

Tip: Looking for answers? Try searching our database.

give focus to a comp?onent (JTabbedPane or other):how?

Thread view: 
_mario lat - 09 Mar 2005 12:52 GMT
I have 3 Jpanel in a JTabbedPane
I'd like to set focus (to see) the Jpanel2 when I click on a button in
Jpanel3.
How can I do?

I try with:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
       jPanel2.requestFocus();
       jPanel2.grabFocus();
       jPanel2.transferFocus();
       jPanel2.requestFocus();
       jPanel2.getFocusCycleRootAncestor();
       System.out.println("bottone cliccato");

   }

But it don't works.
Thank you in advance,
Mario.

============== code ===========================

public class principale extends javax.swing.JFrame {

   /** Creates new form principale */
   public principale() {
       initComponents();
   }

   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   private void initComponents() {
       jTabbedPane1 = new javax.swing.JTabbedPane();
       jPanel1 = new javax.swing.JPanel();
       jPanel2 = new javax.swing.JPanel();
       OK = new javax.swing.JLabel();
       jPanel3 = new javax.swing.JPanel();
       jButton1 = new javax.swing.JButton();

       setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
       jTabbedPane1.addMouseListener(new java.awt.event.MouseAdapter() {
           public void mouseClicked(java.awt.event.MouseEvent evt) {
               jTabbedPane1MouseClicked(evt);
           }
       });

       jTabbedPane1.addTab("tab1", jPanel1);

       OK.setText("OK");
       jPanel2.add(OK);

       jTabbedPane1.addTab("tab2", jPanel2);

       jButton1.setText("jButton1");
       jButton1.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               jButton1ActionPerformed(evt);
           }
       });

       jPanel3.add(jButton1);

       jTabbedPane1.addTab("tab3", jPanel3);

       getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

       pack();
   }

   private void jTabbedPane1MouseClicked(java.awt.event.MouseEvent evt) {
       // TODO add your handling code here:
   }

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
       // TODO add your handling code here:
       jPanel2.requestFocus();
       jPanel2.grabFocus();
       jPanel2.transferFocus();
       jPanel2.requestFocus();
       jPanel2.getFocusCycleRootAncestor();
       System.out.println("bottone cliccato");

   }

   /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new principale().setVisible(true);
//            }
//        });
//    }

   // Variables declaration - do not modify
   private javax.swing.JLabel OK;
   private javax.swing.JButton jButton1;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JPanel jPanel2;
   private javax.swing.JPanel jPanel3;
   private javax.swing.JTabbedPane jTabbedPane1;
   // End of variables declaration

}
Arnaud Berger - 09 Mar 2005 15:18 GMT
Hi,

What you want is not to change the focus (i.e the component which actually
is selected), but rather you want
to display another tab.

Just put :
jTabbedPane1.setSelectedComponent(jPanel2);

in your "actionPerformed" method.

Regards,

Arnaud

> I have 3 Jpanel in a JTabbedPane
> I'd like to set focus (to see) the Jpanel2 when I click on a button in
[quoted text clipped - 37 lines]
>         jPanel3 = new javax.swing.JPanel();
>         jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
>         jTabbedPane1.addMouseListener(new java.awt.event.MouseAdapter() {
>             public void mouseClicked(java.awt.event.MouseEvent evt) {
[quoted text clipped - 61 lines]
>
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.