GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Mar-2009, 22:26
MasterAchilles MasterAchilles is offline
New Member
 
Join Date: Mar 2009
Posts: 5
MasterAchilles is on a distinguished road

Applet Problem: returning a custom JPanel to an applet


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.

JAVA Code:
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()
	{
		try
		{
			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);
					
					add(PERT_GetData());
				}
			});
		} 
		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.setOpaque(true);
		
		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.
		enterButton.addActionListener(this);
		
		// Add Action Listener to create Table Button.
		createTableButton.addActionListener(this);
		
		// Add the components to the JPanel.
		dataPanel.add(activityLabel);
		dataPanel.add(activityField);
		dataPanel.add(descriptionLabel);
		dataPanel.add(descriptionField);
		dataPanel.add(durationLabel);
		dataPanel.add(durationField);
		dataPanel.add(successorsLabel);
		dataPanel.add(successorField1);
		dataPanel.add(successorField2);
		dataPanel.add(successorField3);
		dataPanel.add(enterButton);
		dataPanel.add(createTableButton);
		
		dataPanel.validate();
		
		// 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
		createGraphButton.addActionListener(this);
		
		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);
		
		finalTablePanel.validate();
		
		// 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();
			System.out.println(tempAct);
			
			// Set Activity
			path[index].setActivity(tempAct);
			activityField.setText("");
			
			// Set Description
			path[index].setDescription(descriptionField.getText());
			descriptionField.setText("");
			
			// Set Duration
			path[index].setDuration(Integer.parseInt(durationField.getText()));
			durationField.setText("");
			
			// Set Successors
			path[index].setSuccessor1(successorField1.getText());
			successorField1.setText("");
			path[index].setSuccessor2(successorField2.getText());
			successorField2.setText("");
			path[index].setSuccessor3(successorField3.getText());
			successorField3.setText("");
			
			index = index + 1;
		}
		
		else if(ae.getActionCommand().equals("Create Table"))
		{
			if(index > 0 && !tableCreated)
			{		
				PERT_Table = new JTable(new PERT_TableModel(index, path));
				
				getContentPane().add(createPERT_Table());
				
				tableCreated = true;
				
				getContentPane().remove(dataPanel);
			}
		} 
		
		else if(ae.getActionCommand().equals("Create Graph"))
		{
			getContentPane().add(createGraph());
			
			graphCreated = true;
			
			getContentPane().remove(finalTablePanel);
		}
	}
}

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.
JAVA Code:
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
		super.paintComponent(g);
		
		// cast the graphics object into a Graphics2D object
		// because we want pretty anti-aliasing
		Graphics2D g2d = (Graphics2D)g;
		
        // Enable anti-aliasing for shapes
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		
		g2d.fillOval(xPos, yPos, radius, radius);
	}
}

Is there any other way to return it or repaint it once it returns?
Last edited by LuciWiz : 27-Mar-2009 at 01:44. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 27-Mar-2009, 07:32
MasterAchilles MasterAchilles is offline
New Member
 
Join Date: Mar 2009
Posts: 5
MasterAchilles is on a distinguished road

Re: Applet Problem: returning a custom JPanel to an applet


Never mind. Thanks anyway.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
java.lang.NullPointerException Error Help MasterAchilles Java Forum 4 26-Mar-2009 09:00
Applet problem john123 Java Forum 0 27-Jul-2006 10:36
Problem returning value mikhail C Programming Language 4 10-Aug-2004 03:25

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:20.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.