-1

I have a simple java applet,but the panel is not appearing even after adding it,i have set the gridlayout for the panel and the the default layout for Jpanel.

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//<applet code="vote.class" width=400 height=200></applet>
public class vote extends JApplet implements ActionListener
{
JLabel l1,l2,l3;
JButton b1,b2,b3;
JPanel mp;
Panel p1,p2;

public void init()
{
this.setLayout(null);
mp=new JPanel();
//mp.setLayout(null);
Panel p1=new Panel();
p1.setLayout(null);
p1.setLayout(new GridLayout(3,3,5,5));

l1=new JLabel("test");
l2=new JLabel("test2");
l3=new JLabel("test2");
p1.add(l1);

p1.add(l2);
p1.add(l3);


mp.add(p1);
add(mp);
}
public void actionPerformed(ActionEvent AE)
{
}
}

The applet is running blank

EDIT: Modified the code the code now runs but im little confused how the layout manager works the gridlayout with values 3,3,5,5 says there should be 3 rows and 3 columns with 5padding.SO why is the applet like this

enter image description here

techno
  • 6,100
  • 16
  • 86
  • 192
  • 3
    Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Feb 24 '13 at 10:30
  • *" little confused how the layout manager works the gridlayout with values 3,3,5,5 says there should be 3 rows and 3 columns with 5padding."* 3x3 is 9 components, whereas you have only added 3 components. Try adding more. – Andrew Thompson Feb 24 '13 at 12:19
  • @AndrewThompson ok,but what if i want like this say i need 3 components in a row the added components should be in the same row until there are 3 in the same row then the next row should be filled,what type of layout should i use – techno Feb 24 '13 at 12:34
  • Fill them in the order required by the layout (for pities sake). Use some initiative! – Andrew Thompson Feb 24 '13 at 13:41

3 Answers3

4

As going trough your code

this.setLayout(null); // seems to be the problem comment it or provide mp with bounds

You should not do setLayout to null

Harmeet Singh
  • 2,555
  • 18
  • 21
4

Try this code instead. It uses layouts consistently, uses only Swing components, puts colors to the panels to make them more clear & has been seen to work.

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//<applet code="vote.class" width=400 height=200></applet>
public class vote extends JApplet
{
    JLabel l1,l2,l3;
    JButton b1,b2,b3;
    JPanel mp;
    // don't mix Swing with AWT components!
    JPanel p1,p2;

    public void init()
    {
        mp=new JPanel();
        mp.setBackground(Color.YELLOW);
        p1=new JPanel();
        p1.setBackground(Color.GREEN);
        p1.setLayout(new GridLayout(3,3,5,5));

        l1=new JLabel("test");
        l2=new JLabel("test2");
        l3=new JLabel("test2");
        p1.add(l1);
        p1.add(l2);
        p1.add(l3);


        mp.add(p1);
        add(mp);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • @HarmeetSingh: Also note Andrew's convenient _tag-in-source_ comment, illustrated [here](http://stackoverflow.com/a/7455066/230513). – trashgod Feb 24 '13 at 13:32
1

I just added flow layout and it works fine. the problem must bee null layout.

public void init()
{
this.setLayout(new FlowLayout());
mp=new JPanel();
...
Arpit
  • 12,767
  • 3
  • 27
  • 40