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