java - Jtext field to listen without pressing enter -
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.documentevent; import javax.swing.text.document; import java.sql.connection; import java.sql.databasemetadata; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; @suppresswarnings("serial") public class save extends jpanel { connection con; statement st; resultset rs; string trailername; string blockname; string locationname; string newline = "\n"; string text; jcombobox<?> trailerlist; jcombobox<?> blocklist; jcombobox<?> locationlist; jtextfield textfield; jtextarea textarea; public save() { super(new borderlayout()); textfield = new jtextfield(20); textarea = new jtextarea(5, 20); textarea.seteditable(false); textfield.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { text = textfield.gettext(); textarea.append(text + newline); textfield.selectall(); textarea.setcaretposition(textarea.getdocument().getlength()); system.out.println("comment: " + text); } }); string[] trailer = { "a1", "a2", "b1", "b2", "c1", "please select trailer" }; string[] block = { "a", "b", "c", "d", "e", "please select block" }; string[] location = { "door a3", "door b2", "door c4", "door d2", "door e9", "please select location" }; trailerlist = new jcombobox<object>(trailer); trailerlist.setselectedindex(5); trailerlist.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { trailername = (string) trailerlist.getselecteditem(); system.out.println("trailer: " + trailername); } }); blocklist = new jcombobox<object>(block); blocklist.setselectedindex(5); blocklist.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { blockname = (string) blocklist.getselecteditem(); system.out.println("block: " + blockname); }
});
locationlist = new jcombobox<object>(location); locationlist.setselectedindex(5); locationlist.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { locationname = (string) locationlist.getselecteditem(); system.out.println("location: " + locationname); }
});
jbutton buttonsave = new jbutton("save"); add(buttonsave); buttonsave.setactioncommand("save"); add(buttonsave); buttonsave.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { string action = evt.getactioncommand(); if (action.equals("save")) { system.out.println("button pressed!"); try { string driver = "sun.jdbc.odbc.jdbcodbcdriver"; class.forname(driver); string db = "jdbc:odbc:ttracking"; con = drivermanager.getconnection(db); databasemetadata meta = con.getmetadata(); system.out.println("server name: " + meta.getdatabaseproductname()); system.out.println("server version: " + meta.getdatabaseproductversion()); system.out.println(""); system.out.println("inserting records table..."); preparedstatement st = con.preparestatement( "insert trailerlocation (trailer, block, location, day, settime, comment) " + "values (?, ?, ?, now(), now(), ?)"); st.setstring(1, trailername); st.setstring(2, blockname); st.setstring(3, locationname); st.setstring(4, text); st.addbatch(); st.executebatch(); joptionpane.showmessagedialog(null,"inserted record table: " + "\ntrailer : " + trailername + "\nblock : " + blockname + "\nlocation : " + locationname + "\ncomment : " + text); }catch(sqlexception se){ //handle errors jdbc se.printstacktrace(); }catch(exception e1){ //handle errors class.forname e1.printstacktrace(); }finally{ //finally block used close resources try{ if(st!=null) con.close(); }catch(sqlexception se){ }// nothing try{ if(con!=null) con.close(); }catch(sqlexception se){ se.printstacktrace(); }//end try }//end try system.out.println("goodbye!"); }} }); add(trailerlist, borderlayout.west); add(blocklist, borderlayout.center); add(locationlist, borderlayout.east); add(buttonsave,borderlayout.south); add(textfield, borderlayout.north); setborder(borderfactory.createemptyborder(20,20,20,20)); } private static void createandshowgui() { //create , set window. jframe frame = new jframe("save"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlocationrelativeto(null); frame.setundecorated(true); //create , set content pane. jcomponent newcontentpane = new save(); newcontentpane.setopaque(true); //content panes must opaque frame.setcontentpane(newcontentpane); //display window. frame.pack(); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); } public static void main(string[] args) { //schedule job event-dispatching thread: //creating , showing application's gui. javax.swing.swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }
for form creating have text field people add comments issue comment save need press enter button there way without pressing enter button?
tried doc-listener , action listener not code work
simple solution is, remove usage of variable "text" , directly use textfield.gettext() ,but update text area still need use documentlistener.
ex:
st.setstring(4, textfield.gettext());
but in design perspective should improved, advise use model maintain gui data status, can use beansbinding synchronize gui components model. once user click on save button, model should submitted other class handle save function.
Comments
Post a Comment