I have defined an abstract table model but one of the columns should house date values as dd/mm/yyyy format not sure how to do this.
I have a external global file and have hard coded the dates as dd/mm/yyyy.
How can I define this column within my abstract table model so that开发者_如何学C to only allow only dates having dd/mm/yyyy format.
public class OptraderGlobalParameters
{
public static boolean DEBUG = true; //Set DEBUG = true for Debugging
/*=========================*/
/*Table Array For Dividends*/
/*=========================*/
public static String[] columnNames
= {"Date",
"Dividend",
"Actual",
"Yield (%)"
};
public static Object[][] data
= { {"dd/mm/yyyy", new Double(5), new Boolean(false), new Boolean(false)},
{"dd/mm/yyyy", new Double(5), new Boolean(false), new Boolean(false)},
{"dd/mm/yyyy", new Double(5), new Boolean(false), new Boolean(false)},
{"dd/mm/yyyy", new Double(5), new Boolean(false), new Boolean(false)},
{"dd/mm/yyyy", new Double(5), new Boolean(false), new Boolean(false)},
};
}
You should not limit your user just because of the format you store the dates in. Have your table model parse user input into a Date object and just use a SimpleDateFormat to format the Date to match your file storage.
If you need some validation look at cell editors or don't allow the user to move to the next step if they enter bad data.
Have a look at the Oracle tutorial. http://download.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html It should be pretty clear.
To extend what Oleg wrote, to give you the most flexibility you should write your class along the lines as:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class OptraderInfo {
private Date date;
private Double dividend;
public static final String DEFAULT_DATE_FORMAT = "dd/MM/yyyy";
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
public String getDateFormatted() {
return DATE_FORMAT.format(date);
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public void setDate(String date) throws ParseException {
this.date = DATE_FORMAT.parse(date);
}
public void setDate(String date, String format) throws ParseException {
this.date = new SimpleDateFormat(format).parse(date);
}
public Double getDividend() {
return dividend;
}
public void setDividend(Double dividend) {
this.dividend = dividend;
}
}
Storing the actual Date as a date in the info class allows you to sort and other types of manipulations as needed. It is never a good idea to store dates as strings.
It sounds like you want to make sure that whenever someone adds a row to your table, or modifies a row, they can only use dd/mm/yyyy format, and not just some arbitrary String.
If that's the case, I would suggest creating a new class
public class OptraderInfo{
private String date;
private Double dividend;
// etc
private void setDate(String newDate){
// Insert logic checking the newDate format here
date = newDate;
}
}
and then changing your data table to contain instances of OptraderInfo
public static OptraderInfo[] data = new OptraderInfo[5];
Also, consider using an ArrayList instead of a simple array. That will make it easier to add and remove items.
public static ArrayList<OptraderInfo> data = new ArrayList<OptraderInfo>(5);
Store a Date object in the model. Then Table Format Renderers make it easy to display the date in a format you desire.
精彩评论