Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4258
  • Last Modified:

JTabbedPane and Jpanel

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);

  }
}

0
dkim18
Asked:
dkim18
  • 7
  • 5
  • 5
2 Solutions
 
objectsCommented:
> 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.
0
 
dkim18Author 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,
0
 
objectsCommented:
its a bit confusing as the code you have posted is not the same as the link.
Which are we discussing here?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
zzynxSoftware engineerCommented:
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.
0
 
dkim18Author 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...
0
 
objectsCommented:
Use the approach suggested by zzynx, but instead of a button in the south, use a JPanel that contains the button.
0
 
zzynxSoftware engineerCommented:
>> what i did is so far is that I placed 6 JTabbedPane on the JPanel
Hey, that's not how you use a JTabbedPane!
You should use only *one* JTabbedPane containing 6 panels.

JTabbedPane theTabbedPane = new JTabbedPane();
theTabbedPane.addTab("Tab 1 text", myPanel1);
theTabbedPane.addTab("Tab 2 text", myPanel2);
theTabbedPane.addTab("Tab 3 text", myPanel3);
theTabbedPane.addTab("Tab 4 text", myPanel4);
theTabbedPane.addTab("Tab 5 text", myPanel5);
theTabbedPane.addTab("Tab 6 text", myPanel6);

So, let's repeat:
1) Give your main panel the BorderLayout
         mainPanel.setLayout(new BorderLayout());
2) Add the JTabbedPane (containing the six tab-panels) in the center:
         mainPanel.add(theTabbedPane, BorderLayout.CENTER);
3) Create a new Panel (e.g. "southPanel") and add the verifyButton to that panel:

      JPanel southPanel = new JPanel();
      southPanel.add( verifyButton );

4) Add that southPanel to your main panel in the south
        mainPanel.add(southPanel, BorderLayout.sOUTH);

Success.
0
 
dkim18Author 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);

    }
}
0
 
zzynxSoftware engineerCommented:
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);
    }
}
0
 
dkim18Author Commented:
I tried your code, but still same problem...
0
 
zzynxSoftware engineerCommented:
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?
0
 
objectsCommented:
you never set the size of the internal frame.
0
 
zzynxSoftware engineerCommented:
objects, you mean frame.setSize() or even setPreferredSize()?

Is that needed?
He does a frame.pack();
0
 
dkim18Author 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?
0
 
objectsCommented:
> He does a frame.pack();

sorry, didn't see the pack() call.
0
 
zzynxSoftware engineerCommented:
>> 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?
0
 
zzynxSoftware engineerCommented:
Thanks for accepting
That keeps us going.
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now