Applet Problem

Problemreturning a custom JPanel to an applet

Page 1 of 1

1 Replies - 495 Views - Last Post: 28 March 2009 - 01:55 PM Rate Topic: -----

#1 MasterAchilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-March 09

Applet Problem

Posted 26 March 2009 - 09:31 PM

I am trying to create applet which first asks a user for info and then stores that info in an array of a separate class. Using that info it creates a Table. So far that all works. Now I am trying to use that info to create a graph.

I have created an custom panel that this initial class can call to paint() the oanel and its drwan contents. For some reason however it is just printing the panel by itself to the applet.

In the actionPerformed() method I am calling the method createGraph() which returns the JPanel to the content pane of the applet.

package PERT_Project;
import java.awt.*;

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

public class PERT_Data extends JApplet implements ActionListener
	// Initialize Pert_SETUP to store info about each node of the graph.
	PERT_SETUP[] path = new PERT_SETUP[10];
	// Initialize Panels to go through the different screens of the applet.
	// 1: PERT_GetData(): Get info about nodes from each user.
	// 2: PERT_Table(): Print out JTable for user
	// 3: PERT_Graph(): Print out Graph with nodes and lines.
	private JPanel dataPanel;
	private JPanel finalTablePanel;
	private PaintGraph graphPanel;
	// input PERT_Data
	private JLabel activityLabel;
	private JTextField activityField;
	private JLabel descriptionLabel;
	private JTextField descriptionField;
	private JLabel durationLabel;
	private JTextField durationField;
	private JLabel successorsLabel;
	private JTextField successorField1;
	private JTextField successorField2;
	private JTextField successorField3;
	private JButton enterButton;
	private JButton createTableButton;
	// createPERT_Table
	private JButton createGraphButton;
	private JTable PERT_Table;
	// Action Performed
	int index = 0;
	private boolean tableCreated = false;
	private boolean criticalPathCreated = false;
	private boolean graphCreated = false;
	// Initialize applet and construct the GUI.
	public void init()
			SwingUtilities.invokeAndWait(new Runnable()
				public void run()
					for(int m = 0; m < path.length; m++)
						path[m] = new PERT_SETUP();
					tableCreated = false;
					criticalPathCreated = false;
					//Creates a box layout for the JPanels.
					getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
					setSize(600, 600);
		catch(Exception e)
			System.out.println("Can't create because of " + e);
	// Initial GUI for User input data
	public JPanel PERT_GetData()
		// Set Layout
		setLayout(new FlowLayout());
		// Create a new JPanel to hold all elements.
		dataPanel = new JPanel();
		// Set Preferred Size of Panel
		dataPanel.setPreferredSize(new Dimension(340, 300));
		dataPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
		// Create Labels
		activityLabel = new JLabel("Enter Activity Character: ");
		descriptionLabel = new JLabel("Enter a short Description for this Activity.");
		durationLabel = new JLabel("Enter the time required to perform this Activity.");
		successorsLabel = new JLabel("Enter all of the successors of this Activity.(From 1- 3)");
		// Create Text Fields.
		activityField = new JTextField(5);
		descriptionField = new JTextField(20);
		durationField = new JTextField(15);
		successorField1 = new JTextField(5);
		successorField2 = new JTextField(5);
		successorField3 = new JTextField(5);
		// Create Enter button.
		enterButton = new JButton("Enter Activity Data");
		enterButton.setActionCommand("Enter Data");
		// Create Table Button.
		createTableButton = new JButton("Create Table");
		// Add Action Listener to enter button.
		// Add Action Listener to create Table Button.
		// Add the components to the JPanel.
		// Return panel holding data from user to the applet.
		return dataPanel;
	public JPanel createPERT_Table()
		createGraphButton = new JButton("Create Graph");
		createGraphButton.setMaximumSize(new Dimension(50, 25));
		// Press to create graph
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(createGraphButton, BorderLayout.CENTER);
		JPanel headerArea = new JPanel();
		headerArea.setLayout(new BorderLayout());
		headerArea.add(PERT_Table.getTableHeader(), BorderLayout.LINE_START);
		// Create area that holds entire Table Panel
		// Initialize each panels place in this main panel.
		JPanel tablePanel = new JPanel();
		tablePanel.setLayout(new BorderLayout());
		// Header goes at the top of tablePanel(NORTH)
		tablePanel.add(headerArea, BorderLayout.PAGE_START);
		// The actual Table(PERT_Table) goes to the EAST.
		tablePanel.add(PERT_Table, BorderLayout.LINE_START);
		// Button goes to the SOUTH.
		tablePanel.add(buttonPanel, BorderLayout.PAGE_END);
		finalTablePanel = new JPanel();
		finalTablePanel.setLayout(new BorderLayout());
		finalTablePanel.add(tablePanel, BorderLayout.LINE_START);
		// Put panel holding table onto the applet.
		return finalTablePanel;
	public JPanel createGraph()
		graphPanel = new PaintGraph();
		return graphPanel;
	public void actionPerformed(ActionEvent ae) 
		if(ae.getActionCommand().equals("Enter Data"))
			String tempAct = activityField.getText().trim();
			// Set Activity
			// Set Description
			// Set Duration
			// Set Successors
			index = index + 1;
		else if(ae.getActionCommand().equals("Create Table"))
			if(index > 0 && !tableCreated)
				PERT_Table = new JTable(new PERT_TableModel(index, path));
				tableCreated = true;
		else if(ae.getActionCommand().equals("Create Graph"))
			graphCreated = true;

I am just using a test case and this is not really what I want the Panel to display but I am just trying to get it to work at the moment.
package PERT_Project;

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

public class PaintGraph extends JPanel
	public int xPos;
	public int yPos;
	public int radius;
	public Color color;
	public int earliestStart;
	public int earliestFinish;
	public int time;
	public String name;
	// Default configuration of this panel
	public PaintGraph()
		xPos = 100;
		yPos = 100;
		radius = 20;
	// Override the paintComponent() method
	protected void paintComponent(int index, Graphics g)
		// Call super class method first to repaint component
		// cast the graphics object into a Graphics2D object
		// because we want pretty anti-aliasing
		Graphics2D g2d = (Graphics2D)g;
		// Enable anti-aliasing for shapes
		g2d.fillOval(xPos, yPos, radius, radius);

Is there any other way to return it so that it repaints?

Is This A Good Question/Topic? 0
  • +

Replies To: Applet Problem

#2 pbl  Icon User is offline

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

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

Re: Applet Problem

Posted 28 March 2009 - 01:55 PM

Your PaintGraph JPanel has a NullLayout so it should be dimensioned
Now I am not sure how in a BoxLayout (from jour original Applet) you can Add and Add and Add new GraphPanel() as often as your actionPerformed() method is called.

Better to create one single instance of GraphPanel and put it empty in your Applet
and redraw it if needed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1