How do I traverse tabbedpanes programmatically?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2006
Posts: 138
Reputation: ryy705 is an unknown quantity at this point 
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

How do I traverse tabbedpanes programmatically?

 
0
  #1
Sep 4th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: How do I traverse tabbedpanes programmatically?

 
0
  #2
Sep 4th, 2006
Why would the panel need to request the tab change?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 138
Reputation: ryy705 is an unknown quantity at this point 
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

Re: How do I traverse tabbedpanes programmatically?

 
0
  #3
Sep 4th, 2006
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.
Last edited by ryy705; Sep 4th, 2006 at 11:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: How do I traverse tabbedpanes programmatically?

 
0
  #4
Sep 5th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 138
Reputation: ryy705 is an unknown quantity at this point 
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

Re: How do I traverse tabbedpanes programmatically?

 
0
  #5
Sep 5th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: How do I traverse tabbedpanes programmatically?

 
0
  #6
Sep 5th, 2006
Well, letting the JPanel know about the Tabbed Pane is easy.

Subclass JPanel and name is something like JTabbedPanePanel.

  1. public class JTabbedPanePanel extends JPanel
  2. {
  3. private JTabbedPane aJTabbedPane = null;
  4.  
  5. public JTabbedPanePanel()
  6. {
  7. super();
  8. }
  9.  
  10. public void setJTabbedPane(JTabbedPane jTabbedPane)
  11. {
  12. aJTabbedPane = jTabbedPane;
  13. }
  14. }

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.

  1. public class ExtendedJList extends JList implements ListSelectionListener
  2. {
  3. // Add all the constructors here... each one calling init()
  4. public ExtendedJList()
  5. {
  6. init();
  7. }
  8.  
  9. private void init()
  10. {
  11. addListSecionListener(this);
  12. }
  13.  
  14. // Implement the method from ListSelectionListener
  15. public void valueChanged(ListSelectionEvent e)
  16. {
  17. System.out.println("OVERRIDE THIS BEHAVIOR WITH AN ANONYMOUS INNER CLASS!");
  18. }
  19. }

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

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

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 138
Reputation: ryy705 is an unknown quantity at this point 
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

Re: How do I traverse tabbedpanes programmatically?

 
0
  #7
Sep 6th, 2006
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?

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.*;
  4.  
  5. public class AddEditPane extends JPanel
  6. {
  7. private JLabel lb_desig;
  8. private JLabel lb_details;
  9. private JTextField t_desig;
  10. private JTextArea ta_details;
  11. private JList ls_group;
  12.  
  13. private JButton b_clear;
  14. private JButton b_edit;
  15. private JButton b_save;
  16. private JButton b_erase;
  17.  
  18.  
  19. public AddEditPane()
  20. {
  21. setLayout(new GridBagLayout());
  22. GridBagConstraints gbc = new GridBagConstraints();
  23. Insets ins = new Insets(2, 2, 2, 2);
  24.  
  25.  
  26.  
  27. lb_desig = new JLabel("Designation:");
  28. gbc.gridx = 0;
  29. gbc.gridy = 0;
  30. gbc.gridwidth = 1;
  31. gbc.gridheight = 1;
  32. gbc.insets = ins;
  33. gbc.anchor = GridBagConstraints.NORTHEAST;
  34. add(lb_desig, gbc);
  35.  
  36.  
  37. lb_details = new JLabel("Details:");
  38. gbc.gridx = 0;
  39. gbc.gridy = 1;
  40. gbc.gridwidth = 1;
  41. gbc.gridheight = 1;
  42. gbc.insets = ins;
  43. gbc.anchor = GridBagConstraints.NORTHEAST;
  44. add(lb_details,gbc);
  45.  
  46. t_desig = new JTextField(20);
  47. gbc.gridx = 1;
  48. gbc.gridy = 0;
  49. gbc.gridwidth = 1;
  50. gbc.gridheight = 1;
  51. gbc.insets = ins;
  52. gbc.fill = GridBagConstraints.BOTH;
  53. gbc.anchor = GridBagConstraints.NORTHWEST;
  54. add(t_desig, gbc);
  55.  
  56. ta_details = new JTextArea(8, 20);
  57. ta_details.setLineWrap(true);
  58. ta_details.setWrapStyleWord(true);
  59. gbc.gridx = 1;
  60. gbc.gridy = 1;
  61. gbc.gridwidth = 1;
  62. gbc.gridheight = 1;
  63. gbc.insets = ins;
  64. gbc.anchor = GridBagConstraints.NORTHWEST;
  65. JScrollPane detScrPane = new JScrollPane(ta_details);
  66. detScrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  67. add(detScrPane, gbc);
  68.  
  69.  
  70. b_clear = new JButton("Clear");
  71. b_edit = new JButton("Edit");
  72. b_save = new JButton("Save");
  73. b_erase = new JButton("Erase");
  74.  
  75. JPanel buttonPanel = new JPanel();
  76. buttonPanel.setLayout(new GridLayout());
  77. buttonPanel.add(b_clear);
  78. buttonPanel.add(b_edit);
  79. buttonPanel.add(b_save);
  80. buttonPanel.add(b_erase);
  81. gbc.gridx = 1;
  82. gbc.gridy = 2;
  83. gbc.insets = ins;
  84. add(buttonPanel, gbc);
  85.  
  86. //Sets up the CheckBox List
  87. ls_group = new JList();
  88. JScrollPane grScrPane = new JScrollPane(ls_group,
  89. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  90. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  91. gbc.gridx = 2;
  92. gbc.gridy = 0;
  93. gbc.gridwidth = 1;
  94. gbc.gridheight = 2;
  95. gbc.weightx = .1;
  96. gbc.fill = GridBagConstraints.BOTH;
  97. add(grScrPane, gbc);
  98. }
  99.  
  100. public JButton getClearB(){ return b_clear;}
  101. public JButton getEditB(){ return b_edit;}
  102. public JButton getSaveB(){ return b_save;}
  103. public JButton getEraseB(){ return b_erase;}
  104. public JList getGroupLs(){ return ls_group;}
  105. public JTextField getDesigT(){return t_desig;}
  106. public JTextArea getDetailsTA() {return ta_details;}
  107.  
  108. /**
  109.   *
  110.   */
  111. public void setFields(String desig, String details)
  112. {
  113. t_desig.setText(desig);
  114. ta_details.setText(details);
  115. }
  116.  
  117. /**
  118.   * Clears all fields
  119.   */
  120. public void clearAll()
  121. {
  122. t_desig.setText("");
  123. ta_details.setText("");
  124. }
  125.  
  126. }
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.
Attached Thumbnails
GroupPane.gif   ListPane.gif   AddEditPane.gif  
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2132 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC