We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

JTabbedPane and Jpanel

dkim18
dkim18 asked
on
Medium Priority
4,320 Views
Last Modified: 2013-11-23
hi!

Can anybody take a look this? (It is hard to explain w/o actual applet.)
http://webdev.apl.jhu.edu/~rbe/java/Homework4/index.html

If you drag up the bottom of window, the button disapears. My question is:
Is there two panels in this applet? if so, JTabbedPane is top of JPanel?  Or some others???

this is what I did so far.

++++++++++++++++++++++++++++++
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class TabbedPane
    extends JPanel implements ActionListener  {
  private JTabbedPane tabbedPane;
  private JPanel btmPanel;
  private JPanel axisSpacingPanel;
  private JPanel axisFormatPanel;
  private JPanel axisTitlesPanel;
  private JPanel sizePanel;
  private JPanel sinDataPanel;
  private JPanel cosDataPanel;
  private boolean wrappedTabs = true;
  Dimension component_dimen = new Dimension(300, 300);

  private JPanel buttonPanel;
  private JButton btn;
  private Container container;

  public TabbedPane() {

    super(new GridLayout(1, 1));
    setPreferredSize( component_dimen );
    setBackground( Color.gray );

    createAxisSpacing();
    createAxisFormat();
    createAxisTitles();
    createSize();
    createSinData();
    createCosData();

    tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Axis Spacing", axisSpacingPanel);
    tabbedPane.addTab("Axis Format", axisFormatPanel);
    tabbedPane.addTab("Axis Titles", axisTitlesPanel);
    tabbedPane.addTab("Size", sizePanel);
    tabbedPane.addTab("Sin Data", sinDataPanel);
    tabbedPane.addTab("Cos Data", cosDataPanel);

    //layout = new FlowLayout();
    //container = getContentPane();
    //container.setLayout(layout);
    //btn = new JButton("Verify");
    //buttonPanel = new JPanel();
    //buttonPanel.setLayout(new GridLayout(1, btn));
    //btn = new JButton("Verify");
    //buttonPanel.add(btn);
    //container.add(buttonPanel, BorderLayout.SOUTH);
    //container.add(btm);
    //layout.setAlignment(FlowLayout.b);
    //btmPanel = new JPanel();
    //btmPanel.add(container) ;
    //btmPanel.setPreferredSize(new Dimension(300, 400)) ;
    //Add the tabbed pane to this panel.
    add(tabbedPane);
  }

  void createAxisSpacing() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  void createAxisFormat() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  void createAxisTitles() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  void createSize() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  void createSinData() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  void createCosData() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  protected JComponent makeTextPanel(String text) {
    JPanel panel = new JPanel(false);
    JLabel filler = new JLabel(text);
    filler.setHorizontalAlignment(JLabel.CENTER);
    panel.setLayout(new GridLayout(1, 1));
    panel.add(filler);
    return panel;
  }

  public void actionPerformed(ActionEvent evt) {
      wrappedTabs = !wrappedTabs;
      //System.out.println("## Inside" );
      if (wrappedTabs) {
          tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
      } else {
          tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
      }
      tabbedPane.revalidate();
  }


  public static void main(String[] args) {

    final JFrame jf = new JFrame("Homework 4");
  TabbedPane hw = new TabbedPane();
  //Graph pp = new Graph( );
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  jf.setContentPane(hw);
  jf.pack();
  jf.setVisible(true);

  }
}
+++++++++++++++++++++++++++++++++++++++++++++++++++
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class TabbedPane
    extends JPanel implements ActionListener  {
  private JTabbedPane tabbedPane;
  private JPanel btmPanel;
  private JPanel axisSpacingPanel;
  private JPanel axisFormatPanel;
  private JPanel axisTitlesPanel;
  private JPanel sizePanel;
  private JPanel sinDataPanel;
  private JPanel cosDataPanel;
  private boolean wrappedTabs = true;
  Dimension component_dimen = new Dimension(300, 300);

  private JPanel buttonPanel;
  private JButton btn;
  private Container container;

  public TabbedPane() {

    super(new GridLayout(1, 1));
    setPreferredSize( component_dimen );
    setBackground( Color.gray );

    createAxisSpacing();
    createAxisFormat();
    createAxisTitles();
    createSize();
    createSinData();
    createCosData();

    tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Axis Spacing", axisSpacingPanel);
    tabbedPane.addTab("Axis Format", axisFormatPanel);
    tabbedPane.addTab("Axis Titles", axisTitlesPanel);
    tabbedPane.addTab("Size", sizePanel);
    tabbedPane.addTab("Sin Data", sinDataPanel);
    tabbedPane.addTab("Cos Data", cosDataPanel);

    //layout = new FlowLayout();
    //container = getContentPane();
    //container.setLayout(layout);
    //btn = new JButton("Verify");
    //buttonPanel = new JPanel();
    //buttonPanel.setLayout(new GridLayout(1, btn));
    //btn = new JButton("Verify");
    //buttonPanel.add(btn);
    //container.add(buttonPanel, BorderLayout.SOUTH);
    //container.add(btm);
    //layout.setAlignment(FlowLayout.b);
    //btmPanel = new JPanel();
    //btmPanel.add(container) ;
    //btmPanel.setPreferredSize(new Dimension(300, 400)) ;
    //Add the tabbed pane to this panel.
    add(tabbedPane);
  }

  void createAxisSpacing() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  void createAxisFormat() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  void createAxisTitles() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  void createSize() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  void createSinData() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  void createCosData() {
    JComponent panel1 = makeTextPanel("Panel #1");

  }

  protected JComponent makeTextPanel(String text) {
    JPanel panel = new JPanel(false);
    JLabel filler = new JLabel(text);
    filler.setHorizontalAlignment(JLabel.CENTER);
    panel.setLayout(new GridLayout(1, 1));
    panel.add(filler);
    return panel;
  }

  public void actionPerformed(ActionEvent evt) {
      wrappedTabs = !wrappedTabs;
      //System.out.println("## Inside" );
      if (wrappedTabs) {
          tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
      } else {
          tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
      }
      tabbedPane.revalidate();
  }


  public static void main(String[] args) {

    final JFrame jf = new JFrame("Homework 4");
  TabbedPane hw = new TabbedPane();
  //Graph pp = new Graph( );
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  jf.setContentPane(hw);
  jf.pack();
  jf.setVisible(true);

  }
}

Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Is there two panels in this applet? if so, JTabbedPane is top of JPanel?  Or some others???

There are lots of panels

That bottom panel is disappearing because there is no space to display it.

Author

Commented:
>There are lots of panels
you mean a lot of JTabbedpanes?

>That bottom panel is disappearing because there is no space to display it.
what do you mean by "no space to display it"?

So, six JTabbedpanes are top of JPanel?

Can you explain the anatomy of this applet?

Thanks,
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
its a bit confusing as the code you have posted is not the same as the link.
Which are we discussing here?
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
If you don't want the Verify button to disappear,
then have a panel with borderlayout with in the center the tabbedpane
and in the south the button.

Author

Commented:
objects,

the code I posted is what I did so far, but I am trying to figure out make it as same as the applet on the link site.

zzynx,
I am trying to make exactly the same applet as the link, but I am wondering how...
what i did is so far is that I placed 6 JTabbedPane on the JPanel, but I don't know how to put "verify" button out side of JTabbedPane. It looks like there are more than one Jpanel or something else...
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Sr. Software engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
zzynx,

Sorry, I meant JTabbedPane with 6 tabs. I followed your solution, but nothing appeared on the applet.
and here is my code.
+++++++++++++++++++++++++++++++++++++++++++++++++++
public class TabbedPane
    extends JPanel implements ActionListener  {
  private JTabbedPane tabbedPane;
  private JPanel btmPanel;
  private JPanel axisSpacingPanel;
  private JPanel axisFormatPanel;
  private JPanel axisTitlesPanel;
  private JPanel sizePanel;
  private JPanel sinDataPanel;
  private JPanel cosDataPanel;
  private boolean wrappedTabs = true;
  Dimension component_dimen = new Dimension(300, 300);

  private JButton verifyBtn;
  private Container container;

  public TabbedPane() {

    super(new GridLayout(1, 1));
    setPreferredSize( component_dimen );
    setBackground( Color.gray );

    tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Axis Spacing", axisSpacingPanel);
    tabbedPane.addTab("Axis Format", axisFormatPanel);
    tabbedPane.addTab("Axis Titles", axisTitlesPanel);
    tabbedPane.addTab("Size", sizePanel);
    tabbedPane.addTab("Sin Data", sinDataPanel);
    tabbedPane.addTab("Cos Data", cosDataPanel);

    setLayout(new BorderLayout());
    add(tabbedPane, BorderLayout.CENTER);
    JPanel southPanel = new JPanel();
    southPanel.add( verifyBtn );
    add(southPanel, BorderLayout.SOUTH);

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

public class HW4 extends       JApplet {//implements ActionListener  {//{// implements ActionListener {
  private JDesktopPane theDesktopPane;
      private            JTabbedPane tabbedPane;

    public void init() {

      setBackground(Color.gray);
      theDesktopPane = new JDesktopPane();
      getContentPane().add(theDesktopPane, BorderLayout.CENTER);

      JInternalFrame frame = new JInternalFrame("Homework 4", true, true, true, true);
      theDesktopPane.add(frame);

      final TabbedPane tabbedPane = new TabbedPane();

      Container container = frame.getContentPane();
      container.add(tabbedPane, BorderLayout.CENTER);
      //this.validate();
      //frame.getContentPane().add(tabbedPane);
      frame.pack();

      frame.setVisible(true);
      setSize(400, 400);
      setVisible(true);

    }
}
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
I think it's something with (the order in) your init() function.
Can you try the following:

public class HW4 extends JApplet {

     private JDesktopPane theDesktopPane;
     private          JTabbedPane tabbedPane;

    public void init() {

      setBackground(Color.gray);
      theDesktopPane = new JDesktopPane();
      getContentPane().add(theDesktopPane, BorderLayout.CENTER);

      JInternalFrame frame = new JInternalFrame("Homework 4", true, true, true, true);
      final TabbedPane tabbedPane = new TabbedPane();
      Container container = frame.getContentPane();
      container.add(tabbedPane, BorderLayout.CENTER);

      frame.pack(); // call this *after* all components are added
      theDesktopPane.add(frame);
      frame.show();

      setSize(400, 400);
    }
}

Author

Commented:
I tried your code, but still same problem...
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Mmmm..

When I make it an application instead of an applet it works OK:

public class HW4 extends JFrame {

     private JDesktopPane theDesktopPane;
     private JTabbedPane tabbedPane;

     public HW4() {

      setBackground(Color.gray);
      theDesktopPane = new JDesktopPane();
      getContentPane().add(theDesktopPane, BorderLayout.CENTER);

      JInternalFrame frame = new JInternalFrame("Homework 4", true, true, true, true);
      final TabbedPane tabbedPane = new TabbedPane();
      Container container = frame.getContentPane();
      container.add(tabbedPane, BorderLayout.CENTER);

      frame.pack(); // call this *after* all components are added
      theDesktopPane.add(frame);
      frame.show();

      setSize(400, 400);
    }
     
    public static void main(String args[]){
        HW4 hw = new HW4();
        hw.show();
    }
   
}

Should it be an applet?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you never set the size of the internal frame.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
objects, you mean frame.setSize() or even setPreferredSize()?

Is that needed?
He does a frame.pack();

Author

Commented:
I missed verifyBtn = new JButton("Verify");
Sorry for the mistake....

Now, the tricky part is When I drag up the bottom of your hw4 applet on the above link, verify button panel slide under the tabbedPane. How do I do that?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> He does a frame.pack();

sorry, didn't see the pack() call.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> When I drag up the bottom of your hw4 applet on the above link,
>> verify button panel slide under the tabbedPane. How do I do that?
I can't imagine that would be the wanted behaviour. Is it?
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Thanks for accepting
That keeps us going.
:)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.