JApplet using JPanel

Panels not displaying!

Page 1 of 1

10 Replies - 5916 Views - Last Post: 21 November 2009 - 11:56 PM Rate Topic: -----

#1 dnamrax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 18-September 09

JApplet using JPanel

Post icon  Posted 20 November 2009 - 04:23 PM

I am working on a program that is to be a basic "Font Selector" type.

You can select the color, font type, size, bold, and italic settings.

For now the output is just the text "SAMPLE" at the bottom of the applet.

The problem I am running into is with getting the panels to display.
I have never used panels before, I only have experience with stuff like FlowLayout in an applet.

It is my understanding that when you add a panel to the init() method it should be displayed in the applet?

Anyways, I have been looking threw the code and trying things for awhile now and can't seem to find out how to get the panels to display.
Can anyone help me to display the JPanels { radioPanel, fontPanel, and stylePanel }?


Here is my code...

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

public class FontEdit extends JApplet implements ActionListener
{
	private JRadioButton radio1;
	private JRadioButton radio2;
	private JRadioButton radio3;
	private JRadioButton radio4;
	private ButtonGroup radioGroup;
	private JPanel radioPanel;

	private JComboBox fontBox;
	private JList sizeList;
	private int sizeIndex;
	private JPanel fontPanel;

	private JCheckBox bold;
	private JCheckBox italic;
	private JPanel stylePanel;

	public void init()
	{
		setLayout(new FlowLayout());

		radio1 = new JRadioButton("Black", true);
		radio2 = new JRadioButton("Red", false);
		radio3 = new JRadioButton("Green", false);
		radio4 = new JRadioButton("Blue", false);
		radioPanel = new JPanel();
		radioPanel.setSize(250, 100);
		radioPanel.add(radio1);
		radioPanel.add(radio2);
		radioPanel.add(radio3);
		radioPanel.add(radio4);
		radioGroup = new ButtonGroup();
		radioGroup.add(radio1);
		radioGroup.add(radio2);
		radioGroup.add(radio3);
		radioGroup.add(radio4);
		radio1.addActionListener(this);
		radio2.addActionListener(this);
		radio3.addActionListener(this);
		radio4.addActionListener(this);

		String choices[] = {"TimesRoman", "Courier", "Helvetica"};
		fontBox = new JComboBox(choices);
		fontBox.setSelectedIndex(1);
		String sizes[] = {"10", "14", "18", "24"};
		sizeList = new JList(sizes);
		sizeList.setVisibleRowCount(4);
		sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		sizeList.setSelectedIndex(0);
		fontPanel = new JPanel();
		fontPanel.setSize(250, 100);
		fontPanel.add(fontBox);
		fontPanel.add(sizeList);
		fontBox.addActionListener(this);
		sizeList.addListSelectionListener(new ListSelectionListener()
		{
			public void valueChanged(ListSelectionEvent event)
			{
				sizeIndex = sizeList.getSelectedIndex();
				repaint();
			}
		});

		bold = new JCheckBox("Bold");
		italic = new JCheckBox("Italic");
		stylePanel = new JPanel();
		stylePanel.setSize(250, 50);
		stylePanel.add(bold);
		stylePanel.add(italic);
		bold.addActionListener(this);
		italic.addActionListener(this);
		
		add(radioPanel);
		add(fontPanel);
		add(stylePanel);
	}

	public void paint(Graphics g)
	{
		if(radio1.isSelected())
			g.setColor(Color.black);
		else if(radio2.isSelected())
			g.setColor(Color.red);
		else if(radio3.isSelected())
			g.setColor(Color.green);
		else
			g.setColor(Color.blue);

		String style;
		if(fontBox.getSelectedIndex() == 0)
			style = "Serif";
		else if(fontBox.getSelectedIndex() == 1)
			style = "Monospaced";
		else
			style = "SanaSerif";

		int size;
		if(sizeIndex == 0)
			size = 10;
		else if(sizeIndex == 1)
			size = 14;
		else if(sizeIndex == 2)
			size = 18;
		else
			size = 24;

		if(bold.isSelected())
			g.setFont(new Font(style, Font.BOLD, size));
		else if(italic.isSelected())
			g.setFont(new Font(style, Font.ITALIC, size));
		else if(bold.isSelected() && italic.isSelected())
			g.setFont(new Font(style, Font.BOLD + Font.ITALIC, size));
		else
			g.setFont(new Font(style, Font.PLAIN, size));
		
		g.drawString("SAMPLE", 25, 250);
	}

	public void actionPerformed(ActionEvent evt)
	{
			repaint();
	}
}


and the html to run the Japplet after the .java has been compiled to .class...

FontEdit.html
<html>
<applet code = "FontEdit.class" width = "250" height = "500">
</applet>
</html>


thanks for reading my post!

Is This A Good Question/Topic? 0
  • +

Replies To: JApplet using JPanel

#2 fsloke  Icon User is offline

  • D.I.C Regular

Reputation: 25
  • View blog
  • Posts: 412
  • Joined: 19-December 07

Re: JApplet using JPanel

Posted 20 November 2009 - 05:00 PM

The first step before change the code to applet is test it in JFrame.

Now the code can output the Frame.
Change your code to use actionPerformed and actionListener.

We very rare use paint() ===> Paint() is for drawing purpose eg draw line

Here the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class FontEdit extends JFrame implements ActionListener
{
	private JRadioButton radio1;
	private JRadioButton radio2;
	private JRadioButton radio3;
	private JRadioButton radio4;
	private ButtonGroup radioGroup;
	private JPanel radioPanel;

	private JComboBox fontBox;
	private JList sizeList;
	private int sizeIndex;
	private JPanel fontPanel;

	private JCheckBox bold;
	private JCheckBox italic;
	private JPanel stylePanel;

	public FontEdit()
	{
		setLayout(new FlowLayout());
	   
		radio1 = new JRadioButton("Black", true);
		radio2 = new JRadioButton("Red", false);
		radio3 = new JRadioButton("Green", false);
		radio4 = new JRadioButton("Blue", false);
		radioPanel = new JPanel();
		radioPanel.setSize(250, 100);
		radioPanel.add(radio1);
		radioPanel.add(radio2);
		radioPanel.add(radio3);
		radioPanel.add(radio4);
		radioGroup = new ButtonGroup();
		radioGroup.add(radio1);
		radioGroup.add(radio2);
		radioGroup.add(radio3);
		radioGroup.add(radio4);
		radio1.addActionListener(this);
		radio2.addActionListener(this);
		radio3.addActionListener(this);
		radio4.addActionListener(this);

		String choices[] = {"TimesRoman", "Courier", "Helvetica"};
		fontBox = new JComboBox(choices);
		fontBox.setSelectedIndex(1);
		String sizes[] = {"10", "14", "18", "24"};
		sizeList = new JList(sizes);
		sizeList.setVisibleRowCount(4);
		sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		sizeList.setSelectedIndex(0);
		fontPanel = new JPanel();
		fontPanel.setSize(250, 100);
		fontPanel.add(fontBox);
		fontPanel.add(sizeList);
		fontBox.addActionListener(this);
		sizeList.addListSelectionListener(new ListSelectionListener()
		{
			public void valueChanged(ListSelectionEvent event)
			{
				sizeIndex = sizeList.getSelectedIndex();
				repaint();
			}
		});

		bold = new JCheckBox("Bold");
		italic = new JCheckBox("Italic");
		stylePanel = new JPanel();
		stylePanel.setSize(250, 50);
		stylePanel.add(bold);
		stylePanel.add(italic);
		bold.addActionListener(this);
		italic.addActionListener(this);

		add(radioPanel);
		add(fontPanel);
		add(stylePanel);

		setVisible(true);
		setSize(300,200);
	}

	public void paint(Graphics g)
	{
		if(radio1.isSelected())
			g.setColor(Color.black);
		else if(radio2.isSelected())
			g.setColor(Color.red);
		else if(radio3.isSelected())
			g.setColor(Color.green);
		else
			g.setColor(Color.blue);

		String style;
		if(fontBox.getSelectedIndex() == 0)
			style = "Serif";
		else if(fontBox.getSelectedIndex() == 1)
			style = "Monospaced";
		else
			style = "SanaSerif";

		int size;
		if(sizeIndex == 0)
			size = 10;
		else if(sizeIndex == 1)
			size = 14;
		else if(sizeIndex == 2)
			size = 18;
		else
			size = 24;

		if(bold.isSelected())
			g.setFont(new Font(style, Font.BOLD, size));
		else if(italic.isSelected())
			g.setFont(new Font(style, Font.ITALIC, size));
		else if(bold.isSelected() && italic.isSelected())
			g.setFont(new Font(style, Font.BOLD + Font.ITALIC, size));
		else
			g.setFont(new Font(style, Font.PLAIN, size));

		g.drawString("SAMPLE", 25, 250);
	}

	public void actionPerformed(ActionEvent evt)
	{
			repaint();
	}

	public static void main(String [] args){
		FontEdit a = new FontEdit();
	}
}



PLease further post if you need any assistance.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,214
  • Joined: 20-September 08

Re: JApplet using JPanel

Posted 20 November 2009 - 05:01 PM

Call

super.paint(g)


as the first line of paint
Was This Post Helpful? 0
  • +
  • -

#4 dnamrax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 18-September 09

Re: JApplet using JPanel

Posted 20 November 2009 - 05:19 PM

I tested the code in JFrame, using the code fsloke provided(thank you), and the JPanels display!

So why are they not working in an applet?
Was This Post Helpful? 0
  • +
  • -

#5 fsloke  Icon User is offline

  • D.I.C Regular

Reputation: 25
  • View blog
  • Posts: 412
  • Joined: 19-December 07

Re: JApplet using JPanel

Posted 20 November 2009 - 05:24 PM

You forgot to set:

setVisible(true);
setSize(300,200);

And the Constructor

public FontEdit()

Not

public void FontEdit()

Please try to finish up your code. Then the next step is integrate with Applet.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JApplet using JPanel

Posted 20 November 2009 - 05:29 PM

JPanel panel = new JPanel();

creates a JPanel with a null layout
in a panel with null layout you have to set the bounds (sie, position) of all components which default to 0,0,0,0
so sure you can see them

You seem to be familiar with the FlowLayout if you are confortable with it use it

but from what I see... you should use radioPanel = new JPanel(new GridLayout(1,4));
that will make a row of 4 "squares" where your buttons can be positioned

If you play with panels, the Applet main panel shouldn't be a FlowLayout as you do

setLayout(new FlowLayout());

use a BorderLayout

setLayout(new BorderLayout());

this splits your screen into 5 regions: NORTH, SOUTH, EAST, WEST, CENTER

now you can add your radioButton to the top

add(radioPanel, BorderLayout.NORTH);

The fontPanel should go CENTER

add(fontPanel, BorderLayout.CENTER);

and do not set the size() or your different panels, in that type of arrangment the layout manager will take care of all of these

http://java.sun.com/...rderLayout.html
Was This Post Helpful? 0
  • +
  • -

#7 dnamrax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 18-September 09

Re: JApplet using JPanel

Posted 20 November 2009 - 08:53 PM

Thanks for all the feedback!

I seem to have the JFrame working the way it should.
Is there anyway to import the JFrame into a JApplet?
This would seem to be the best way to do things as when I try to write the code as a JApplet without the JFrame it doesn't like to work.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,214
  • Joined: 20-September 08

Re: JApplet using JPanel

Posted 21 November 2009 - 01:19 AM

Quote

Panel panel = new JPanel();

creates a JPanel with a null layout


It creates a panel with a FlowLayout

Quote

when I try to write the code as a JApplet without the JFrame it doesn't like to work.


I showed you how to solve that problem above - maybe you missed it?
Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JApplet using JPanel

Posted 21 November 2009 - 12:58 PM

View Postg00se, on 21 Nov, 2009 - 12:19 AM, said:

Quote

Panel panel = new JPanel();

creates a JPanel with a null layout


It creates a panel with a FlowLayout

In an Applet you are right. Sorry
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,214
  • Joined: 20-September 08

Re: JApplet using JPanel

Posted 21 November 2009 - 01:17 PM

Quote

In an Applet you are right.


It's the default layout for a JPanel
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JApplet using JPanel

Posted 21 November 2009 - 11:56 PM

View Postg00se, on 21 Nov, 2009 - 12:17 PM, said:

Quote

In an Applet you are right.


It's the default layout for a JPanel

OK... never use FlowLayout so that is why I alsways create my JPanel(withANotherLayout)
and it is the JFrame that by default has BorderLayout
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1