Hi,
I have a class that extends JFrame and displays a tabbedpane object. Each pane contained in the tabbedpane is its own jpanel class and the jpanels do not have access to the tabbedpane object. Therefore, I cannot use the tabbedpane.setSelectedComponet(panel). However the panel that wants to instigate a focus change does have access to the panel object that has to be brought in focus.

I there a way shift focus programmatically or should start thinking of a different design? Thanks in advance.

Why would the panel need to request the tab change?

A list is displayed in one panel. Once user selects an item from the list and clicks a button the details are displayed in another panel.

Well, there are a few ways to do what you want to do.

First, allow each panel to know about the tabbed pane.

Second, have the event from the list box be recieved by something that does know about the tabbed pane.

BUT, typically tabbed menus are driven by the user clicking on the tab itself. I think it would be considered strange behavior for a listbox to cause a different tabbed pane to be selected. Are you able to put the information below the listbox on the one panel? I would think that would be more logical/appropriate behavior.

Thanks for your prompt response. Kindly elaborate. How can each panel know about the tabbed pane? The tabbed pane contains the individual panels. Therefore, the individual panels cannot contain the tabbed pane. If box1(tabbedpane) contains box2(panel), box2 cannot contain box1. Right?

I am having the same logic problem with your second idea where I call on an object that contains the tabbed pane. Or maybe I am not understanding what you are trying to say. Please elaborate.

Well, letting the JPanel know about the Tabbed Pane is easy.

Subclass JPanel and name is something like JTabbedPanePanel.

public class JTabbedPanePanel extends JPanel
{
  private JTabbedPane aJTabbedPane = null;

  public JTabbedPanePanel()
  {
    super();
  }

  public void setJTabbedPane(JTabbedPane jTabbedPane)
  {
    aJTabbedPane = jTabbedPane;
  }
}

Then at some point you have to be handling the event from the List Box, via a ListSelectionEvent, right? My guess is that the panel itself is listening for that event and then is handeling the event. Then in this case the panel then knows about the event and knows about JTabbedPane and life is good.


Second example. With components like the JList I always make them Listeners of themselves.

public class ExtendedJList extends JList implements ListSelectionListener
{
  // Add all the constructors here... each one calling init()
  public ExtendedJList()
  {
    init();
  }

  private void init()
  {
    addListSecionListener(this);
  }

  // Implement the method from ListSelectionListener
  public void valueChanged(ListSelectionEvent e)
  {
    System.out.println("OVERRIDE THIS BEHAVIOR WITH AN ANONYMOUS INNER CLASS!");
  }
}

Then when we add this ExtendedJList to a panel we do the following...

ExtendedJList extendedJList = new ExtendedJList()
{
  public void valueChanged(ListSelectionEvent e)
  {
    // 1. Get the JTabbedPane some how...
    // 2. Set the assigned tab number on the tabbed pane.
  }
}

So at some point the JTabbedPane has to be known. You could use a sington object for the Main JPanel of your application and then have a setter and getter for your JTabbedPane. All these solutions will work.

BUT, again, i would urge you not do do the behavior of having one event on one JPanel to cause the JTabbedPane to change selected tabs. Have the information from the selection on the JList to appear below the JList. The end user will most likely be happier.

Yes, when the individual panels know about the tabbedpane life truely is good. If you are wondering why I need to traverse tabbedpanes programmatically you can take look at the attached pictures. When the user selects an item from the lists and clicks view, the item is diplayed on a different panel. Just for ease, I wanted to the program to bring that different panel into focus so the user wouldn't have to do it manually.


While we are at it do you think you can tell me how to tame the panels? In the Add/Edit panel I would like to shrink the width of the checkbox List. Do have any recommendation on how to do that?

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

public class AddEditPane extends JPanel
{
    private JLabel lb_desig;
    private JLabel lb_details;
    private JTextField t_desig;
    private JTextArea ta_details;
    private JList ls_group;
   
    private JButton b_clear;
    private JButton b_edit;
    private JButton b_save;
    private JButton b_erase;
      
    
    public AddEditPane()
    {
        setLayout(new GridBagLayout());                
        GridBagConstraints gbc = new GridBagConstraints();
        Insets ins = new Insets(2, 2, 2, 2);
        
        
        
        lb_desig = new JLabel("Designation:");
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.insets = ins;
        gbc.anchor = GridBagConstraints.NORTHEAST;
        add(lb_desig, gbc);
        
        
        lb_details = new JLabel("Details:");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.insets = ins;        
        gbc.anchor = GridBagConstraints.NORTHEAST;
        add(lb_details,gbc);
        
        t_desig = new JTextField(20);
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.insets = ins;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        add(t_desig, gbc);
        
        ta_details = new JTextArea(8, 20);
        ta_details.setLineWrap(true);
        ta_details.setWrapStyleWord(true);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.insets = ins;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        JScrollPane detScrPane = new JScrollPane(ta_details);
        detScrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(detScrPane, gbc);
        
            
        b_clear = new JButton("Clear");
        b_edit = new JButton("Edit");
        b_save = new JButton("Save");
        b_erase = new JButton("Erase");
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout());
        buttonPanel.add(b_clear);
        buttonPanel.add(b_edit);
        buttonPanel.add(b_save);
        buttonPanel.add(b_erase);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = ins;
        add(buttonPanel, gbc);
        
        //Sets up the CheckBox List
        ls_group = new JList();
        JScrollPane grScrPane = new JScrollPane(ls_group,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        gbc.weightx = .1;
        gbc.fill = GridBagConstraints.BOTH;
        add(grScrPane, gbc);
    }
    
    public JButton getClearB(){ return b_clear;}
    public JButton getEditB(){ return b_edit;}
    public JButton getSaveB(){ return b_save;}
    public JButton getEraseB(){ return b_erase;}
    public JList getGroupLs(){ return ls_group;}
    public JTextField getDesigT(){return t_desig;}
    public JTextArea getDetailsTA() {return ta_details;}   
    
    /**
     * 
     */
    public void setFields(String desig, String details)
    {
        t_desig.setText(desig);
        ta_details.setText(details);
    }
    
    /**
     * Clears all fields
     */
    public void clearAll()
    {
        t_desig.setText("");
        ta_details.setText("");
    }       
  
}

If can show me how manage this one panel I'm sure I can do the other panels myself. And I thank you very much for all your generosity.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.