Sam Zheng

Ranch Hand
+ Follow
since Nov 29, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Sam Zheng

Jose,
Thanks! Adding the line

if(value==null) setText("");


does fix the problem. I am curious to know why the original code produces the wierd behavior, why the value I entered is displayed all over the cells.
Would you shed some light?
21 years ago
I have a TableCellRenderer problem that seems to be very wierd. I seached this forum and couldn't find any discussion about this behavior. Please have a look and let me know if you have any solution.
In the following code, all the table cell contains Double.class object. It has three columns. In the second column (column = 1), I want the double to be displayed in scientific format. For example, 12345 as 1.234E4. The problem I have is that once I enter one number in any of the table, the number is displayed all over the table! Say I enter 100 at cell (0,1),
after I hit enter. The number is correctly displayed as 1E2. Then I click anywhen within the table, this number (1E2) is propagated all over the table! That is crazy! Here is the complete code:

import javax.swing.*;
import java.awt.*;
import java.text.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class Test extends JFrame {
JTable table;
Object data[][];
String[] columnNames = {"first", "second", "third"};
MyTableModel tableModel;

/** Creates new Test */
public Test() {
data = new Object[100][3];
table = new JTable(new MyTableModel(data, columnNames));
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setDefaultRenderer(Double.class, new ScientificDoubleRenderer());
JScrollPane scrollPane = new JScrollPane(table);

getContentPane().add(scrollPane);
setBounds(100, 100, 300, 400);
//setVisible(true);
}

public static void main(String argv[]){
Test t = new Test();
t.setVisible(true);
}

class MyTableModel extends DefaultTableModel{
String column[];
Object data[][];

public MyTableModel(Object dat[][], String[] col){
super(dat, col);
column = col;
data = dat;
}

public Class getColumnClass(int c){
return Double.class;
}
}

class ScientificDoubleRenderer extends JLabel implements TableCellRenderer{
DecimalFormat formatSci;
DecimalFormat formatDbl;
String patternSci = "0.###E0";
String patternDbl = "#######.###";
public ScientificDoubleRenderer(){
formatSci = new DecimalFormat(patternSci);
formatDbl = new DecimalFormat(patternDbl);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column){
if (value != null) {
if(column == 1)
setText(formatSci.format(((Double)value).doubleValue()));
else
setText(formatDbl.format(((Double)value).doubleValue()));
}
return this;
}
}
}
21 years ago
I have a group of components on a JTabbedPane. When the user clicks any one of the components, I want to perform certain data validation before displaying the new component on the JTabbedPane. Is there any listener that I can add to the JTabbedPane for me to accomplish this? Thanks for help!
Sam
21 years ago
I have a JTable whose cell elements are Double. The default behavior of the table is that when you click the cell and start type, the new text starts from the end of previous text. For example, if one cell has a value of "0.001", when I click this cell and type "2", the cell becomes "0.0012".
What I want is that when I click a new cell and type a number, the new value just over-writes the old value, i.e., "2" overwrites "0.001" so that new value on the cell becomes "2".
Would anyone give a suggestion?
Thanks!
Sam
21 years ago
Thanks, Nathan.
It works. I don't know why my previous approach didn't work. Looking at the implementation, I find out that my table cell renderer implements
TableCellRenderer interface, while yours extends DefaultTableCellRenderer.
Maybe this makes all the difference.
Sam
21 years ago
Nathan,
When I first tried it, I did use what you suggested below:
myTable.setDefaultRenderer( Double.class, new MyTableCellRenderer() );
Since it didn't work, I registered it with Object hoping that it may. But it didn't either. BTW, in my TableModel, the getColumnClass(int col) does return Double.class.
It really baffles me that the MyTableCellRenderer() never get called! I set a breakpoint at the first valid statement of this class, and it never stops there. Any other thoughts?
I will try Abhik's suggestion later to see whether it works.
Any other suggestions are welcome and appreciated!
Sam
21 years ago
Thanks Nathan!
I have tried the following without success. Could you please help?
First, I implement a tablecellrenderer as below:
public class MyTableCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
TableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer();
Component c = defaultTableCellRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(((Double)value).doubleValue() > 10)
c.setForegroung(Color.red);
return c;
}
}
Then, in my JTable, I call the following:
myTable.setDefaultRenderer(Object.class, new MyTableCellRenderer());
Once I compile and run, I found out that the function getTableCellRendererComponent in MyTableCellRenderer is never called whenever I type in any number on the table.
What went wrong?
Thanks!
Sam
[ July 26, 2003: Message edited by: Sam Zheng ]
21 years ago
I need to add a conditional check for data entered in a JTable. Whenever user enters a data that is bigger than a specified one, the corresponding table cell changes color.
I am thinking to implement the conditional check in the setValueAt() method of TableModel as this method is called whenever data is entered. However, from the setValueAt() method in TableModel, I don't have a way to control the appearance of the table.
Please help!
Sam
21 years ago
It seems that the default JTable allows user to drag the column and change the sequence of the column on the table. For example, if I have a JTable with column in the following sequence
A B C D
Once I create such a table, I will be able to drag the column and re-shuffle its sequence on the fly, such as
B D C A
How can I turn off this feature so that the table column sequence will be fixed, cannot be changed by users?
Thanks!
Sam
21 years ago
I have a JTable with one specific header. I want to change the header to another one in the program when a specific condition is satisfied. How can I do that?
Thanks!
Sam
21 years ago
Under the java's look and feel, the JButton looks silly. It is just a small area with an etch boundary. How to change the look and feel of the JButton so that it really looks like a button, with the look of a "raised" appearance?
Thanks!
Sam
21 years ago
I have created a GUI which consists of a JFrame and a menubar. The menu bar has several menus which corresponds to different views (JPanels). Each view displays a number of JTextfield and table. The data in each view can be expressed in either metric unit or english unit. When user selects a menuitem "JRadioButtonMenuItem" (e.g. SI Unit) contained in a unit selection menu, the data in each view should be changed accordingly to reflect the new unit. However, if the display is already in SI Unit and the SI Unit menu item is clicked, then the view remains the same.
I planned to implement this unit change feature by adding an item listener on the JRadioButtonMenuItem. When user changes the unit selection, the listener method (itemStateChanged) will update and repaint ALL views within this GUI to reflect the new unit.
The problem with this approach is that it is too costly. Remember at each instance, the GUI only shows one view, so it seems to me that I should only update and repaint the view on display. For other views, the update and repaint should be done when user switches view. Am I right? If yes, how can I update the data on a hidden view once user switches view? If no, what would be a better approach? I have a hunch that this has something to do with spontaneous repainting. But my knowledge on this aspect is quite limited.
Please help!
Sam
21 years ago
Paul,
You are incredible! It works! What was I thinking :-)
Thanks!
Sam
[ June 27, 2003: Message edited by: Sam Zheng ]
21 years ago
It seems none of you Swing Gurus have reviewed my dirty code above. In fact, this is an interesting problem. Again, the program works fine in accepting user input. The problem is in setting the value on the table within the program by clicking the "Test" button. I found out that if I set all fields on the table model as Object or String, it doesn't have any problem either. The problem occurs only when I set the column as anyting other than Object and String, such as Double, Integer.
Any suggestions are appreciated!
Sam
21 years ago
The suggestion on using table header renderer is good except it wraps the text based on letter instead of word, i.e., it wraps the text like
measured d
istance
How can I make it wraps text based on word such as
measured
distance
BTW, the use of HTML tag is great. It solves my problem. But I am still curious about using the renderer.
Thanks!
Sam.
21 years ago