🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #722

Re: jTabbedPane focus problem

#722
From: Arthur Ore
Date: Tue, 04 Jan 2005 22:22
103 lines
4111 bytes
I'm using JBuilderX Ent too, (JDK 1.4.2_06-b03). If I've understood what you 
are asking, the following works for me. Click any of the buttons or on the 
tabs and the field I specified always seems to get focus. Hope this helps. I 
assume you know you can search for bugs at www.javasoft.com. There do seem 
to be some issues with JTabbedPanes. However, if you can narrow your code 
down to always get a reproducible error and you can't find a bug then you 
could post it here and someone may be able to help.

Arth

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Frame1
    extends JFrame
{
  JTabbedPane jTabbedPane1 = new JTabbedPane();
  JPanel jPanel2 = new JPanel();
  JPanel jPanel3 = new JPanel();
  JButton jButton1 = new JButton("jButton1");
  JButton jButton2 = new JButton("jButton1");
  JPanel jPanel1 = new JPanel();
  JTextField jTextField2 = new JTextField("jTextField2");
  JTextField jTextField1 = new JTextField("jTextField1");
  JTextField jTextField3 = new JTextField("jTextField3");
  public Frame1()
  {
    try
    {
      jbInit();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception
  {
    this.setSize(new Dimension(400, 300));
    jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener()
    {
      public void stateChanged(ChangeEvent e)
      {
        jTabbedPane1_stateChanged(e);
      }
    });
    jButton1.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        jButton1_actionPerformed(e);
      }
    });
    jButton2.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        jButton1_actionPerformed(e);
      }
    });
    jButton2.setText("jButton2");
    this.getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
    jTabbedPane1.add(jPanel2, "jPanel2");
    jPanel2.add(jButton1, null);
    jTabbedPane1.add(jPanel3, "jPanel3");
    jPanel3.add(jButton2, null);
    jTabbedPane1.add(jPanel1, "jPanel1");
    jPanel1.add(jTextField1, null);
    jPanel1.add(jTextField2, null);
    jPanel1.add(jTextField3, null);
  }

  public static void main(String[] args)
  {

    Frame1 frame = new Frame1();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    frame.setLocation( (screenSize.width - frameSize.width) / 2, 
(screenSize.height - frameSize.height) / 2);
    frame.setVisible(true);
  }

  void jTabbedPane1_stateChanged(ChangeEvent e)
  {
    if (jTabbedPane1.getSelectedIndex() == 2)
    {
      //this.validate(); works without validate here
      jTextField2.requestFocus();
    }
  }

  void jButton1_actionPerformed(ActionEvent e)
  {
    jTabbedPane1.setSelectedIndex(2);
    this.validate();  // validate needed otherwise component doesn't retain 
focus
    jTextField2.requestFocus();
  }

} 
Technical Details
Message-ID: <41db1740@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <41D99FA3.7060700@johntdow.com> <41d9ade3@newsgroups.borland.com> <41DA069C.3060608@johntdow.com> <41da51e6$1@newsgroups.borland.com> <41DAFE36.2000900@johntdow.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,a0ad10712e673590
X-Google-Attributes: gid111d45,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Arthur Ore" <nospamthx@nospamthx.co.uk>
Newsgroups: borland.public.jbuilder.java.swing
References: <41D99FA3.7060700@johntdow.com> <41d9ade3@newsgroups.borland.com> <41DA069C.3060608@johntdow.com> <41da51e6$1@newsgroups.borland.com> <41DAFE36.2000900@johntdow.com>
Subject: Re: jTabbedPane focus problem
Date: Tue, 4 Jan 2005 22:22:38 -0000
Lines: 105
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-RFC2646: Format=Flowed; Response
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
NNTP-Posting-Host: 82.69.65.131
Message-ID: <41db1740@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1104877376 82.69.65.131 (4 Jan 2005 14:22:56 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail