Hi,
Unten haengt der Code meiner JTable an. Mein Problem ist, dass wenn ich im Float column die erste Zelle selektiere und einfach Zahlen eingabe automatisch der '5' ein Dezimalpunkt folgt obwohl ich keinen eingebe. Also z.B.: 5.4323
Ich will dass man den Dezimalpunkt explizit eingeben muss um ihn zu erhalten und nicht diesen Automatismus.
Ich habe uebrigens getColumnClass(...) ueberschrieben um den Typ einer Column zurueckzugeben.
[HIGHLIGHT="Java"]
@Override
public Class getColumnClass(int c) {
return columnTypes.get(c);
}
[/HIGHLIGHT]
[HIGHLIGHT="Java"]
package components;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SimpleTableDemo extends JPanel {
private boolean DEBUG = false;
public SimpleTableDemo() {
super(new GridLayout(1,0));
String[] columnNames = {"Integer Col", "Float Col"};
Object[][] data = {
{new Integer(1), new Float(5)},
{new Integer(2), new Float(7.5)},
};
MyTableModel model = new MyTableModel(data, columnNames, new Class[]{Integer.class, Float.class});
final JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(300, 100));
table.setFillsViewportHeight(true);
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
class MyTableModel extends DefaultTableModel {
public Vector<Class> columnTypes;
public MyTableModel(Object[][] data, String[] columnNames, Class[] columnTypes) {
super(data, columnNames);
this.columnTypes = new Vector<Class>(Arrays.asList(columnTypes));
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
@Override
public Class getColumnClass(int c) {
// Class<?> clazz = getValueAt(0, c).getClass();
// System.out.println(clazz);
// return clazz;
return columnTypes.get(c);
}
}
}
[/HIGHLIGHT]
Unten haengt der Code meiner JTable an. Mein Problem ist, dass wenn ich im Float column die erste Zelle selektiere und einfach Zahlen eingabe automatisch der '5' ein Dezimalpunkt folgt obwohl ich keinen eingebe. Also z.B.: 5.4323
Ich will dass man den Dezimalpunkt explizit eingeben muss um ihn zu erhalten und nicht diesen Automatismus.
Ich habe uebrigens getColumnClass(...) ueberschrieben um den Typ einer Column zurueckzugeben.
[HIGHLIGHT="Java"]
@Override
public Class getColumnClass(int c) {
return columnTypes.get(c);
}
[/HIGHLIGHT]
[HIGHLIGHT="Java"]
package components;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SimpleTableDemo extends JPanel {
private boolean DEBUG = false;
public SimpleTableDemo() {
super(new GridLayout(1,0));
String[] columnNames = {"Integer Col", "Float Col"};
Object[][] data = {
{new Integer(1), new Float(5)},
{new Integer(2), new Float(7.5)},
};
MyTableModel model = new MyTableModel(data, columnNames, new Class[]{Integer.class, Float.class});
final JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(300, 100));
table.setFillsViewportHeight(true);
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
class MyTableModel extends DefaultTableModel {
public Vector<Class> columnTypes;
public MyTableModel(Object[][] data, String[] columnNames, Class[] columnTypes) {
super(data, columnNames);
this.columnTypes = new Vector<Class>(Arrays.asList(columnTypes));
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
@Override
public Class getColumnClass(int c) {
// Class<?> clazz = getValueAt(0, c).getClass();
// System.out.println(clazz);
// return clazz;
return columnTypes.get(c);
}
}
}
[/HIGHLIGHT]