jtable not displaying

J

jon23d

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!
 
O

Oliver Wong

[...]
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
 
J

jon23d

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}...
}

Oliver said:
[...]
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
 
O

Oliver Wong

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}...
}

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

- Oliver
 
J

jon23d

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 ! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top