Results 1 to 11 of 11
- 05-03-2012, 04:48 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
ArrayStoreException with Object[][] array and ImageIcon
Hi everybody,
I've ran to an runtime error where I couldn't figure what's wrong with it. I'm trying to create a JTable (with custom DefaultCellRender of course) with a .gif pictures on 5th row.
a tiny part of my code;
I'm confused about this, it should have worked fine because I've tested it on another class and it ran fineJava Code:Object[][] = new Object[3][6]; //lot of coding using loops, etc -- all work fine except this ImageIcon icon = new ImageIcon("unmoved.gif"); tableinformation[0][5] = icon;
You can go ahead and test it yourself with code above. Don't forget to assign a .gif file inside the folder. You will see that it works just fine. I copied and pasted data object 2d array to my code where I have problem. It ran just fine with icons on the table. Whats a problem with assigning "tableinformation[0][5] = icon;"?Java Code:import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class TableIcon extends JFrame { public TableIcon() { ImageIcon aboutIcon = new ImageIcon("moved.gif"); ImageIcon addIcon = new ImageIcon("moved.gif"); ImageIcon copyIcon = new ImageIcon("unmoved.gif"); //String[] columnNames = {"Picture", "Description"}; String[] columnNames = {"1", "2", "3", "4", "5", "6"}; /*Object[][] data = { {aboutIcon, "About"}, {addIcon, "Add"}, {copyIcon, "Copy"}, }; */ Object[][] data = { {"about", "name", "size", "dd", "bb", aboutIcon}, {"abt", "nme", "e", "dd", "cc", copyIcon}, }; DefaultTableModel model = new DefaultTableModel(data, columnNames); JTable table = new JTable( model ) { // Returning the Class of each column will allow different // renderers to be used based on Class public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } }; table.setPreferredScrollableViewportSize(table.getPreferredSize()); JScrollPane scrollPane = new JScrollPane( table ); getContentPane().add( scrollPane ); } public static void main(String[] args) { TableIcon frame = new TableIcon(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setVisible(true); } }
- 05-03-2012, 05:35 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: ArrayStoreException with Object[][] array and ImageIcon
Consider posting brief (but compilable) code that does *not* wrok. That is code which, at runtime, does not do what you expect or intend. By "brief" I mean without code that is irrelevent to illustrating the problem. Less is better when you're hunting bugs: the same goes for dependencies (images etc).
If the problem isa runtime exception, post the whole thing along wkth the code to which it refers.
- 05-03-2012, 06:32 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
Re: ArrayStoreException with Object[][] array and ImageIcon
Alright, Ill post a method that returns JPanel
I hope this will be sufficient for you guys to understand my situation. I'm tired of having to explain my situation more than enough.Java Code://You will need import java.util.*; import javax.swing.*; import java.awt.*; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; //Attributes that you readers will need to know private ArrayList<String[]> data = new ArrayList<String[]>(); //To readers: please assume that there is data inside the ArrayList private DefaultTableModel tableModel; private JTable table; // public JPanel buildTable(){ JPanel tablePanel = new JPanel(new GridLayout(1,1)); String colHeader[] = {"#", "Name", "Size", "Host Directory", "Repository Directory", ""}; //Defining the table Object[][] tableinformation = new String[data.size()][6]; if(data.size() != 0){ for(int i = 0; i < data.size(); i++){ String[] information = data.get(i); for(int j = 0; j < 6; j++){ if(j == 0){ tableinformation[i][0] = Integer.toString((i+1)); }else if(j == 1){ tableinformation[i][1] = information[0]; }else if(j == 2){ tableinformation[i][2] = information[5]; }else if(j == 3 || j == 4){ tableinformation[i][j] = information[(j-2)]; }else if(j == 5){ ImageIcon icon = null; if(information[4].equals("")){ icon = new ImageIcon("unmoved.gif"); }else{ icon = new ImageIcon("moved.gif"); } tableinformation[i][5] = icon; //THIS DOESNT WORK RIGHT - THROWS ARRAYSTOREEXCEPTION EXCEPTION } } }//End of for loop }//End of if statement //Defining the table ends DefaultTableModel tableModel = new DefaultTableModel(tableinformation, colHeader); //JTable Formatting table = new JTable(tableModel) {public Class getColumnClass(int column){ return getValueAt(0, column).getClass();}}; //JTable Formatting End JTableHeader header = table.getTableHeader(); JScrollPane pane = new JScrollPane(table); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); tablePanel.add(pane); return tablePanel; }//End of buildTable() method
EDIT: You might need this for testing - add this to anyplace like constructor
Java Code:String[] information = {"AppName", "AppDirectory", "RepoDirectory", "SL", "", "1.23GB"}; data.add(information);Last edited by X75TIGER75X; 05-03-2012 at 06:13 PM.
- 05-03-2012, 07:00 AM #4
Re: ArrayStoreException with Object[][] array and ImageIcon
What type of array do you create on line 11? Can that array hold an ImageIcon?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-03-2012, 02:01 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
Re: ArrayStoreException with Object[][] array and ImageIcon
ArrayList, it contains raw String data. No ImageIcons on that arraylist. You can see between line 12 and 33, program loads and translates the data into Object[][] which will be used on JTable. That Object[][] array is supposed to be able to hold ImageIcon. In my example code, it worked beautifully, but my current code fails at runtime when I try to assign ImageIcon to Object[][]. That what puzzles me.
- 05-03-2012, 02:12 PM #6
Re: ArrayStoreException with Object[][] array and ImageIcon
For testing we need a program that compiles, executes and shows the problem. Partial code is useless. see post#2
Also it would help if you posted the full text of the error messages.
Did you read the API doc the the exception you are getting? Its pretty clear on what your problem is.Last edited by Norm; 05-03-2012 at 02:17 PM.
If you don't understand my response, don't ignore it, ask a question.
- 05-03-2012, 06:09 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
Re: ArrayStoreException with Object[][] array and ImageIcon
Copy/Paste of Error Code in console
----jGRASP exec: java SoftwareMover
Exception in thread "main" java.lang.ArrayStoreException: javax.swing.ImageIcon
at SoftwareMover.buildTable(SoftwareMover.java:160)
at SoftwareMover.buildUI(SoftwareMover.java:238)
at SoftwareMover.<init>(SoftwareMover.java:45)
at SoftwareMover.main(SoftwareMover.java:263)
----jGRASP wedge2: exit code for process is 1.
This caused the error
In attempt to avoid any further confusions and questions, tableinformation is Object[][]. information is String[]. If you need more information, look at my runnable method code post above (it returns JPanel, this means you can build a quick JFrame and use add(buildTable()) method. It shouldn't be hard for you guys). It should provide you more than enough information about my situation. Now how do I solve this "tableinformation[i][5] = icon;"Java Code:ImageIcon icon = null; if(information[4].equals("")){ icon = new ImageIcon("unmoved.gif"); }else{ icon = new ImageIcon("moved.gif"); } tableinformation[i][5] = icon; //This is where Runtime Error points
- 05-03-2012, 06:11 PM #8
Re: ArrayStoreException with Object[][] array and ImageIcon
Did you read the API doc for the exception? It explains what the problem is.
If you have any questions about what it says, copy the doc here and ask you questions.
We're not paid for this. If you are too lazy to create a testing program, you'll be lucky to get much help.it shouldn't be hard for you guysLast edited by Norm; 05-03-2012 at 06:13 PM.
If you don't understand my response, don't ignore it, ask a question.
- 05-03-2012, 06:15 PM #9
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
Re: ArrayStoreException with Object[][] array and ImageIcon
Yes... I checked the API. It simply says "Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects." Believe me, I've done my homework, researching every possible solution and I cannot find it. This is why I asked a question on this forum to look for solution to this problem.
EDIT: Fine, heres tester program. You can copy and paste it in any blank .java and compile it.
EDIT2: And please try this template code. I used the template from this code. This code works fine. But my code above doesnt work.Java Code:import java.util.*; import javax.swing.*; import java.awt.*; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; public class TestGUI extends JFrame{ private ArrayList<String[]> data = new ArrayList<String[]>(); //To readers: please assume that there is data inside the ArrayList private DefaultTableModel tableModel; private JTable table; public TestGUI(){ String[] information = {"AppName", "AppDirectory", "RepoDirectory", "SL", "", "1.23GB"}; data.add(information); add(buildTable()); setSize(400,400); setLocation(200,200); setVisible(true); } public JPanel buildTable(){ JPanel tablePanel = new JPanel(new GridLayout(1,1)); String colHeader[] = {"#", "Name", "Size", "Host Directory", "Repository Directory", ""}; //Defining the table Object[][] tableinformation = new String[data.size()][6]; if(data.size() != 0){ for(int i = 0; i < data.size(); i++){ String[] information = data.get(i); for(int j = 0; j < 6; j++){ if(j == 0){ tableinformation[i][0] = Integer.toString((i+1)); }else if(j == 1){ tableinformation[i][1] = information[0]; }else if(j == 2){ tableinformation[i][2] = information[5]; }else if(j == 3 || j == 4){ tableinformation[i][j] = information[(j-2)]; }else if(j == 5){ ImageIcon icon = null; if(information[4].equals("")){ icon = new ImageIcon("unmoved.gif"); }else{ icon = new ImageIcon("moved.gif"); } tableinformation[i][5] = icon; //THIS DOESNT WORK RIGHT - THROWS ARRAYSTOREEXCEPTION EXCEPTION } } }//End of for loop }//End of if statement //Defining the table ends DefaultTableModel tableModel = new DefaultTableModel(tableinformation, colHeader); //JTable Formatting table = new JTable(tableModel) {public Class getColumnClass(int column){ return getValueAt(0, column).getClass();}}; //JTable Formatting End JTableHeader header = table.getTableHeader(); JScrollPane pane = new JScrollPane(table); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); tablePanel.add(pane); return tablePanel; }//End of buildTable() method public static void main(String[] args){ new TestGUI(); } }
Java Code:import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class TableIcon extends JFrame { public TableIcon() { ImageIcon aboutIcon = new ImageIcon("moved.gif"); ImageIcon addIcon = new ImageIcon("moved.gif"); ImageIcon copyIcon = new ImageIcon("unmoved.gif"); //String[] columnNames = {"Picture", "Description"}; String[] columnNames = {"1", "2", "3", "4", "5", "6"}; /*Object[][] data = { {aboutIcon, "About"}, {addIcon, "Add"}, {copyIcon, "Copy"}, }; */ Object[][] data = { {"about", "name", "size", "dd", "bb", aboutIcon}, {"abt", "nme", "e", "dd", "cc", copyIcon}, }; DefaultTableModel model = new DefaultTableModel(data, columnNames); JTable table = new JTable( model ) { // Returning the Class of each column will allow different // renderers to be used based on Class public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } }; table.setPreferredScrollableViewportSize(table.getPreferredSize()); JScrollPane scrollPane = new JScrollPane( table ); getContentPane().add( scrollPane ); } public static void main(String[] args) { TableIcon frame = new TableIcon(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setVisible(true); } }Last edited by X75TIGER75X; 05-03-2012 at 06:27 PM.
- 05-03-2012, 06:37 PM #10
Re: ArrayStoreException with Object[][] array and ImageIcon
What about the example with the API doc:attempt has been made to store the wrong type of object into an array of objects."
For example, the following code generates an ArrayStoreException:Your code parallels that exactly:Java Code:Object x[] = new String[3]; x[0] = new Integer(0);
Change the value given to tableinformationJava Code:Object[][] tableinformation = new String[data.size()][6]; .... tableinformation[i][5] = icon; // defined by ImageIcon icon
If you don't understand my response, don't ignore it, ask a question.
- 05-03-2012, 06:46 PM #11
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
How do I scan a photo from my scanner into an ImageIcon object
By fortwnty420 in forum Advanced JavaReplies: 6Last Post: 05-09-2013, 08:51 AM -
Is an object array considered to be an object?
By guest_user in forum New To JavaReplies: 1Last Post: 07-13-2011, 06:40 AM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Array of Object
By haiforhussain in forum New To JavaReplies: 1Last Post: 06-25-2008, 11:24 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks