Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

jtable not displaying

Thread view: 
jon23d@gmail.com - 20 Jul 2006 22:52 GMT
I have a vector of String[] objects, each with a key and a value.  This
is contained in an Options class which an application uses to store
keys for different windows which it contains.  I would like to be able
to display the vector in a JTable for quick editing and display.  I
have another application which imports the Options package and
instantiates it like this:

try {
           options.open("/home/jon23d/test.save");
       } catch (FileNotFoundException ex) {
           ex.printStackTrace();
       } catch (IOException ex) {
           ex.printStackTrace();
       }

A key can be set by: options.set("key", "value");
and read by options.get("key") which throws NoSuchFieldException.

Everything is cool until I call options.showDialog().  When I do I get
a ton of errors that I'm not sure how to trace down.  They seem to be
coming from the portion of the code that displays or initializes the
JTable.  The errors are:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
[Ljava.lang.String;
       at
javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:621)
       at javax.swing.JTable.getValueAt(JTable.java:1902)
       at javax.swing.JTable.prepareRenderer(JTable.java:3907)
       at
javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2070)
       at
javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1972)
       at
javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1895)
       at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
       at javax.swing.JComponent.paintComponent(JComponent.java:742)
       at javax.swing.JComponent.paint(JComponent.java:1005)
       at javax.swing.JComponent.paintChildren(JComponent.java:842)
       at javax.swing.JComponent.paint(JComponent.java:1014)
       at javax.swing.JViewport.paint(JViewport.java:728)
       at javax.swing.JComponent.paintChildren(JComponent.java:842)
       at javax.swing.JComponent.paint(JComponent.java:1014)
       at javax.swing.JComponent.paintChildren(JComponent.java:842)
       at javax.swing.JComponent.paint(JComponent.java:1014)
       at javax.swing.JComponent.paintChildren(JComponent.java:842)
       at javax.swing.JComponent.paint(JComponent.java:1014)
       at javax.swing.JLayeredPane.paint(JLayeredPane.java:559)
       at javax.swing.JComponent.paintChildren(JComponent.java:842)
       at
javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4970)
       at
javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4916)
       at javax.swing.JComponent.paint(JComponent.java:995)
       at
java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
       at
sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
       at
sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
       at java.awt.Container.paint(Container.java:1709)
       at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
       at
sun.awt.X11.XRepaintArea.paintComponent(XRepaintArea.java:56)
       at sun.awt.RepaintArea.paint(RepaintArea.java:224)
       at
sun.awt.X11.XComponentPeer.handleEvent(XComponentPeer.java:630)
       at java.awt.Component.dispatchEventImpl(Component.java:4031)
       at java.awt.Container.dispatchEventImpl(Container.java:2024)
       at java.awt.Window.dispatchEventImpl(Window.java:1774)
       at java.awt.Component.dispatchEvent(Component.java:3803)
       at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
       at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
       at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
       at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
       at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
       at
java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

And the Options code is:

package options;

import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.util.Vector;

public class Options extends JFrame implements Serializable {

   private Vector preferences;
   private String fileName = null;

   public Options() {
       preferences = new Vector(10);
       setupGui();
   }

   public Options(int defaultSize) {
       preferences = new Vector(defaultSize);
       setupGui();
   }

   public void open(String fileName) throws FileNotFoundException,
IOException {
       FileInputStream in = new FileInputStream(fileName);
       ObjectInputStream objIn = new ObjectInputStream(in);
       try {
           Vector tempList = (Vector)objIn.readObject();
           preferences.clear();
           preferences.addAll(tempList);

       } catch (ClassNotFoundException ex) {
           throw new FileNotFoundException();
       }
       this.fileName = fileName;
   }

   public void save(String fileName) throws IOException {
       writeFile(fileName);
   }

   public void save() throws NoFileSpecifiedException, IOException {
       if (fileName != null) {
           writeFile(fileName);
       } else {
           throw new NoFileSpecifiedException();
       }
   }

   private void writeFile(String fileName) throws IOException {
       FileOutputStream out = new FileOutputStream(fileName);
       ObjectOutputStream objOut = new ObjectOutputStream(out);
       objOut.writeObject(preferences);
       objOut.flush();
   }

   public String get(String key) throws NoSuchFieldException {
       int index;
       try {
           index = getKeyIndex(key);
           String[] keyPair = (String[])preferences.get(index);
           return keyPair[1];
       } catch (NoSuchFieldException ex) {
           throw new NoSuchFieldException();
       }

   }

   public void set(String key, String value) {
       try {
           remove(key);
       } catch (NoSuchFieldException ex) {
           // no need to catch, if key does not exist then
           // continue and set the new key
           ;
       }
       String[] keyPair = {key, value};
       preferences.add(keyPair);
   }

   public void remove(String key) throws NoSuchFieldException {
       int keyIndex = getKeyIndex(key);
       preferences.remove(keyIndex);
   }

   private int getKeyIndex(String key) throws NoSuchFieldException {
       int index = -1;

       if (!preferences.isEmpty()) {
           for (int i = 0; i <= preferences.size()-1; i++) {
               String[] keyPair = (String[])preferences.get(i);
               if (keyPair[0].equals(key)) { index = i; }
           }
       }

       if (index != -1) {
           return index;
       } else {
           throw new NoSuchFieldException();
       }
   }

   public void showDialog(boolean modal) {
       this.setVisible(true);
   }

   public void showDialog() {
       showDialog(true);
   }

   private void setupGui() {
       setTitle("Options");
       setLocation(300, 300);
       setSize(300,300);

       setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

       Container contentPane = getContentPane();
       Vector colNames = new Vector();
       colNames.add(new String[] {"Key", "Value"});

       JTable keyPairsTable = new JTable(preferences, colNames);
       JScrollPane scrollPane = new JScrollPane(keyPairsTable);
       //keyPairsTable.setPreferredScrollableViewportSize(new
Dimension(290, 250));
       contentPane.add(scrollPane);
   }
}

Any ideas???  Thanks!
Oliver Wong - 20 Jul 2006 23:01 GMT
[...]
> Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
[...]
> public class Options extends JFrame implements Serializable {
>
>    private Vector preferences;
[...]
>    public void set(String key, String value) {
[...]
>        String[] keyPair = {key, value};
>        preferences.add(keyPair);
>    }
[...]
>    private void setupGui() {
[...]
>        Vector colNames = new Vector();
>        colNames.add(new String[] {"Key", "Value"});
>
>        JTable keyPairsTable = new JTable(preferences, colNames);
[...]

   Next time try to strip your code as much as possible, while still
reproducing the problem.

   The problem is that the constructor of JTable expects a Vector of Vector
as its first argument, but you've provided a Vector of Array of String.

   Consider implementing a TableModel; it'll be less error prone, I think.

   - Oliver
jon23d@gmail.com - 20 Jul 2006 23:20 GMT
Thank you Oliver,

When I first wrote this class I used an ArrayList, until I found out
that the default table model will only accept a Vector.  Are you Saying
that my preferences vector needs to look like this:

Vector {
 Vector {String Key, String Value},
 Vector {String Key, String Value}...
}

> [...]
> > Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
[quoted text clipped - 26 lines]
>
>     - Oliver
Oliver Wong - 20 Jul 2006 23:23 GMT
> Thank you Oliver,
>
[quoted text clipped - 6 lines]
>  Vector {String Key, String Value}...
> }

   Yes, I think so. But as I said, it'd probably be less error prone if you
implement a Table Model.

   - Oliver
jon23d@gmail.com - 20 Jul 2006 23:26 GMT
I'm slowly getting there, from what I've read I agree.  I'm having a
little bit of difficulty understanding this concept though, otherwise I
probably would of just stuck with the ArrayList as it seems that if I
ever have 50,000 key-value pairs then it would present a slight
performance increase ! :)
> > Thank you Oliver,
> >
[quoted text clipped - 11 lines]
>
>     - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.