10 Replies - 398 Views - Last Post: 20 June 2013 - 12:45 PM Rate Topic: -----

#1 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Displaying on a JTable Multiple Dimension Arrays

Posted 19 June 2013 - 08:53 AM

I am creating this program for my class it needs to be able to display information I gather up on an array table.
I thought I had it down packed but I keep getting an java.lang.ArrayIndexOutOfBoundsException but I can't figure out why. What am I doing wrong?



import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Charity extends JFrame
{

	// variables necessary
	private JTable displayTable;
	
	// Constants for size.
	private final int WIDTH = 400;
	private final int HEIGHT = 200;
	
	// main constructor
	public Charity(Object[][] data, Object[] colNames)
	{
		// Specify an action for the close button.
		 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		// Create a JTable with the results.
		 displayTable = new JTable(data, colNames);
		//displayTable.addColumn(null);
		
		// Put the table in a scroll pane.
		JScrollPane scrollPane = new JScrollPane(displayTable);
		
		// Add the table to the content pane.
		add(scrollPane, BorderLayout.CENTER);
		
		// Set the size and display.
		setSize(WIDTH, HEIGHT);
		setVisible(true);

	}
	// main method
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 // Table data
		String[] colNames = {"Donor Name", "Charity Name", "Pledged Amount",}; // Column names
		ArrayList<String> tableData = new ArrayList<String>();
		String donorName, charityName, pledgedAmount;
		boolean ok = true;
		
		
		do
		{
			
			donorName = JOptionPane.showInputDialog(null, "What is your name?");
			charityName= JOptionPane.showInputDialog(null, "What is your Charity's Name?");
			pledgedAmount = JOptionPane.showInputDialog(null, "Amount Pledging?");
			tableData.add(donorName +  charityName +  pledgedAmount);
			
			
					
				String answer = JOptionPane.showInputDialog(null, "Would you like to add more Y or N?");
				if(answer.equalsIgnoreCase("n")){
					ok= false;}
			
		}while(ok);
			
		   // to make it format to the object of [][]
			String[][] tableRow = {{tableData.toString()}};
		new Charity(tableRow, colNames);
		
	  }// end main
	}







Is This A Good Question/Topic? 0
  • +

Replies To: Displaying on a JTable Multiple Dimension Arrays

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,113
  • Joined: 27-December 08

Re: Displaying on a JTable Multiple Dimension Arrays

Posted 19 June 2013 - 08:54 AM

Please post your stack trace.
Was This Post Helpful? 0
  • +
  • -

#3 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Re: Displaying on a JTable Multiple Dimension Arrays

Posted 19 June 2013 - 09:01 AM

Quote

Please post your stack trace.


Stack Trace
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
	at javax.swing.JTable$1.getValueAt(Unknown Source)
	at javax.swing.JTable.getValueAt(Unknown Source)
	at javax.swing.JTable.prepareRenderer(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
	at javax.swing.plaf.ComponentUI.update(Unknown Source)
	at javax.swing.JComponent.paintComponent(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JViewport.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JLayeredPane.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
	at javax.swing.RepaintManager.paint(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
	at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
	at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
	at java.awt.Container.paint(Unknown Source)
	at java.awt.window.paint(Unknown Source)
	at javax.swing.RepaintManager$3.run(Unknown Source)
	at javax.swing.RepaintManager$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.access$1000(Unknown Source)
	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$200(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)


I think it looks neater in the code box

This post has been edited by DADDYCARDONA: 19 June 2013 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#4 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Re: Displaying on a JTable Multiple Dimension Arrays

Posted 20 June 2013 - 07:43 AM

Is this what you was looking for?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,113
  • Joined: 27-December 08

Re: Displaying on a JTable Multiple Dimension Arrays

Posted 20 June 2013 - 07:57 AM

I think your problem comes from the fact that you are concatenating the inputs for a single person into one String. So you'll only get a single row of Strings. You really want to have a JTable TableModel where you organize the data with a List<Donation> objects, where Donation is a class you create. The way you are concatenating Strings is bad form, as it makes it hard to access the data in an efficient manner.

The more quick and dirty solution is to actually use a String[][] or List<String[]>. Each piece of data should be an element in the array.
Was This Post Helpful? 0
  • +
  • -

#6 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Re: Displaying on a JTable Multiple Dimension Arrays

Posted 20 June 2013 - 08:15 AM

View Postmacosxnerd101, on 20 June 2013 - 09:57 AM, said:

I think your problem comes from the fact that you are concatenating the inputs for a single person into one String. So you'll only get a single row of Strings. You really want to have a JTable TableModel where you organize the data with a List<Donation> objects, where Donation is a class you create. The way you are concatenating Strings is bad form, as it makes it hard to access the data in an efficient manner.

The more quick and dirty solution is to actually use a String[][] or List<String[]>. Each piece of data should be an element in the array.

Can you give me an example of that? I am confused I'm still new to this but if I see it I can make more sense of it
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,113
  • Joined: 27-December 08

Re: Displaying on a JTable Multiple Dimension Arrays

Posted 20 June 2013 - 08:21 AM

I would just extend AbstractTableModel and implement the methods according to the TableModel interface.

To give you an idea of the process. Obviously there are other methods you'll need to take into account to handle things like the column names. The docs are a good resource on that, and the JTable docs tell you how to handle the TableModel.
class DonationTableModel extends AbstractTableModel{

    private List<Donation> donations;

    public Object getValueAt(int row, int column){
        Donation d = donations.get(row);
        switch(column){
           case 0: return donations.getName();
           case 1: return donations.getCharity();
           case 2: return donations.getAmount();
        }
    }

}


Was This Post Helpful? 2
  • +
  • -

#8 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Re: Displaying on a JTable Multiple Dimension Arrays

Posted 20 June 2013 - 08:38 AM

Thanks again I would look into this tomorrow I have work homework today. I get confused still but I will figure it out
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,113
  • Joined: 27-December 08

Re: Displaying on a JTable Multiple Dimension Arrays

Posted 20 June 2013 - 08:39 AM

Glad I could help! Good luck!
Was This Post Helpful? 0
  • +
  • -

#10 riperator  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 47
  • Joined: 19-May 13

Re: Displaying on a JTable Multiple Dimension Arrays

Posted 20 June 2013 - 10:29 AM

Are you doing this for Athene :) ?
Was This Post Helpful? 0
  • +
  • -

#11 DADDYCARDONA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 04-July 10

Re: Displaying on a JTable Multiple Dimension Arrays

Posted 20 June 2013 - 12:45 PM

View Postriperator, on 20 June 2013 - 12:29 PM, said:

Are you doing this for Athene :)/> ?

No I am doing this for my class I understood mist of it but this part was confusing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1