capstone project

no rush, first version

Page 1 of 1

11 Replies - 1869 Views - Last Post: 21 January 2010 - 01:12 PM Rate Topic: -----

#1 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

capstone project

Posted 03 January 2010 - 02:12 PM

This is my code.

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

public class farmersPanel  extends JFrame
{

	private JButton addAnimal = new JButton("Add Animal");
	private JButton addCrop = new JButton("Add Crop");
	private JButton addMilk = new JButton("Add Milk");
	private JButton Previous = new JButton("Previous");
	private JButton Next = new JButton("Next");
	private JButton Save = new JButton("Save");
	private JPanel farmersPanel = new JPanel();

String [] columnNamesAnimals = {"Product", "Weight", "Expenses", "Selling Price"};

	Object [][] animalData = {
		{"", "", "", ""}, 
		{"", "", "", ""}
	};

	DefaultTableModel animalTableModel = new DefaultTableModel(animalData, columnNamesAnimals);
	JTable animalTable = new JTable(animalTableModel);


	public farmersPanel()
	{   
		setLayout(new GridBagLayout());
		
		GridBagConstraints gbConstraints = new GridBagConstraints();
		
		gbConstraints.fill = GridBagConstraints.BOTH;
		gbConstraints.anchor  = GridBagConstraints.CENTER;
		
		Container container = getContentPane();
		
		addComp(addAnimal, container, gbConstraints, 0, 0, 1, 2, 0, 0);
		addComp(addCrop, container, gbConstraints, 0, 2, 1, 1, 5, 0);
		addComp(addMilk, container, gbConstraints, 0, 3, 1, 1, 5, 0);
		addComp(animalTable, container, gbConstraints, 2, 3, 5, 3, 5, 5);
		addComp(Previous, container, gbConstraints, 3, 1, 1, 1, 5, 0);
		addComp(Next, container, gbConstraints, 3, 3, 1, 1, 5, 0);
		addComp(Save, container, gbConstraints, 3, 4, 1, 1, 5, 0);
		
		farmersPanel.setBackground(Color.black);
		farmersPanel.setBorder(new javax.swing.border.LineBorder(Color.black));
		
		gbConstraints.insets = new Insets(10, 10, 10, 10);
		addComp(farmersPanel, container, gbConstraints, 1, 1, 2, 2, 10, 1);
	}

	private void addComp(Component c, Container container, GridBagConstraints gbConstraints,
						 int row, int column, int numberOfRows, int numberOfColumns,
						 double weightx, double weighty)
	{
		gbConstraints.gridx = column;
		gbConstraints.gridy = row;
		gbConstraints.gridwidth = numberOfColumns;
		gbConstraints.gridheight = numberOfRows;
		gbConstraints.weightx = weightx;
		gbConstraints.weighty = weighty;

		container.add(c, gbConstraints);
	}
	public static void main(String[] args)
	{

		farmersPanel frame = new farmersPanel();
		frame.setTitle("Farmers Records of Crops and Produce");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 250);
		frame.setVisible(true);

	}
}



As you can see, my code is clear cut and professional looking, as is required by the capstone professor. The only problem I'm having is not an error this time, but the resizing of my table on the panel. The way I have it now it's in the right row, but not extending to the full length of the row, and there is a big black box on the left hand side that i cannot seem to make go away. Simple thing to fix I know, but I can't seem to get it.

Thanks in advance for your help.

Edit: I am using a gridbag layout, and I have 2 more table to add yet.

This post has been edited by circuspeanuts: 03 January 2010 - 02:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: capstone project

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: capstone project

Posted 03 January 2010 - 03:10 PM

Looks like you are doing things back to front here. That black blob is actually your JPanel. You are never adding anything to this panel. You pass everything to your addComp method, and place it onto a Container. So why do you have this JPanel if your using a container?

GridBagLayout is quite a complicated layout, and you shouldnt use it unless you are good with it. Your first problem is that as it stands, your table will never spread the whole length, because your settings placed on your next and save buttons interfer with this.

ADVISE: Firstly, dont limit yourself to one layout. Split your gui up into sections, and have 1 JPanel for each section, with the components placed on their appropiate panel. What I normally do is have 5 JPanels, and using BorderLayout, I set one at the top, one at the bottom, one on each side, and one in the middle. The top panel I place my title JLabel, bottom panel, I place my JButtons, middle panel I place the main content and etc.

If you want to work on what you already have, start to comment out these lines
//addComp(Next, container, gbConstraints, 3, 3, 1, 1, 5, 0);
//addComp(Save, container, gbConstraints, 3, 4, 1, 1, 5, 0);
	   
//gbConstraints.insets = new Insets(10, 10, 10, 10);
// addComp(farmersPanel, container, gbConstraints, 1, 1, 2, 2, 10, 1);


And change to this
addComp(animalTable, container, gbConstraints, 2, 0, 16, 3, 5, 5);


Its not a complete fix, but it will show you your problem through what I have said and commented out.
Was This Post Helpful? 0
  • +
  • -

#3 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: capstone project

Posted 03 January 2010 - 03:50 PM

I have to have that section though because that's where my buttons are. would it be better for me to get rid of the JPanel and would that be a fix of sorts?
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: capstone project

Posted 03 January 2010 - 03:53 PM

What I posted will somewhat solve the problem, but if you try and put everything on one component, your just going to run into more issues later. Place your buttons on a bottom panel with a FlowLayout, place your tables in a middle panel etc. Believe me, it might be more work now, but you will be praising it after.
Was This Post Helpful? 0
  • +
  • -

#5 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: capstone project

Posted 03 January 2010 - 04:02 PM

haha, well before I do that I will have to consult with the rest of our group, because we had planned on using a gridbag layout (I was outvoted for the record).
Was This Post Helpful? 0
  • +
  • -

#6 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: capstone project

Posted 04 January 2010 - 01:19 PM

I've fixed that, although I think my main problem now is that I'm not sure what each of the numbers mean directly. That is something I will look up in my book. So adding these other two tables will give me the basic same thing, but I noticed something. It gives me the table, but not the labels at the top of the table?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: capstone project

Posted 04 January 2010 - 02:44 PM

View Postcircuspeanuts, on 4 Jan, 2010 - 12:19 PM, said:

I've fixed that, although I think my main problem now is that I'm not sure what each of the numbers mean directly. That is something I will look up in my book. So adding these other two tables will give me the basic same thing, but I noticed something. It gives me the table, but not the labels at the top of the table?

JTable have to be in a JScrollPane for their column headers to be displayed
Was This Post Helpful? 1
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: capstone project

Posted 04 January 2010 - 03:27 PM

As PBL stated, you need to place your table into a scrollpane. I do it like this. Declare the variables
   protected static final String [] columnNames = 
	{
		"<html><b><h2>POSITION</h2></b></html>", 
		"<html><b><h2>NATIONALITY</h2></b></html>", 
		"<html><b><h2>FIRST NAME</h2></b></html>",
		"<html><b><h2>LAST NAME</h2></b></html>",
		"<html><b><h2>TIME SET</h2></b></html>",
		
	};
	
	//some hardcoded data for JTable
	private Object[][] data = {
	{"FIRST", "", "", "",}, {"SECOND", "", "", "",},
	{"THIRD", "", "", "",}
	};
	
	private JTable table;
	private DefaultTableModel model;
	private JScrollPane scroller;


And then initialise them somewhere else, creating your model, applying this to your table, and then adding your table to a scroll pane.
model = new DefaultTableModel(data, columnNames);  //Using default Table Model for the JTable
table = new JTable(model);
table.putClientProperty("terminateEditonfocusLost", Boolean.TRUE);
table.setRowHeight(25);
table.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),("selectNextColumnCell")); //giving the enter key the same properties as Tab key
table.isCellEditable(false);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;  //Always show scrollpane
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
scroller = new javax.swing.JScrollPane(table, v, h);  //put JTable and scrollpane constants into scroller (JScrollPane)
scroller.setPreferredSize(new Dimension(630,380));
wemRightPanel.add(scroller, 0);

Was This Post Helpful? 0
  • +
  • -

#9 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: capstone project

Posted 11 January 2010 - 06:00 PM

um wow..

I'll try that and see what I come up with.


I messed around with it and now it says it needs an identifier. Trying to work it out.


EDIT:

I moved the actual definition into my constructor and it now looks like this

public farmersPanel()
	{   
		setLayout(new GridBagLayout());
		
		 model = new DefaultTableModel(animalData, columnNamesAnimal);  //Using default Table Model for the JTable
		 animalTable = new JTable(model);
		 animalTable.putClientProperty("terminateEditonfocusLost", Boolean.TRUE);
		 animalTable.setRowHeight(25);
		 animalTable.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),("selectNextColumnCell")); //giving the enter key the same properties as Tab key
		 animalTable.isCellEditable(false);
		 int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;  //Always show scrollpane
		 int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
		 scroller = new javax.swing.JScrollPane(animalTable, v, h);  //put JTable and scrollpane constants into scroller (JScrollPane)
		 scroller.setPreferredSize(new Dimension(630,380));
		 wemRightPanel.add(scroller, 0);
		
		GridBagConstraints gbConstraints = new GridBagConstraints();
		
		gbConstraints.fill = GridBagConstraints.BOTH;
		gbConstraints.anchor  = GridBagConstraints.CENTER;
		
		Container container = getContentPane();
	   
		addComp(addAnimal, container, gbConstraints, 0, 0, 1, 2, 0, 0);
		addComp(addCrop, container, gbConstraints, 0, 2, 1, 1, 5, 0);
		addComp(addMilk, container, gbConstraints, 0, 3, 1, 1, 5, 0);
		addComp(animalTable, container, gbConstraints, 2, 3, 5, 3, 5, 5);
		addComp(Previous, container, gbConstraints, 3, 1, 1, 1, 5, 0);
		addComp(Next, container, gbConstraints, 3, 3, 1, 1, 5, 0);
		addComp(Save, container, gbConstraints, 3, 4, 1, 1, 5, 0);
		
		farmersPanel.setBackground(Color.black);
		farmersPanel.setBorder(new javax.swing.border.LineBorder(Color.black));
		
		gbConstraints.insets = new Insets(10, 10, 10, 10);
		addComp(animalTable, container, gbConstraints, 1, 1, 2, 8, 10, 1);
		addComp(animalTotal, container, gbConstraints, 1, 1, 3, 8, 10, 1);
		//addComp(cropTable, container, gbConstraints, 2, 2, 3, 8, 11, 2);
	}



it doesn't like
 wemRightPanel.add(scroller, 0);


I'm not sure I understand what the "wemRightPanel" part of it is.

and it doesn't like
animalTable.isCellEditable(false);



says the table can't be applied to the boolean

trying to figure these out.

This post has been edited by circuspeanuts: 11 January 2010 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#10 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: capstone project

Posted 11 January 2010 - 07:23 PM

K. The scrollbar needs to be added to somthing, I got that example from some previous work I had done. wemRightPanel is as it says, a JPanel. It is a JPanel I created though. I dont know how your doing it, whether your using seperate panels for your work. But whereever you want to add the scroller, just change
wemRightPanel.add(scroller, 0);


To whatever panel your using. Looking at your original post, it will prob be somthing like
farmersPanel.add(scroller, 0);



But as I mentioned before, you will find it a far greater benefit seperating all your work onto seperate panels.

As for your second error, just comment it out for now....even though it should work....Its not that important however, you can just remove it for now. Try the above and see what you get.
Was This Post Helpful? 0
  • +
  • -

#11 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: capstone project

Posted 21 January 2010 - 12:33 PM

haha ok, nick. Thanks, that helps a lot, and I figured out how to get it going in gridbaglayout!

check this out.


//imports required libraries in order to run the program correctly
import java.awt.*;
import javax.swing.*;

public class profitEstimator extends JFrame
{
		//upper panel buttons
		private JButton addAnimal = new JButton("Add Animal");
		private JButton addCrop = new JButton("Add Crop");
		private JButton addMilk = new JButton("Add Milk");

		//lower panel buttons
		private JButton previous = new JButton("Previous");
		private JButton next = new JButton("Next");
		private JButton save = new JButton("Save");
		private JButton delete = new JButton("Delete");
		private JButton print = new JButton("Print");

		//declaration of table
		String [] animalColumnNames = {"Product", "Live Weight", "Expenses", "Selling Cost"};
		Object [][] animalData =
		{
			{"", "","",""}, {"","","",""},{"","","",""}
		};
		JTable animalTable = new JTable(animalData, animalColumnNames);
		JScrollPane scrollPane = new JScrollPane(animalTable);
		animalTable.setFillsViewportHeight(true);


		//puts the tables on top of each other, and on the frame.
		private JPanel upperButtonPanel = new JPanel(new GridBagLayout());
		private JPanel lowerButtonPanel = new JPanel(new GridBagLayout());
		private JPanel mainGUI = new JPanel(new GridBagLayout());
		private JFrame frame2 = new JFrame("Profit Estimator");


	public profitEstimator()
	{
		GridBagConstraints gbConstraints = new GridBagConstraints();
		gbConstraints.fill = GridBagConstraints.BOTH;
		gbConstraints.anchor = GridBagConstraints.CENTER;

		frame2.setLayout(new GridBagLayout());

		addComp(addAnimal, upperButtonPanel, gbConstraints, 0, 0, 2, 1, 0, 0);
		addComp(addCrop, upperButtonPanel, gbConstraints, 0, 1, 2, 1, 0, 0);
		addComp(addMilk, upperButtonPanel, gbConstraints, 0, 2, 2, 1, 0, 0);

		addComp(previous, lowerButtonPanel, gbConstraints, 0, 0, 2, 1, 0, 0);
		addComp(next, lowerButtonPanel, gbConstraints, 0, 4, 2, 1, 0, 0);
		addComp(delete, lowerButtonPanel, gbConstraints, 0, 3, 1, 1, 0, 0);
		addComp(print, lowerButtonPanel, gbConstraints, 0, 2, 2, 1, 0, 0);
		addComp(save, lowerButtonPanel, gbConstraints, 0, 1, 2, 1, 0, 0);


		addComp(upperButtonPanel, mainGUI, gbConstraints, 0, 0, 1, 1, 0, 0);
		addComp(lowerButtonPanel, mainGUI, gbConstraints, 5, 0, 1, 1, 0, 0);

		addComp(mainGUI, frame2, gbConstraints, 0, 0, 1, 1, 0, 0);

		frame2.setTitle("Profit Estimator");
		frame2.setLocationRelativeTo(null);
		frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame2.setSize(400,300);
		frame2.setVisible(true);
		frame2.pack();
	}

private void addComp(Component c, Container container,
		GridBagConstraints gbConstraints, int row, int column, int numberOfRows,
		int numberOfColumns, double weightx, double weighty)
{
	gbConstraints.gridx = column;
	gbConstraints.gridy = row;
	gbConstraints.gridwidth = numberOfColumns;
	gbConstraints.gridheight = numberOfRows;
	gbConstraints.weightx = weightx;
	gbConstraints.weighty = weighty;
	
	container.add(c, gbConstraints);

}

public static void main(String[] args)
	{

	profitEstimator PE = new profitEstimator();

	}
}


The only thing I've got to do now is figure out why it doesn't like
animalTable.setFillsViewportHeight(true);



and I'm good to go. I don't really think that little bit of code in the above snippet matters to my code overall, but it's still something I'd like to figure out. I got some tutoring finally (they had an opening) and it's helped me out with this as well. Now the only thing I have to do is implement the table on a panel and attach that panel to the main panel and the main panel is already attached to the frame. Thanks for the advice with the different panels, it's made things so much easier. If you see anything in my code that could be made clearer or simplified, feel free to let me know, you guys have been a huge help!
Was This Post Helpful? 0
  • +
  • -

#12 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: capstone project

Posted 21 January 2010 - 01:12 PM

View Postnick2price, on 4 Jan, 2010 - 02:27 PM, said:

As PBL stated, you need to place your table into a scrollpane. I do it like this. Declare the variables
   protected static final String [] columnNames = 
	{
		"<html><b><h2>POSITION</h2></b></html>", 
		"<html><b><h2>NATIONALITY</h2></b></html>", 
		"<html><b><h2>FIRST NAME</h2></b></html>",
		"<html><b><h2>LAST NAME</h2></b></html>",
		"<html><b><h2>TIME SET</h2></b></html>",
		
	};
	
	//some hardcoded data for JTable
	private Object[][] data = {
	{"FIRST", "", "", "",}, {"SECOND", "", "", "",},
	{"THIRD", "", "", "",}
	};
	
	private JTable table;
	private DefaultTableModel model;
	private JScrollPane scroller;


And then initialise them somewhere else, creating your model, applying this to your table, and then adding your table to a scroll pane.
model = new DefaultTableModel(data, columnNames);  //Using default Table Model for the JTable
table = new JTable(model);
table.putClientProperty("terminateEditonfocusLost", Boolean.TRUE);
table.setRowHeight(25);
table.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),("selectNextColumnCell")); //giving the enter key the same properties as Tab key
table.isCellEditable(false);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;  //Always show scrollpane
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
scroller = new javax.swing.JScrollPane(table, v, h);  //put JTable and scrollpane constants into scroller (JScrollPane)
scroller.setPreferredSize(new Dimension(630,380));
wemRightPanel.add(scroller, 0);



It likes everything I've implemented from this snippet, but I had a question for you. What is
		animalTable.add(scroller, 0);


the problem with this line of code. I'm told it needs and int, int?

This post has been edited by circuspeanuts: 21 January 2010 - 04:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1