6 Replies - 345 Views - Last Post: 09 October 2012 - 09:35 PM Rate Topic: -----

#1 summer_breeze  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 27-July 12

Help with Layout Managers

Posted 09 October 2012 - 08:49 PM

The program doesn't show pane2. What could be the problem?
Here's the part of the program:

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

public class Test extends JFrame implements ActionListener
{
	static String[] yourChoicesItems =
	                         {"Blueberry Muffin        1.45",
	                          "Strawberry Bagel        0.80",
	                          "Lite Yogurt             0.75",
	                          "Vannila Ice Cream       2.75",
	                          "Hash Browns             2.50",
	                          "Toast                   2.00",
	                          "French Fries            1.50",
	                          "Onion Soup              3.00",
	                          "Coffee                  0.90",
	                          "Iced Tea                1.00",
	                          "Hot Chocolate           1.75"};
    static double[] yourChoicesPrices = {1.45, 0.80, 0.75, 2.75,
                                         2.50, 2.00, 1.50, 3.00,
                                         0.90, 1.00, 1.75};
    private JList<String> yourChoices;
    private JTextArea bill;
    private JFrame myFrame;
    private JPanel pane1;
    private JPanel pane2;

public Test()
{
	super("Welcome to Java Kiosk");
	myFrame.setLayout(new GridLayout(2,1));
        myFrame.setBackground(new Color(0, 200, 200));
        pane1.setLayout(new BorderLayout(5, 5));
        pane2.setLayout(new GridLayout(2,1));

	JLabel yourChoicesJLabel = new JLabel("MENU");
	pane1.add(yourChoicesJLabel, BorderLayout.NORTH);
	yourChoicesJLabel.setFont(new Font("Dialog",Font.BOLD,20));

	yourChoices = new JList<>(yourChoicesItems);
	pane1.add(new JScrollPane(yourChoices), BorderLayout.WEST);
	yourChoices.setFont(new Font("Courier",Font.BOLD+Font.ITALIC,14));

	bill = new JTextArea();
	pane1.add(bill, BorderLayout.EAST);
	bill.setFont(new Font("Courier",Font.PLAIN,12));

	JButton button1 = new JButton("Selection Completed");
	pane1.add(button1, BorderLayout.SOUTH);
	button1.addActionListener(this);

    JButton button2 = new JButton("Add");
    pane2.add(button2);
    button2.addActionListener(this);

    JButton button3 = new JButton("Delete");
    pane2.add(button3);
    button3.addActionListener(this);

    myFrame.add(pane1);
    myFrame.add(pane2);
	setSize(500, 360);
	setVisible(true);
	setDefaultCloseOperation(EXIT_ON_CLOSE);
}


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Layout Managers

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Help with Layout Managers

Posted 09 October 2012 - 08:58 PM

Well first of all. Why are you using 2 JFrames? :notify:

I think you are misunderstand the point of extending JFrame in your class. This makes your class a JFrame. There is no need to make a JFrame object in a JFrame class.

The reason this won't work is because you are adding your panels to a frame that is never told to show up. You have one JFrame with setVisible() and what not, and one JFrame that is just holding other containers.
Was This Post Helpful? 1
  • +
  • -

#3 summer_breeze  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 27-July 12

Re: Help with Layout Managers

Posted 09 October 2012 - 09:06 PM

View PostKinaces, on 09 October 2012 - 08:58 PM, said:

Well first of all. Why are you using 2 JFrames? :notify:

I think you are misunderstand the point of extending JFrame in your class. This makes your class a JFrame. There is no need to make a JFrame object in a JFrame class.

The reason this won't work is because you are adding your panels to a frame that is never told to show up. You have one JFrame with setVisible() and what not, and one JFrame that is just holding other containers.

Yes, you're right. I'm quite confused. lol
I've removed myFrame, what to do next? Thank you!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Test extends JFrame implements ActionListener
{
	static String[] yourChoicesItems =
	                         {"Blueberry Muffin        1.45",
	                          "Strawberry Bagel        0.80",
	                          "Lite Yogurt             0.75",
	                          "Vannila Ice Cream       2.75",
	                          "Hash Browns             2.50",
	                          "Toast                   2.00",
	                          "French Fries            1.50",
	                          "Onion Soup              3.00",
	                          "Coffee                  0.90",
	                          "Iced Tea                1.00",
	                          "Hot Chocolate           1.75"};
    static double[] yourChoicesPrices = {1.45, 0.80, 0.75, 2.75,
                                         2.50, 2.00, 1.50, 3.00,
                                         0.90, 1.00, 1.75};
    private JList<String> yourChoices;
    private JTextArea bill;

    private JPanel pane1;
    private JPanel pane2;

public Test()
{
	super("Welcome to Java Kiosk");
    setBackground(new Color(0, 200, 200));
    pane1.setLayout(new BorderLayout(5, 5));
    pane2.setLayout(new GridLayout(2,1));

	JLabel yourChoicesJLabel = new JLabel("A LA CARTE MENU");
	pane1.add(yourChoicesJLabel, BorderLayout.NORTH);
	yourChoicesJLabel.setFont(new Font("Dialog",Font.BOLD,20));

	yourChoices = new JList<>(yourChoicesItems);
	pane1.add(new JScrollPane(yourChoices), BorderLayout.WEST);
	yourChoices.setFont(new Font("Courier",Font.BOLD+Font.ITALIC,14));

	bill = new JTextArea();
	pane1.add(bill, BorderLayout.EAST);
	bill.setFont(new Font("Courier",Font.PLAIN,12));

	JButton button1 = new JButton("Selection Completed");
	pane1.add(button1, BorderLayout.SOUTH);
	button1.addActionListener(this);

    JButton button2 = new JButton("Add");
    pane2.add(button2);
    button2.addActionListener(this);

    JButton button3 = new JButton("Delete");
    pane2.add(button3);
    button3.addActionListener(this);

	setSize(500, 360);
	setVisible(true);
	setDefaultCloseOperation(EXIT_ON_CLOSE);
}

Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Help with Layout Managers

Posted 09 October 2012 - 09:09 PM

Well now all you have to do is add your panels to your JFrame.
Simply add(componentName) will work.

The best way to do it is to create a Container with a layout that is equal to the content pane of the frame. Then just add the panels to that container.

Container cp = getContentPane();
cp.setLayout(/*whatever layout*/)
cp.add(/*component*/,/*layout argument*/)

Ta-dah.


This post has been edited by Kinaces: 09 October 2012 - 09:11 PM

Was This Post Helpful? 0
  • +
  • -

#5 summer_breeze  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 27-July 12

Re: Help with Layout Managers

Posted 09 October 2012 - 09:20 PM

View PostKinaces, on 09 October 2012 - 09:09 PM, said:

Well now all you have to do is add your panels to your JFrame.
Simply add(componentName) will work.

The best way to do it is to create a Container with a layout that is equal to the content pane of the frame. Then just add the panels to that container.

Container cp = getContentPane();
cp.setLayout(/*whatever layout*/)
cp.add(/*component*/,/*layout argument*/)

Ta-dah.


What am I doing wrong? :helpsmilie:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Test extends JFrame implements ActionListener
{
	static String[] yourChoicesItems =
	                         {"Blueberry Muffin        1.45",
	                          "Strawberry Bagel        0.80",
	                          "Lite Yogurt             0.75",
	                          "Vannila Ice Cream       2.75",
	                          "Hash Browns             2.50",
	                          "Toast                   2.00",
	                          "French Fries            1.50",
	                          "Onion Soup              3.00",
	                          "Coffee                  0.90",
	                          "Iced Tea                1.00",
	                          "Hot Chocolate           1.75"};
    static double[] yourChoicesPrices = {1.45, 0.80, 0.75, 2.75,
                                         2.50, 2.00, 1.50, 3.00,
                                         0.90, 1.00, 1.75};
    private JList<String> yourChoices;
    private JTextArea bill;
    private Container cpane = getContentPane();
    private JPanel pane1;
    private JPanel pane2;

public Test()
{
	super("Welcome to Java Kiosk");
    cpane.setBackground(new Color(0, 200, 200));
    cpane.setLayout(new GridLayout(2,2));

	JLabel yourChoicesJLabel = new JLabel("A LA CARTE MENU");
	pane1.add(yourChoicesJLabel, BorderLayout.NORTH);
	yourChoicesJLabel.setFont(new Font("Dialog",Font.BOLD,20));

	yourChoices = new JList<>(yourChoicesItems);
	pane1.add(new JScrollPane(yourChoices), BorderLayout.WEST);
	yourChoices.setFont(new Font("Courier",Font.BOLD+Font.ITALIC,14));

	bill = new JTextArea();
	pane1.add(bill, BorderLayout.EAST);
	bill.setFont(new Font("Courier",Font.PLAIN,12));

	JButton button1 = new JButton("Selection Completed");
	pane1.add(button1, BorderLayout.SOUTH);
	button1.addActionListener(this);

    JButton button2 = new JButton("Add");
    pane2.add(button2);
    button2.addActionListener(this);

    JButton button3 = new JButton("Delete");
    pane2.add(button3);
    button3.addActionListener(this);

    cpane.add(pane1, new BorderLayout(5,5));
	cpane.add(pane2, new GridLayout(2,1));
	setSize(500, 360);
	setVisible(true);
	setDefaultCloseOperation(EXIT_ON_CLOSE);
}

Was This Post Helpful? 0
  • +
  • -

#6 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Help with Layout Managers

Posted 09 October 2012 - 09:28 PM

Even I missed this until I actually tried to run the program. You never instantiated the JPanels.
Was This Post Helpful? 2
  • +
  • -

#7 summer_breeze  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 27-July 12

Re: Help with Layout Managers

Posted 09 October 2012 - 09:35 PM

View PostKinaces, on 09 October 2012 - 09:28 PM, said:

Even I missed this until I actually tried to run the program. You never instantiated the JPanels.

Thanks for helping out! :w00t:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1