Solved

JTabbedPane and Jpanel

Posted on 2004-03-25
17
4,203 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);

  }
}

0
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 5
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10684541
> 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
 

Author Comment

by:dkim18
ID: 10684594
>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
 
LVL 92

Expert Comment

by:objects
ID: 10684605
its a bit confusing as the code you have posted is not the same as the link.
Which are we discussing here?
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 37

Expert Comment

by:zzynx
ID: 10684912
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
 

Author Comment

by:dkim18
ID: 10689124
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
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 10690602
Use the approach suggested by zzynx, but instead of a button in the south, use a JPanel that contains the button.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 150 total points
ID: 10694967
>> 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
 

Author Comment

by:dkim18
ID: 10697482
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10702047
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
 

Author Comment

by:dkim18
ID: 10702150
I tried your code, but still same problem...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10702345
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
 
LVL 92

Expert Comment

by:objects
ID: 10702728
you never set the size of the internal frame.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10703024
objects, you mean frame.setSize() or even setPreferredSize()?

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

Author Comment

by:dkim18
ID: 10708975
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
 
LVL 92

Expert Comment

by:objects
ID: 10709065
> He does a frame.pack();

sorry, didn't see the pack() call.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10711195
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 10711399
Thanks for accepting
That keeps us going.
:)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question