J. Noah

Greenhorn
+ Follow
since Nov 15, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by J. Noah

You could also try to parse with the standard methods, but put a try-catch clause around it. If you can't parse using a standard method, then try your custom parse case.
15 years ago
Depending on the app, another thing you could do is create a singleton for the scorekeeper or mainwindow if that's applicable in this case. That way you'd be able to access it elsewhere while still preserving some of the ideal loose coupling properties.
15 years ago
I actually don't think that's correct - one pipe is for bitwise inclusive OR. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
15 years ago
If you're getting a proper java.util.Date object, you can be sure that it is legitimate. IE the day of the month won't be greater than 31, the year won't be negative, etc. You can always do validation, but unfortunately the methods in java.util.Date are deprecated so you'll have to pass the Date into a Calendar object and then use the get methods to determine whether the day, month, year, etc. are within your valid ranges.
15 years ago
Insertion sort is a simple place to start...
15 years ago
Another choice aside from XML would be to use JSON.
15 years ago
I'm not sure if this is all the code or not, but it's unclear where the actual drawing takes place. You do set parameters for all the body parts, but you don't actually draw them on the screen. IE you have a bunch of ellipses, but you haven't drawn them. That aside, the way listeners work is as follows. A JFrame for example, can "listen" to mouse events. Such behavior isn't exactly automatic though as the frame doesn't know what to do with any events it would receive. As such, you need to add a MouseListener to the Frame. MouseListener is an interface which means that you need to create your own class which implements this interface. To simplify coding, you could extend a MouseAdapter instead of implementing a MouseListener as MouseAdapter has null ops for ease of typing. In the mousePressed code, you would tell your snowman to melt based on the mouse's coordinates. You should be able to google "how to use MouseListeners" to get more details.
15 years ago
You could set the TableCellRenderer for that column to be a custom renderer in which you'd need to override:

public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column){
DateFormat df = new SimpleDateFormat("yyyyMMdd");
this.setText(df.parse(new Date((Long)value)));
return this;
}
[ November 15, 2008: Message edited by: J. Noah ]
15 years ago
If you're trying to validate data in a table, you could use something like this class:

import org.apache.log4j.Logger;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

/**
* Implements a cell editor that uses a text field to validate and edit Double values.
*/
public class DoubleEditor extends DefaultCellEditor {

private static final Logger log = Logger.getLogger(DoubleEditor.class);

private JTextField textField;
private double min, max;
private static final Color RED_COLOR = new Color(240, 175, 175);
private static final Border BLACK_BORDER = new LineBorder(Color.BLACK);

public DoubleEditor(double min, double max) {
this(new JTextField(), min, max);
}

public DoubleEditor(JTextField jt, double min, double max) {
super(jt);
textField = (JTextField) getComponent();
this.min = min;
this.max = max;

textField.setHorizontalAlignment(JTextField.TRAILING);

textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
textField.getActionMap().put("check", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
log.debug("text:" + textField.getText());
textField.postActionEvent();
}
});
}

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
JTextField textField = (JTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);
textField.setText(value == null ? null : value.toString());
textField.setBackground(Color.WHITE);
textField.setBorder(BLACK_BORDER);
return textField;
}

public Object getCellEditorValue() {
JTextField ftf = (JTextField) getComponent();
return ftf.getText() == null || ftf.getText().trim().equals("") ? null : Double.parseDouble(ftf.getText());
}

/**
* Override to check whether the edit is valid, setting the value if it is and complaining if it isn't. If it's OK
* for the editor to go away, we need to invoke the superclass's version of this method so that everything gets
* cleaned up.
*/
public boolean stopCellEditing() {
JTextField textField = (JTextField) getComponent();
if (textField.getText().trim().equals("")) { // if it's the empty string, set it to null
textField.setText(null);
return super.stopCellEditing();
} else if (textField.getText() == null || isValid(textField.getText())) { // otherwise, if it's valid, set it

textField.setBackground(Color.WHITE);
return super.stopCellEditing();
} else { // otherwise, it's not valid
textField.selectAll();
textField.setBackground(RED_COLOR);
return false;
}
}

private boolean isValid(String text) {
try {
double num = Double.parseDouble(text);
return num >= min && num < max;
} catch (NumberFormatException nfe) {
log.debug("bad number: " + text);
}

return false;
}

}
[ November 15, 2008: Message edited by: J. Noah ]
15 years ago