We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,183 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Question about JPanel/JApplet/BoxLayout

Hello,

When I run the following program, the applet window displays nothing. I will be grateful for any help.

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

public class BoxPanel extends JPanel
{
   private JTextField label1, label2;
      
      
   public BoxPanel()
   {
         setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
         label1 = new JTextField(20);
         label2 = new JTextField(20);
         add(label1);
         add(label2);
    }
    
    
    
}
import javax.swing.*;
import java.awt.*;
public class App extends JApplet
{
    private JLabel label1, label2;
    private BoxPanel panelA, panelB;
    
    public void init()
    {
        panelA = new BoxPanel();
        Container c = getContentPane();
        
        c.add(panelA);
             
        
    }
    
}

Thank you!

3
Contributors
2
Replies
21 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
javaprog200
Light Poster
46 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I can't see anywhere that you have constructed any frame or panel in which to add to the frame to then set it frame visible, you can also use a Container...

e.g.

JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
p.add(label1);         
p.add(label2);

I would suggest you look at the BoxLayout or JFrames/Panels tutorials so you can see how a basic class is put together as a whole. Oracle uses a container or you can construct a frame and panel to then add the panel to it. Links provided below:

Box Layout Examplehttp://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BoxLayoutDemoProject/src/layout/BoxLayoutDemo.java

Box Layouts http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

Panels http://docs.oracle.com/javase/tutorial/uiswing/components/panel.html#creating

Frames: http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

I would also make the lables public as you want to use them in a public method.

Hope that helps

StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
Skill Endorsements: 0

It works for me. I see a vertical dividing line between the two JTextFields.

Try typing something into the text fields.
Try putting a String into them in the constructor.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0810 seconds using 2.71MB