Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Setlocation Won't Work

Recommended Posts

I'm having trouble getting my applet's layout set up properly. I can't get any layout other then GridLayout to work, and GridLayout doesn't look all that great because I would like to make the graph bigger then the buttons, etc.

I've tried using other layout managers, but I couldn't get any of them to work... so then I continued researching, and I found mention of a way to arrange things using setLocation? I tried it... but it just won't work! I use it... and theres no differance in location, although the code doesnt error.

So now I'm stuck. Please, any help would be appreciated... the project is due on Friday, and I've gotten stuck on such a small part... thanks again!

Code we've got so far:

Code:


import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.table.DefaultTableModel;import java.util.*;import java.io.*;public class CPT extends JPanel implements ActionListener{    JButton ModifyEntriesButton, ViewEntriesButton, SearchEntriesButton, SaveButton, BackButton;    private final static String newline = "\n";    private DefaultTableModel model;    public CPT ()    {	super (new GridLayout (3, 0));	model = new PropertiesModel ("entries.data");	ModifyEntriesButton = new JButton ("Modify Entries");	ModifyEntriesButton.setVerticalTextPosition (AbstractButton.TOP);	ModifyEntriesButton.setHorizontalTextPosition (AbstractButton.LEFT);	ModifyEntriesButton.setToolTipText ("Click this button to modify database entries.");	ModifyEntriesButton.setMnemonic (KeyEvent.VK_M);	ModifyEntriesButton.setActionCommand ("ModifyEntries");	ModifyEntriesButton.addActionListener (this);	ViewEntriesButton = new JButton ("View Entries");	ViewEntriesButton.setVerticalTextPosition (AbstractButton.CENTER);	ViewEntriesButton.setHorizontalTextPosition (AbstractButton.LEFT);	ViewEntriesButton.setToolTipText ("Click this button to add view all database entries.");	ViewEntriesButton.setMnemonic (KeyEvent.VK_V);	ViewEntriesButton.setActionCommand ("ViewEntries");	ViewEntriesButton.addActionListener (this);	SearchEntriesButton = new JButton ("Search Entries");	SearchEntriesButton.setVerticalTextPosition (AbstractButton.BOTTOM);	SearchEntriesButton.setHorizontalTextPosition (AbstractButton.LEFT);	SearchEntriesButton.setToolTipText ("Click this button to search through all database entries.");	SearchEntriesButton.setMnemonic (KeyEvent.VK_S);	SearchEntriesButton.setActionCommand ("SearchEntries");	SearchEntriesButton.addActionListener (this);	SaveButton = new JButton ("Save");	SaveButton.setVerticalTextPosition (AbstractButton.TOP);	SaveButton.setHorizontalTextPosition (AbstractButton.RIGHT);	SaveButton.setToolTipText ("Click this button to save database entries.");	SaveButton.setMnemonic (KeyEvent.VK_S);	SaveButton.setActionCommand ("Save");	SaveButton.addActionListener (this);	BackButton = new JButton ("Back");	BackButton.setVerticalTextPosition (AbstractButton.BOTTOM);	BackButton.setHorizontalTextPosition (AbstractButton.RIGHT);	BackButton.setToolTipText ("Click this button to return to the main menu.");	BackButton.setMnemonic (KeyEvent.VK_B);	BackButton.setActionCommand ("Back");	BackButton.addActionListener (this);	add (ModifyEntriesButton);	add (ViewEntriesButton);	add (SearchEntriesButton);    }    class PropertiesModel extends DefaultTableModel    {	public PropertiesModel (String filename)	{     addColumn ("Item Number");     addColumn ("Description");     addColumn ("Price");     //Fill model with data from property file     Properties props = readFile (filename);     if (props != null)     {  Enumeration coll = props.keys ();  while (coll.hasMoreElements ())  {      String property = (String) coll.nextElement ();      String value = props.getProperty (property, "");      addRow (new Object[]      {  	property, value      }      );  }     }	}    }    private Properties readFile (String filename)    {	try	{     Properties props = new Properties ();     props.load (new FileInputStream (filename));     return props;	}	catch (IOException ioe)	{     return null;	}    }    private boolean saveFile (String filename)    {	try	{     Properties props = new Properties ();     for (int i = 0; i < model.getRowCount (); i++)  props.put (model.getValueAt (i, 0), model.getValueAt (i, 1));     props.store (new FileOutputStream (filename), null);     return true;	}	catch (IOException ioe)	{     return false;	}    }    private void ModifyEntriesFunction ()    {	removeAll ();	add (new JScrollPane (new JTable (model)));	//add (new JScrollPane (new JTable (model)), BorderLayout.CENTER);	add (SaveButton);	add (BackButton);	invalidate ();	updateUI ();    }    public void actionPerformed (ActionEvent e)    {	if ("ModifyEntries".equals (e.getActionCommand ()))	{     ModifyEntriesFunction ();	}	if ("ViewEntries".equals (e.getActionCommand ()))	{     removeAll ();     add (BackButton);     invalidate ();     updateUI ();	}	if ("SearchEntries".equals (e.getActionCommand ()))	{     removeAll ();     add (BackButton);     invalidate ();     updateUI ();	}	if ("Back".equals (e.getActionCommand ()))	{     removeAll ();     add (ModifyEntriesButton);     add (ViewEntriesButton);     add (SearchEntriesButton);     invalidate ();     updateUI ();	}	if ("Save".equals (e.getActionCommand ()))	{     if (saveFile ("entries.data"))  JOptionPane.showMessageDialog (null, "File saved successfully.");     else  JOptionPane.showMessageDialog (null, "File could not be saved!");	}    }    // Create the GUI and show it. For thread safety,    // this method should be invoked from the    // event-dispatching thread.    private static void createAndShowGUI ()    {	//Make sure we have nice window decorations.	JFrame.setDefaultLookAndFeelDecorated (true);	//Create and set up the window.	JFrame frame = new JFrame ("Swisha Computer House");	frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);	//Create and set up the content pane.	JComponent newContentPane = new CPT ();	newContentPane.setOpaque (true); //content panes must be opaque	frame.setContentPane (newContentPane);	//Display the window.	frame.pack ();	frame.setSize (300, 300);	frame.setVisible (true);    }    public static void main (String[] args)    {	//Schedule a job for the event-dispatching thread:	//creating and showing this application's GUI.	javax.swing.SwingUtilities.invokeLater (new Runnable ()	{     public void run ()     {  createAndShowGUI ();     }	}	);    }}Attached Images proj.JPG (31.9 KB, 2 views) 

Share this post


Link to post
Share on other sites

I wish if this was something related to PHP :-SI'd really appreciate if anyone could come forward and provide a solution. kvarnerexpress, if you happen to find out the solution, please update this thread.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.