How to know what JTextField is coming into the DocumentListener - jtextfield

I am trying to use a DocumentListener on a number of JTextFields. I need to know which textField is coming into the DocumentEvent so I can do some specific processes. Below is how I have my DocumentListener coded and one of the JTextFields set up [thanks to this example: How to get JTextField name in which is Document placed? ](my textField is declared at a higher scope).
How do I fix this?
final DocumentListener documentListener = new DocumentListener() {
#Override
public void changedUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
#Override
public void insertUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
#Override
public void removeUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
private void printIt(DocumentEvent documentEvent) {
final DocumentEvent.EventType type = documentEvent.getType();
String typeString = null;
final JTextField textField = (JTextField) documentEvent.getDocument().getProperty("parent");
if (type.equals(DocumentEvent.EventType.CHANGE)) {
typeString = "(parent: " + textField + ") Change";
} else if (type.equals(DocumentEvent.EventType.INSERT)) {
typeString = "(parent: " + textField + ") Insert";
} else if (type.equals(DocumentEvent.EventType.REMOVE)) {
typeString = "(parent: " + textField + ") Remove";
}
System.out.print("Type : " + typeString);
final Document source = documentEvent.getDocument();
final int length = source.getLength();
System.out.println("Length: " + length);
}
};
My JTextField is coded like the following...
textFieldFencing_LC1 = new JTextField();
textFieldFencing_LC1.setHorizontalAlignment(SwingConstants.CENTER);
textFieldFencing_LC1.setFont(new Font("Tahoma", Font.PLAIN, 9));
textFieldFencing_LC1.setColumns(10);
textFieldFencing_LC1.setBounds(234, 535, 85, 14);
panelLC.add(textFieldFencing_LC1);
textFieldFencing_LC1.getDocument().addDocumentListener(documentListener);
textFieldFencing_LC1.getDocument().putProperty("parent",textFieldFencing_LC1);
The output I want should look like this
Type : (parent: textFieldFencing_LC1) InsertLength: 1
Type : (parent: textFieldFencing_LC1) InsertLength: 1
The output I am getting looks like this ...
Type : (parent: javax.swing.JTextField[,234,535,85x14,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource#384cdfdd,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=10,columnWidth=0,command=,horizontalAlignment=CENTER]) InsertLength: 2
Type : (parent: javax.swing.JTextField[,234,535,85x14,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource#384cdfdd,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=10,columnWidth=0,command=,horizontalAlignment=CENTER]) InsertLength: 3

After reading the documentation on this Class, I realized the putProperty(Object,Object) method would allow me to put a String into it. So, now my listener on my JTextField looks like this...
textFieldFencing_LC1.getDocument().addDocumentListener(documentListener);
textFieldFencing_LC1.getDocument().putProperty("parent","LC1"); // String, String
Note, the second parameter in the putProperty() is a String with meaning for me, so I can check for LC#. While the update in the DocumentListener looks like this...
final DocumentEvent.EventType type = documentEvent.getType();
String typeString = null;
// Cast documentEvent to String
txtField.setText( (String) documentEvent.getDocument().getProperty("parent") );
if (type.equals(DocumentEvent.EventType.CHANGE)) {
typeString = "(parent: " + txtField.getText() + ") Change";
} else if (type.equals(DocumentEvent.EventType.INSERT)) {
typeString = "(parent: " + txtField.getText() + ") Insert";
} else if (type.equals(DocumentEvent.EventType.REMOVE)) {
typeString = "(parent: " + txtField.getText() + ") Remove";
}
System.out.print("Type : " + typeString);
final Document source = documentEvent.getDocument();
final int length = source.getLength();
System.out.println("Length: " + length);
}
Output now looks like...
Type : (parent: LC1) RemoveLength: 0
Type : (parent: LC1) InsertLength: 1
Type : (parent: LC1) InsertLength: 2
Type : (parent: LC1) InsertLength: 3
Bottom line: a referenced Class and two lines of code placed on the JTextFields I need to listen to so an automatic update occurs, is a lot better than adding a CaretListener on all of those fields.

Related

How to Update/Delete with elements from two different tables SQLite

I am working on a student grade submission program that accepts the following inputs: Student ID, Student First Name, Student Last Name, Class ID, Class Name, Grade Points, and Letter Grade. In order to prevent data redundancy I have created three different tables. However when attempting to "modify" or "delete" an entry with two values from distinct tables I am running into a problem. (For instance delete WHERE student.id is equal to the student.id in STUDENT_TABLE and WHERE class.id is equal to the class.id in the CLASS_TABLE)
With that being said, here is my current Modify code for Modifying a Student Class. I need to modify in a way in which the student.id and class.id are associated (Which are Located in two different Tables)
modifyclass.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(studentid.getText().toString().trim().length()==0 || classid.getText().toString().trim().length()==0 || classname.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Student & Class ID to update class. \n\nAll other Field Entries will be ignored");
return;
}
Cursor c=db.rawQuery("SELECT * FROM CLASS_TABLE WHERE classid='"+classid.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("UPDATE CLASS_TABLE SET classid='"+classid.getText()+"',classname='"+classname.getText()+"' WHERE studentid='"+studentid.getText()+"' AND classid='"+classid.getText()+"'");
showMessage("Success", "Class Record Modified");
}
else
{
showMessage("Error", "Invalid First and Last name or Class ID");
}
clearText();
}
});
EDIT:
Here are my tables for reference:
db=openOrCreateDatabase("STUDENTGRADES", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS STUDENT_TABLE (studentid TEXT, fname TEXT, lname TEXT)");
db.execSQL("CREATE TABLE IF NOT EXISTS CLASS_TABLE(classid TEXT PRIMARY KEY, classname TEXT UNIQUE)");
db.execSQL("CREATE TABLE IF NOT EXISTS GRADE_TABLE (studentid TEXT, classid TEXT, pointgrade INTEGER, lettergrade TEXT)");
You should never need to update multiple tables at once at least for a single change e.g. student's name has changed (student table would be changed) or say a grade changed (change the student's respective grade entry).
So I believe what would best suit are methods to update a Student's details (first or last name or both) and a method to alter the grades (again either or both).
As for deletion you could do this sequentially delete from the grades table and the from the student table or vice-versa.
As such I believe the following code includes methods deleteStudentInfo, changeStudentName, changeStudentFirstName, changeStudentLastName and changeStudentGrade and also includes example usage along with creating and populating the tables (you may wish to consider the revised schema):-
public class MainActivity extends AppCompatActivity {
public static final String DBNAME = "study";
public static final String STUDENT_TABLE_NAME = "STUDENT_TABLE";
public static final String COL_STUDENT_ID = "studentid";
public static final String COL_STUDENT_FIRSTNAME = "fname";
public static final String COL_STUDENT_LASTNAME = "lname";
public static final String CLASS_TABLE_NAME = "CLASS_TABLE";
public static final String COL_CLASS_ID = "classid";
public static final String COL_CLASS_NAME = "classname";
public static final String GRADE_TABLE_NAME = "GRADE_TABLE";
public static final String COL_GRADE_POINTGRADE = "pointgrade";
public static final String COL_GRADE_LETTERGRADE = "lettergrade";
public static final String BY_STUDENTID = COL_STUDENT_ID + "=?";
public static final String BY_CLASSID = COL_CLASS_ID + "=?";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase db = openOrCreateDatabase(DBNAME,Context.MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS " + STUDENT_TABLE_NAME + " (" +
COL_STUDENT_ID + " TEXT PRIMARY KEY, " +
COL_STUDENT_FIRSTNAME + " TEXT," +
COL_STUDENT_LASTNAME + " TEXT)"
);
db.execSQL("CREATE TABLE IF NOT EXISTS " + CLASS_TABLE_NAME + "(" +
COL_CLASS_ID + " TEXT PRIMARY KEY," +
COL_CLASS_NAME + " TEXT UNIQUE " +
")"
);
db.execSQL("CREATE TABLE IF NOT EXISTS " + GRADE_TABLE_NAME + "(" +
COL_STUDENT_ID + " TEXT, " +
COL_CLASS_ID + " TEXT, " +
COL_GRADE_POINTGRADE + " INTEGER, " +
COL_GRADE_LETTERGRADE + " TEXT" +
")"
);
db.execSQL("INSERT OR IGNORE INTO " + STUDENT_TABLE_NAME +
" VALUES" +
"('00001','Fred','Smith')," +
"('00010','Mary','Thomas')," +
"('00910','Angela','Jones')"
);
db.execSQL("INSERT OR IGNORE INTO " + CLASS_TABLE_NAME +
" VALUES" +
"('001','English')," +
"('101','Mathematics')," +
"('201','Chemistry')"
);
db.execSQL("INSERT OR IGNORE INTO " + GRADE_TABLE_NAME +
" VALUES" +
" ('00001','001',99,'A'), -- Fred Smith has 99 point grade as an A in English\n" +
" ('00001','101',25,'F'), -- Fred Smith has 25 point grade as an F on Mathematics\n" +
" ('00010','201',76,'B'), -- Angela Jones 76 a B in Chemistry\n" +
" ('00910','101',50,'C'), \n" +
" ('00910','201',63,'C'),\n" +
" ('00910','001',89,'A')\n" +
";"
);
changeStudentName(db,"00001","Joe","Bloggs");
changeStudentFirstName(db,"00001","Harry");
changeStudentLastName(db,"00001","Hoffmann");
// e.g. won't change due to -1 (skip pointsgrade) and null (skip lettergrade)
changeStudentGrade(db,"00001","001",-1,null);
// Change both
changeStudentGrade(db,"00001","001",25,"D");
changeStudentGrade(db,"00001","001",27,null);
// Ooops invalid student id
if (deleteStudentInfo(db,"001")) {
Log.d("DELETION","Student 001 deleted.");
} else {
Log.d("DELETION","Ooops Student 001 not deleted?????");
}
// Corrected Student ID
if (deleteStudentInfo(db,"00001")) {
Log.d("DELETION","Student 001 deleted.");
} else {
Log.d("DELETION","Ooops Student 001 not deleted?????");
}
}
private boolean deleteStudentInfo(SQLiteDatabase db, String studentid) {
String tag = "STUDENT_DELETE";
String student_table = "STUDENT_TABLE";
String grade_table = "GRADE_TABLE";
long pre_delete_student_count = DatabaseUtils.queryNumEntries(db,student_table);
long pre_delete_grade_count = DatabaseUtils.queryNumEntries(db,grade_table);
String whereclause = "studentid =?";
String[] whereargs = {studentid};
db.delete(student_table,whereclause,whereargs);
db.delete(grade_table,whereclause,whereargs);
long post_delete_student_count = DatabaseUtils.queryNumEntries(db,student_table);
long post_delete_grade_count = DatabaseUtils.queryNumEntries(db,grade_table);
Log.d(
tag,
"Number of Students deleted from " +
student_table + " is " +
String.valueOf(
pre_delete_student_count - post_delete_student_count
));
Log.d(
tag,
"Number of Grades deleted from " + grade_table + " is " +
String.valueOf(
pre_delete_grade_count - post_delete_grade_count
)
);
if ((pre_delete_student_count + pre_delete_grade_count) != (post_delete_student_count + post_delete_grade_count)) {
return true;
}
return false;
}
/**
* Flexible Student Name Change
*
* #param db The SQliteDatabase
* #param studentid The studentid (String)
* #param newfirstname The new firstname, null or blank to leave as is
* #param newlastname the new lastname, null or blank to leave as is
*/
private void changeStudentName(SQLiteDatabase db, String studentid, String newfirstname, String newlastname ) {
//Anything to do? if not do nothing
if ((newfirstname == null || newfirstname.length() < 1) && (newlastname == null || newlastname.length() < 1)) {
return;
}
ContentValues cv = new ContentValues();
if (newfirstname != null && newfirstname.length() > 0) {
cv.put(COL_STUDENT_FIRSTNAME,newfirstname);
}
if (newlastname != null && newlastname.length() > 0) {
cv.put(COL_STUDENT_LASTNAME,newlastname);
}
// Overcautious check
if (cv.size() < 1) {
return;
}
db.update(STUDENT_TABLE_NAME,cv,BY_STUDENTID,new String[]{studentid});
}
/**
* Change a Student's First Name (invokes changeStudentName method)
* #param db The SQLiteDatabase
* #param studentid The student's id (String)
* #param newfirstname The new first name to apply
*/
private void changeStudentFirstName(SQLiteDatabase db, String studentid, String newfirstname) {
changeStudentName(db,studentid,newfirstname,null);
}
/**
* Change a Student's Last Name (invokes changeStudentName method)
* #param db
* #param studentid
* #param newlastname
*/
private void changeStudentLastName(SQLiteDatabase db, String studentid, String newlastname) {
changeStudentName(db,studentid,null,newlastname);
}
/**
* Change a students grade (allowing just one (points/letter))
* #param db
* #param studentid
* #param classid
* #param newpointsgrade
* #param newlettergrade
*/
private void changeStudentGrade(SQLiteDatabase db, String studentid, String classid, int newpointsgrade, String newlettergrade) {
// Anything to do? if not do nothing
if (newpointsgrade < 0 && (newlettergrade == null || newlettergrade.length() < 1)) {
return;
}
ContentValues cv = new ContentValues();
if (newpointsgrade >= 0) {
cv.put(COL_GRADE_POINTGRADE,newpointsgrade);
}
if (newlettergrade != null && newlettergrade.length() > 0) {
cv.put(COL_GRADE_LETTERGRADE,newlettergrade);
}
String whereclause = COL_STUDENT_ID + "=? AND " + COL_CLASS_ID + "=?";
String[] whereargs = new String[]{studentid,classid};
db.update(GRADE_TABLE_NAME,cv,whereclause,whereargs);
}
}

Toast won't show inside Android DatePickerDialog

I have pickDateTime function, that restricts the user of choosing times that are in past. Current time - is the "minimum" time allowed.
This is the function:
public void pickDateTime(View view) {
Calendar c = Calendar.getInstance();
currentYear = c.get(Calendar.YEAR);
currentMonth = c.get(Calendar.MONTH);
currentDay = c.get(Calendar.DAY_OF_MONTH);
currentHour = c.get(Calendar.HOUR_OF_DAY);
currentMinute = c.get(Calendar.MINUTE);
DatePickerDialog datePickerDialog = new DatePickerDialog(NewMissionUser.this
, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
Log.d(TAG,"Chose: year: " + year + " , month: " + (month+1) + " day: " + day);
missionYear = year;
missionMonth = (month+1);
missionDay = day;
TimePickerDialog timePickerDialog = new TimePickerDialog(NewMissionUser.this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
missionTime = missionDay+"/"+missionMonth+"/"+missionYear + " " +hourOfDay + ":" + minute;
Log.d(TAG,"missionTime: " + missionTime);
if (isPast(missionTime)){
Log.d(TAG,"mission time in past!");
Toast.makeText(getApplicationContext(), "APP Mission inserted in past, ignoring date, default date is now.",Toast.LENGTH_LONG);
Toast.makeText(NewMissionUser.this, "Mission inserted in past, ignoring date, default date is now.",Toast.LENGTH_LONG);
Toast.makeText(getBaseContext(), "BASE Mission inserted in past, ignoring date, default date is now.",Toast.LENGTH_LONG);
missionTime = currentDay+"/"+currentMonth+"/"+currentYear + " " +currentHour + ":" + currentMinute;
}
}
},currentHour, currentMinute, true);
timePickerDialog.show();
}
},currentYear, currentMonth, currentDay);
datePickerDialog.show();
}
This is the class:
public class NewMissionUser extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener, GmapFragmentNewMission.TextClicked
The problem is, that the Toast won't show up in either of these tries.
The Log DOES appear, and in debug I see that the code reaches this point and isPast function is behaving as expected.
Why the Toast won't come up? Thanks.
You're missing the call to show() the Toast:
Toast.makeText(getApplicationContext(), "...",Toast.LENGTH_LONG).show();

javafx checklistree output: the term inter comma

for (CheckBoxTreeItem<String> treeItem : treeItems) {
if (treeItem.isSelected()) {
if (treeItem.getValue() != null) {
konular = konular + treeItem.getValue();
}
}
}
System.out.println(konular);
hi, this code for checktreeviews ,treelist sample1 sample2...
I have any choice, output: nullSample3Sample4
I want: sample3, sample4
The Java 8 way:
String konular = treeItems
.stream()
.map(TreeItem::getValue)
.filter(value -> value != null)
.collect(Collectors.joining(", "));
Try setting your variable like this:
String konular = "";
I believe it is currently turning null into a string when you add the first new string, and thus you end up with it starting with "null".
To add the comma, you could do something like this:
for (CheckBoxTreeItem<String> treeItem : treeItems) {
if (treeItem.isSelected()) {
if (treeItem.getValue() != null) {
if(konular!=""){
konular = konular + ", ";
}
konular = konular + treeItem.getValue();
}
}
}
I'm sure it's not the cleanest way, but it should do the trick.

Javafx Tree Table view: how to assign serial number to the direct child nodes of the root node

I am trying to number the direct child nodes of the root node in a serial manner (child-1, child-2...).
Here is my method which sets the cell value factory for myColumn:
private void setCellValueFactory() {
myColumn.setPrefWidth(120);
final int[] si_SubsetCount = {
1
};
myColumn.setCellValueFactory(
(TreeTableColumn.CellDataFeatures < MyDataClass, String > p) - > {
TreeItem < JVCC_PageHeaderInfo > ti_Row = p.getValue();
MyDataClass myDataClass = p.getValue().getValue();
String text;
if (ti_Row.isLeaf()) {
//leaf
} else if (ti_Row.getParent() != null) {
text = "Child-" + si_SubsetCount[0];
si_SubsetCount[0]++;
} else {
si_SubsetCount[0] = 1;
text = "Root";
}
return new ReadOnlyObjectWrapper < > (text);
});
}
But my output is as below:
>Root
>child-4
>leaf
>leaf
>child-8
>leaf
>leaf
I don't understand why the numbering is like 4, 8... instead of 1, 2...
Can someone help me with this.
That is because you can not control, when the CellValueFactorys method for evaluating the value for each row is called. It might get called several times for a single row, which is why your counter does not show the correct value for each line.
A dynamical approach is prefered here. If you should only have to differ between 3 node levels like root/child/leaf, then you could do something like this:
myColumn.setCellValueFactory( ( final CellDataFeatures<String, String> p ) ->
{
final TreeItem<String> value = p.getValue();
String text = "";
if ( value.isLeaf() )
text = "leaf";
else if ( value.getParent() != null )
text = "Child-" + (value.getParent().getChildren().indexOf( value ) + 1);
else
text = "root";
return new ReadOnlyStringWrapper( text );
} );
Since the Children of a TreeItem are stored in an ObservableList, you can just ask them for their index and add 1, since index starts at zero.

Accessing records in Android using rawQuery and then displaying

I am working on several rawQueries to use to parse data from a table in Android. The below code works fine and returns the lowest rowid in the table.
public void firstRecord(View v){
Cursor c = db.rawQuery("SELECT * FROM surveyDB WHERE rowid = (SELECT MIN(rowid) FROM surveyDB)",null);
c.moveToFirst();
szList.add(c.getString(0));
Toast.makeText(getApplicationContext(), "Sucessful Event. szRowid is: " +szList +".", Toast.LENGTH_LONG).show();
}
I have two questions, and they are both extremely basic: 1) what is the best way to expand the above code to create language to capture the contents of other columns in this table at that specific rowid, (rowid, sampler, species, place), and display this in my application? Something like this perhaps:
((EditText)findViewById(R.id.edSpecies)).setText("");
with the proper reference replacing "" in .setText()?
String TABLE_SURVEY = "surveyDB";
String COL_ROW_ID = "rowid";
String COL_SAMPLER = "sampler";
String COL_SPECIES = "species";
String COL_PLACE = "place";
public ArrayList<SurveyRecord> getSurveyRecords()
{
ArrayList<SurveyRecord> records = new ArrayList<SurveyRecord>();
String query = "SELECT * FROM " + TABLE_SURVEY;
query += " WHERE " + COL_ROW_ID = " SELECT MIN ("
query += COL_ROW_ID + ") FROM " + TABLE_SURVEY;
Cursor c = db.rawQuery(query,null);
if(Cursor.moveToFirst())
{
do{
String sampler = c.getString(cursor.getColumnIndex(COL_SAMPLER));
String species= c.getString(cursor.getColumnIndex(COL_SPECIES));
String place = c.getString(cursor.getColumnIndex(COL_PLACE));
String rowId = c.getString(cursor.getColumnIndex(COL_ROW_ID));
records.add(new (rowId,species,place,sampler));
}while(c.moveToNext())
}
c.close();
}
public class SurveyRecord{
String mRowId;
String mSpecies;
String mPlace;
String mSampler;
public SurveyRecord(String rowId,String species,String place,String sampler)
{
this.mRowId = rowId;
this.mSpecies = species;
this.mPlace = place;
this.mSampler = sampler;
}
}
//Goes to the first record in the dataset
public void firstRecord(View v){
Cursor c = db.rawQuery("SELECT * FROM surveyDB WHERE rowid = (SELECT MIN(rowid) FROM surveyDB)",null);
c.moveToFirst();
((EditText)findViewById(R.id.edRowid))
.setText(c.getString(0));
((EditText)findViewById(R.id.edSpecies))
.setText(c.getString(1));
((EditText)findViewById(R.id.edArea))
.setText(c.getString(2));
((EditText)findViewById(R.id.edSampler))
.setText(c.getString(3));
}

Resources