Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 29th, 2012, 10:05 PM
Deptawa923 Deptawa923 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 Deptawa923 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 47 sec
Reputation Power: 0
Homework - Data entered overwrites old data - only 1 line shows at a time

My team and I are creating a program that has a user enter fundraiser information. We have created a GUI that has the user enter a full name, charity name, and amount provided.

A "List" option then pops up and displays the data that was entered.

Here's were the problem begins. If you try and enter more than 1 person's information, the program will delete the old info and list the new info. This means that in our list only 1 row is shown at any given time.

Can anyone please take a look at the code and see what we've done wrong?

I'm sure it's something silly - but any help would be appreciated !


package fundraiser;


import java.awt.*;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;

import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;


public class FundRaiser extends JFrame
{
private JLabel nameMessage; // A message asking for the donor's name
private JLabel charityMessage; // A message asking the user for the name of the charity
private JLabel pledgeMessage; // The message telling the user to enter a pledge amount

private JTextField donorName; // Used to hold user input
private JTextField charityName; // Used to hold the charity name
private JTextField pledgeAmount; // Used to hold the pledge amount

private String name; // The donor's name
private String charity; // The charities name
private String pledge; // The pledged amount

private JButton enterButton; // Enters the user input into the file
private JButton listButton;
private JButton exitButton; // Exits the program
// GUI Constructor
public FundRaiser()
{
// Call the JFrame constructor
super("Learning Team B: Fund Raiser Program");

// Set the size of the window
setSize(440, 150);

// Specify what happens when the close button is clicked
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create the labels, text fields, and buttons
nameMessage = new JLabel("Enter the donors full name: ");
donorName = new JTextField(20);

charityMessage = new JLabel("Enter the name of the charity: ");
charityName = new JTextField(20);

pledgeMessage = new JLabel("Enter the amount pleged: ");
pledgeAmount = new JTextField(20);

enterButton = new JButton("Enter");
enterButton.setMnemonic(KeyEvent.VK_E);
enterButton.setToolTipText("Click here to enter a pledge");

listButton = new JButton("List");
listButton.setMnemonic(KeyEvent.VK_L);
listButton.setToolTipText("Click here to view a table of donor info");

exitButton = new JButton("Exit");
exitButton.setMnemonic(KeyEvent.VK_X);
exitButton.setToolTipText("Click here to exit the program");

// Add an action listener to the buttons
enterButton.addActionListener(new EnterButtonListener());
listButton.addActionListener(new ListButtonListener());
exitButton.addActionListener(new ExitButtonListener());

// Set the layout style
setLayout(new FlowLayout());

// Add the labels, text fields, and buttons to the panel
add(nameMessage);
add(donorName);
add(charityMessage);
add(charityName);
add(pledgeMessage);
add(pledgeAmount);
add(enterButton);
add(listButton);
add(exitButton);

setVisible(true);
}

public class EnterButtonListener implements ActionListener
{

public void actionPerformed(ActionEvent e)
{

// Retrieve the input from the user
name = donorName.getText();
charity = charityName.getText();
pledge = pledgeAmount.getText();

}

}

public class ListButtonListener implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
String[][] rowAndColumn = {
{name, charity, pledge},

};
// defines the header
String[] header = {"Name", "Charity", "Pledge Amount"};
// build the GUI
FundRaiser twm = new FundRaiser(rowAndColumn, header);
}
}
Team model;

// constructor that will display a JTable based on elements received as arguments
FundRaiser(Object[][] obj, String[] header)
{
super("Donor List");

// JPanel to show the JTable
JPanel panel = new JPanel(new BorderLayout());
// This is the constructor of our JTable model
model = new Team(obj, header);
// This is the table from that model
JTable table = new JTable(model);
panel.add(new JScrollPane(table));
add(panel); // This adds panel to frame and then displays it

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
pack();
}


// Create the exit button listener
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}


public static void main(String[] args)
{
new FundRaiser();


}

// The class "Team" extends the AbstractTableModel. This allows us to display a JTable

class Team extends AbstractTableModel {

// Creates an ArrayList for all items within our program
ArrayList<Object[]> al;
// This adds the header(s)
String[] header;

// This code is for the constructor
Team(Object[][] obj, String[] header)
{
// saves the header
this.header = header;
al = new ArrayList<Object[]>();
// This section copies the rows into the ArrayList
for(int i = 0; i < obj.length; ++i)
al.add(obj[i]);
}
// method that needs to be overload. The row count is the size of the ArrayList
public int getRowCount()
{
return al.size();
}

// method that needs to be overload. The column count is the size of our header
public int getColumnCount()
{
return header.length;
}

// method that needs to be overload. The object is in the arrayList at rowIndex
public Object getValueAt(int rowIndex, int columnIndex)
{
return al.get(rowIndex)[columnIndex];
}

// This is a method to return the column name
public String getColumnName(int index)
{
return header[index];
}

// This method will add new line(s) to the table
void add()
{
String[] str = new String[3];
str[0] = donorName.getText();
str[1] = charityName.getText();
str[2] = pledgeAmount.getText();
al.add(str);
//This informs the GUI table of data changes
//fireTableDataChanged();
}

}
}

Reply With Quote
  #2  
Old October 29th, 2012, 10:59 PM
slink's Avatar
slink slink is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 73 slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 17 h 10 m 20 sec
Reputation Power: 14
I suspect your fundamental problem is this line in ListButtonListener:

Code:
FundRaiser twm = new FundRaiser(rowAndColumn, header);


It appears to be recreating the FundRaiser, which is not a good approach. This class is the principal class called by main and it is not usual to create more of these in flight. You possibly need to consider having two separate classes: FundRaiser and DonorList.

I would also encourage you to separate your varoius classes into separate files (except perhaps for the ActionListener implementations). If you do this, it should become clearer what the problem is; as it is I can't be sure, sorry.

Reply With Quote
  #3  
Old October 29th, 2012, 11:49 PM
Deptawa923 Deptawa923 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 Deptawa923 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 47 sec
Reputation Power: 0
Don't be sorry! This is some great advice here. I'll take a look a the program some more and see what changes I can make and maybe figure it out with the info you provided


Quote:
Originally Posted by slink
I suspect your fundamental problem is this line in ListButtonListener:

Code:
FundRaiser twm = new FundRaiser(rowAndColumn, header);


It appears to be recreating the FundRaiser, which is not a good approach. This class is the principal class called by main and it is not usual to create more of these in flight. You possibly need to consider having two separate classes: FundRaiser and DonorList.

I would also encourage you to separate your varoius classes into separate files (except perhaps for the ActionListener implementations). If you do this, it should become clearer what the problem is; as it is I can't be sure, sorry.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Data entered overwrites old data - only 1 line shows at a time

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap