Java buttons to display to screen

Reply

Join Date: Jun 2009
Posts: 140
Reputation: whiteyoh is an unknown quantity at this point 
Solved Threads: 2
whiteyoh whiteyoh is offline Offline
Junior Poster

Java buttons to display to screen

 
0
  #1
Jun 20th, 2009
Hi All,

Im fairly new to Java and im currently working my way through a couple of books but im stumped with something.

I want to know, or get an example of how i can add an action to a button which outputs to a screen. I just cant seem to find any examples at all.

I plan to use java to make desktop applications with a local access database.

Any help would be greatly appreciated.

Thanks

Paul
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,188
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 482
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Java buttons to display to screen

 
0
  #2
Jun 20th, 2009
What you mean by "outputs to a screen"? Something like filling in your form?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

Re: Java buttons to display to screen

 
-1
  #3
Jun 20th, 2009
Originally Posted by whiteyoh View Post

Im fairly new to Java
Since you are new to Java, its better if you use Netbeans or JBuilder, you simply have to drag and drop the Swing components and, Database connectivity is also fairly simple.
Local P ...
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,188
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 482
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Java buttons to display to screen

 
0
  #4
Jun 20th, 2009
I wouldn't call it reasonable advice. There are too many students dependent on their IDE and if somebody force them work on different IDE or from command line they are lost.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 140
Reputation: whiteyoh is an unknown quantity at this point 
Solved Threads: 2
whiteyoh whiteyoh is offline Offline
Junior Poster

Re: Java buttons to display to screen

 
0
  #5
Jun 20th, 2009
Thanks for the replies. Interesting to know theres still more than 1 way to skin a cat.

Im using netbeans.

im after an example of something that gives me an understanding of how clicking a button interacts with an access database to add,edit,delete a record and outputs everything to a simple screen, kinda like how a dos window works.

thanks again for your input, im determined to learn java, i have no problem with the maths side and the uml (2).

Regards

Paul
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Java buttons to display to screen

 
0
  #6
Jun 21st, 2009
Originally Posted by whiteyoh View Post
Thanks for the replies. Interesting to know theres still more than 1 way to skin a cat.

Im using netbeans.

im after an example of something that gives me an understanding of how clicking a button interacts with an access database to add,edit,delete a record and outputs everything to a simple screen, kinda like how a dos window works.

thanks again for your input, im determined to learn java, i have no problem with the maths side and the uml (2).

Regards

Paul

There are lots of ways to skin a cat. Setting up the button, having the button clicking call a function, the database querying, and the displaying the results to a console screen, are all separate, though interwoven tasks. You set up a GUI, you stick a button on it, you add an Action Listener to the button, then you have it do what you want it to do when that button is clicked. You mentioned "console" and "DOS", so I assume "display to the screen" means "display to a console screen", not display to somewhere on the GUI. Below is a sample program that may give you some ideas. It has no database connectivity, instead returning Strings. You'll for sure want to change that. Here's a tutorial on JDBC/database querying.

http://java.sun.com/docs/books/tutorial/jdbc/index.html

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5.  
  6. public class MyFrame extends JFrame implements ActionListener
  7. {
  8. JButton button;
  9.  
  10. public MyFrame ()
  11. {
  12. this.setSize (600, 600);
  13. button = new JButton ("Push me");
  14. button.addActionListener(this);
  15. this.setLayout (new FlowLayout ());
  16. this.add (button);
  17. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18. this.setVisible (true);
  19. }
  20.  
  21.  
  22. public static void main(String[] args)
  23. {
  24. new MyFrame ();
  25. }
  26.  
  27.  
  28. public void actionPerformed(ActionEvent e)
  29. {
  30. System.out.println ("Button has been pressed.");
  31. String addressData[] = DatabaseQuery ();
  32.  
  33. for (int i = 0; i < addressData.length; i++)
  34. System.out.println (addressData[i]);
  35. }
  36.  
  37.  
  38. public String[] DatabaseQuery ()
  39. // connect to, query DB here. You'll deal with ResultSets, not Strings
  40. {
  41. String addressData[] = new String[3];
  42. addressData[0] = "Joe Smith 40 Elm St. Los Angeles, CA";
  43. addressData[1] = "Jim Jones 50 Main St. Houston, TX";
  44. addressData[2] = "Ted Allen 60 Spring St. San Jose, CA";
  45. return addressData;
  46. }
  47. }


Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 19
Reputation: oliver_lundag has a little shameless behaviour in the past 
Solved Threads: 0
oliver_lundag oliver_lundag is offline Offline
Newbie Poster

Re: Java buttons to display to screen

 
0
  #7
Jul 17th, 2009
  1. package src.maintenance.loyalty;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Cursor;
  5. import java.awt.Dimension;
  6. import java.awt.Font;
  7. import java.awt.Point;
  8. import java.awt.Rectangle;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.FocusEvent;
  12. import java.awt.event.FocusListener;
  13. import java.awt.event.KeyEvent;
  14. import java.awt.event.KeyListener;
  15. import java.io.PrintWriter;
  16. import java.io.StringWriter;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.sql.Statement;
  20.  
  21. import javax.swing.BorderFactory;
  22. import javax.swing.InputMap;
  23. import javax.swing.JButton;
  24. import javax.swing.JComboBox;
  25. import javax.swing.JInternalFrame;
  26. import javax.swing.JLabel;
  27. import javax.swing.JOptionPane;
  28. import javax.swing.JPanel;
  29. import javax.swing.JScrollPane;
  30. import javax.swing.JTable;
  31. import javax.swing.KeyStroke;
  32. import javax.swing.SwingConstants;
  33. import javax.swing.event.InternalFrameEvent;
  34. import javax.swing.event.InternalFrameListener;
  35. import javax.swing.table.DefaultTableCellRenderer;
  36. import javax.swing.table.TableCellRenderer;
  37.  
  38. import src.maintenance.DBConn.DBConnect;
  39. import src.maintenance.Functions.Message;
  40. import src.maintenance.Functions.MyField;
  41. import src.maintenance.Functions.createINI;
  42. import src.maintenance.ResultTableModel.ResultSetTableModel;
  43. import src.maintenance.core.mainform;
  44.  
  45.  
  46. /*
  47.  * 05091987revilogadnul
  48.  * 2009
  49.  * 0557 Applied Ideas
  50.  */
  51.  
  52.  
  53. public class City_Municipality extends JInternalFrame{
  54. // JPanel
  55. private JPanel jContentPane = null;
  56. // Declare JLabel
  57. private JLabel JLCountry = null;
  58. private JLabel JLRegion = null;
  59. private JLabel JLProvince = null;
  60. private JLabel JLCity_MunCode = null;
  61. private JLabel JLCity_MunDesc = null;
  62. // Declare JComboBox
  63. public static JComboBox JC_country = null;
  64. public static JComboBox JC_region = null;
  65. public static JComboBox JC_province = null;
  66. // Declare TextField
  67. public static MyField JTCity_MunCode = null;
  68. public static MyField JTCity_MunDesc = null;
  69. //Table
  70. private JScrollPane jScrollPane = null;
  71. private JTable jTable = null;
  72. private String defaultQuery = " SELECT city_municipality,description " +
  73. " FROM addr_ref " +
  74. " WHERE brgy_village = ' ' and street = ' ' " +
  75. "and"+
  76. " country != '' " +
  77. "and"+
  78. " region != '' " +
  79. "and"+
  80. " province != '' " +
  81. "and"+
  82. " city_municipality != '' ";
  83. private static ResultSetTableModel tableMain = null;
  84. // buttons
  85. public static JButton jbtnadd;
  86. public static JButton jbtnedit;
  87. public static JButton jbtnExit;
  88. //variable
  89. static int xRecord = 0;
  90. static int xRecordE = 0;
  91. static int xrecord = 0;
  92. private int fileCount = 0;
  93. private int fileCountE = 0;
  94. private boolean exist = false;
  95. private static String Country_Code = null;
  96. private static String Region_Code = null;
  97. private static String Province_Code = null;
  98.  
  99. public City_Municipality(){
  100. super ("City Municipality Maintenace",//title
  101. false, //rezisable
  102. true, //closable
  103. false, //maximizable
  104. false); //iconifiable
  105. this.setName("City Municipality Maintenace");
  106. try {
  107. setConnection();
  108. } catch (ClassNotFoundException e) {
  109. // TODO Auto-generated catch block
  110. e.printStackTrace();
  111. }
  112. initialize();
  113. }// end of method City_Municipality
  114.  
  115. private void setConnection() throws ClassNotFoundException{
  116. try {
  117. tableMain = new ResultSetTableModel(defaultQuery);
  118. }
  119. catch(SQLException sqlE){
  120. Message.messageInfo("Error log has been created");
  121. StringWriter traceWriter = new StringWriter();
  122. PrintWriter printWriter = new PrintWriter(traceWriter, false);
  123. sqlE.printStackTrace(printWriter);
  124. createINI.create("City Municipality Maintenace", "setConnection", traceWriter.toString());
  125. dispose();
  126. }//end of try catch
  127. }//end of method setConnection()
  128.  
  129. private void initialize(){
  130. this.setSize(370, 430);
  131. this.setContentPane( getcontentJPanel());
  132. this.setOpaque(true);
  133. this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  134. this.setClosable(true);
  135. this.setVisible(true);
  136. this.addInternalFrameListener(new InternalFrameListener(){
  137. public void internalFrameActivated(InternalFrameEvent arg0) {
  138. mainform.tree.setEnabled(false);
  139. mainform.tree.clearSelection();
  140. }
  141. public void internalFrameClosed(InternalFrameEvent arg0) {}
  142. public void internalFrameClosing(InternalFrameEvent arg0) {
  143. mainform.tree.setEnabled(true);
  144. }
  145. public void internalFrameDeactivated(InternalFrameEvent arg0) {
  146. }
  147. public void internalFrameDeiconified(InternalFrameEvent arg0) {}
  148. public void internalFrameIconified(InternalFrameEvent arg0) {}
  149. public void internalFrameOpened(InternalFrameEvent arg0) {}
  150. });
  151. setTitle("City Municipality Maintenace");
  152. startup();
  153. getRecordCountE();
  154. if (xRecord != fileCount){
  155. xRecord++;
  156. }
  157. if (xRecordE != fileCountE){
  158. xRecordE++;
  159. }
  160. xrecord = xRecordE;
  161. }//end of method initialize
  162.  
  163. private void getRecordCountE() throws NullPointerException{
  164. try {
  165. fileCountE = 0;
  166. String query = " SELECT count(*) " +
  167. " FROM addr_ref " +
  168. " WHERE brgy_village = '' and street = '' " +
  169. "and"+
  170. " country != '' " +
  171. "and"+
  172. " region != '' " +
  173. "and"+
  174. " province != '' " +
  175. "and"+
  176. " city_municipality != '' " +
  177. "and"+
  178. " description != '' ";
  179. Statement stmt = mainform.stat;
  180. ResultSet rs = stmt.executeQuery(query);
  181. while(rs.next()){
  182. fileCountE = rs.getInt(1);
  183. }
  184. rs.close();
  185. }
  186. catch(Exception e){
  187. e.printStackTrace();
  188. }//end of try catch
  189. }//end of method getRecordCountE()
  190.  
  191. private JPanel getcontentJPanel(){
  192. if (jContentPane == null){
  193. //label for country
  194. JLCountry = new JLabel();
  195. JLCountry.setHorizontalAlignment(SwingConstants.LEFT);
  196. JLCountry.setFont(new Font("Dialog", Font.BOLD, 12));
  197. JLCountry.setText("Country");
  198. JLCountry.setLocation(new Point(20, 230));
  199. JLCountry.setSize(new Dimension(91, 20));
  200. //label for region
  201. JLRegion = new JLabel();
  202. JLRegion.setHorizontalAlignment(SwingConstants.LEFT);
  203. JLRegion.setFont(new Font("Dialog", Font.BOLD, 12));
  204. JLRegion.setText("Region");
  205. JLRegion.setLocation(new Point(20, 260));
  206. JLRegion.setSize(new Dimension(91, 20));
  207. //label for province
  208. JLProvince = new JLabel();
  209. JLProvince.setHorizontalAlignment(SwingConstants.LEFT);
  210. JLProvince.setFont(new Font("Dialog", Font.BOLD, 12));
  211. JLProvince.setText("Province");
  212. JLProvince.setLocation(new Point(20, 290));
  213. JLProvince.setSize(new Dimension(91, 20));
  214. //label for city municipality code
  215. JLCity_MunCode = new JLabel();
  216. JLCity_MunCode.setHorizontalAlignment(SwingConstants.LEFT);
  217. JLCity_MunCode.setFont(new Font("Dialog", Font.BOLD, 12));
  218. JLCity_MunCode.setText("City Municipality Code");
  219. JLCity_MunCode.setLocation(new Point(20, 320));
  220. JLCity_MunCode.setSize(new Dimension(150, 20));
  221. //label for description
  222. JLCity_MunDesc = new JLabel();
  223. JLCity_MunDesc.setHorizontalAlignment(SwingConstants.LEFT);
  224. JLCity_MunDesc.setFont(new Font("Dialog", Font.BOLD, 12));
  225. JLCity_MunDesc.setText("Code Description");
  226. JLCity_MunDesc.setLocation(new Point(20, 350));
  227. JLCity_MunDesc.setSize(new Dimension(100, 20));
  228. }// end of if
  229. jContentPane = new JPanel();
  230. jContentPane.setLayout(null);
  231. //Jlabel
  232. jContentPane.add(JLCountry,null);
  233. jContentPane.add(JLRegion,null);
  234. jContentPane.add(JLProvince,null);
  235. jContentPane.add(JLCity_MunCode,null);
  236. jContentPane.add(JLCity_MunDesc,null);
  237. //JComboBox
  238. jContentPane.add(getJC_country(),null);
  239. jContentPane.add(getJC_region(),null);
  240. jContentPane.add(getJC_province(),null);
  241. // Text Field
  242. jContentPane.add(getJTCity_MunCode(),null);
  243. jContentPane.add( getJTCity_MunDesc(),null);
  244. //Table
  245. jContentPane.add(getJScrollPane(), null);
  246. //Button
  247. jContentPane.add(getjAdd(),null);
  248. jContentPane.add(getjEdit(),null);
  249. jContentPane.add(getjExit(),null);
  250. return jContentPane;
  251. }// end of method getcontentJPanel()
  252.  
  253. private JScrollPane getJScrollPane() {
  254. if (jScrollPane == null) {
  255. jScrollPane = new JScrollPane();
  256. jScrollPane.setBounds(new Rectangle(20, 10, 320, 150));
  257. jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  258. jScrollPane.setViewportView(getJTable());
  259. jScrollPane.setVisible(true);
  260. }//end of if
  261. return jScrollPane;
  262. }//end of method getJScrollPane()
  263.  
  264. private JTable getJTable() {
  265. if (jTable == null) {
  266. jTable = new JTable(tableMain){
  267.  
  268. /**
  269. *
  270. */
  271. private static final long serialVersionUID = 1L;
  272.  
  273. public boolean isCellEditable(int irows, int icols){
  274. return false;
  275. }
  276. };
  277.  
  278. TableCellRenderer renderer = new Renderer();
  279. jTable.setDefaultRenderer(Object.class,renderer);
  280.  
  281. jTable.setSelectionMode(0);
  282. jTable.getTableHeader().setResizingAllowed(true);
  283. jTable.getTableHeader().setMaximumSize(new Dimension(23,56));
  284. jTable.setPreferredScrollableViewportSize(new Dimension(690,300));
  285. jTable.getTableHeader().setReorderingAllowed(false);
  286.  
  287. reload();
  288. }// end of if
  289.  
  290. return jTable;
  291. }//end of method getJTable()
  292.  
  293. private void reload(){
  294. try {
  295. jTable.getColumnModel().getColumn(0).setMaxWidth(0);
  296. jTable.getColumnModel().getColumn(0).setMinWidth(0);
  297. jTable.getColumnModel().getColumn(1).setMaxWidth(0);
  298. jTable.getColumnModel().getColumn(1).setMinWidth(0);
  299. jTable.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(82);
  300. jTable.getTableHeader().getColumnModel().getColumn(0).setMinWidth(82);
  301. jTable.getTableHeader().getColumnModel().getColumn(1).setMaxWidth(220);
  302. jTable.getTableHeader().getColumnModel().getColumn(1).setMinWidth(220);
  303. jTable.getColumnModel().getColumn(0).setHeaderValue("Code");
  304. jTable.getColumnModel().getColumn(1).setHeaderValue("Description");
  305. } catch (Exception e) {
  306. e.printStackTrace();
  307. }//end of try catch
  308. }//end of method reload
  309.  
  310. private class Renderer extends DefaultTableCellRenderer{
  311. /**
  312. *
  313. */
  314. private static final long serialVersionUID = 1L;
  315.  
  316. public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  317. {
  318. super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
  319. if (column == 1)setHorizontalAlignment(LEFT);
  320. if (column == 2)setHorizontalAlignment(LEFT);
  321. setBorder(BorderFactory.createLineBorder(getBackground(), 1));
  322. return this;
  323. }// end of method getTableCellRendererComponen
  324. }//end of class Renderer extends DefaultTableCellRenderer
  325.  
  326. private JComboBox getJC_country(){
  327. if(JC_country== null){
  328. JC_country = new JComboBox();
  329. JC_country.setSize(new Dimension(260, 20));
  330. JC_country.setLocation(new Point(80, 230));
  331. JC_country.addActionListener(new ActionListener(){
  332. public void actionPerformed(ActionEvent arg0) {
  333. try{
  334. if (JC_country.getSelectedItem().toString().equalsIgnoreCase("(Choose)") == true){
  335. JC_country.removeAllItems();
  336. queryCountry();
  337. }//end of if
  338. if (JC_country.getSelectedItem().toString().equalsIgnoreCase("(Choose)") == true){
  339. Country_Code = "Nothing";
  340. JC_region.removeAllItems();
  341. queryRegion();
  342. JC_region.setSelectedIndex(0);
  343. JC_province.setSelectedIndex(0);
  344. }//end of if
  345. Country_Code = JC_country.getSelectedItem().toString().substring(0, JC_country.getSelectedItem().toString().indexOf("-"));
  346. String Filter_By_Code = "select city_municipality,description from addr_ref " +
  347. "where country = '"+Country_Code +"'" +
  348. "and"+
  349. " region != '' " +
  350. "and"+
  351. " province != '' " +
  352. "and"+
  353. " city_municipality != '' "+
  354. "and"+
  355. " brgy_village = '' " +
  356. "and"+
  357. " street = '' ";
  358. try{
  359. tableMain.setQuery(Filter_By_Code);
  360. }catch(Exception exp){
  361. }//end of try catch
  362. JC_region.removeAllItems();
  363. queryRegion();
  364. JC_region.setSelectedIndex(0);
  365. JC_province.setSelectedIndex(0);
  366. }catch(Exception e){
  367. }//end of try catch
  368. }// end of actionPerformed
  369. });
  370. }//end of JC_country.addActionListener(new ActionListener(){
  371. return JC_country;
  372. }//end of method getJC_country()
  373.  
  374. private JComboBox getJC_region(){
  375. if(JC_region== null){
  376. JC_region = new JComboBox();
  377. JC_region.setSize(new Dimension(260, 20));
  378. JC_region.setLocation(new Point(80, 260));
  379. JC_region.addItem("(Choose)");
  380. JC_region.addActionListener(new ActionListener(){
  381. public void actionPerformed(ActionEvent arg0) {
  382. try{
  383. if (JC_region.getSelectedItem().toString().equalsIgnoreCase("(Choose)") == true){
  384. Region_Code = "Nothing";
  385. JC_province.removeAllItems();
  386. queryProvince();
  387. JC_province.setSelectedIndex(0);
  388. }//end of if
  389. Region_Code = JC_region.getSelectedItem().toString().substring(0, JC_region.getSelectedItem().toString().indexOf("-"));
  390. String Filter_By_Code = "select city_municipality,description from addr_ref " +
  391. "where country = '"+Country_Code +"'" +
  392. "and"+
  393. " region = '"+Region_Code +"'" +
  394. "and"+
  395. " province != '' " +
  396. "and"+
  397. " city_municipality != '' "+
  398. "and"+
  399. " brgy_village = '' " +
  400. "and"+
  401. " street = '' ";
  402. try{
  403. tableMain.setQuery(Filter_By_Code);
  404. }catch(Exception exp){
  405. }//end of try catch
  406. JC_province.removeAllItems();
  407. queryProvince();
  408. JC_province.setSelectedIndex(0);
  409. }catch(Exception exp){
  410. }//end of try catch
  411. }
  412. });
  413. }
  414. return JC_region;
  415. }
  416.  
  417. private JComboBox getJC_province(){
  418. if(JC_province== null){
  419. JC_province = new JComboBox();
  420. JC_province.setSize(new Dimension(260, 20));
  421. JC_province.setLocation(new Point(80, 290));
  422. JC_province.addItem("(Choose)");
  423. JC_province.addActionListener(new ActionListener(){
  424. public void actionPerformed(ActionEvent arg0) {
  425. try{
  426. Province_Code = JC_province.getSelectedItem().toString().substring(0, JC_province.getSelectedItem().toString().indexOf("-"));
  427. String Filter_By_Code = "select city_municipality,description from addr_ref " +
  428. "where country = '"+Country_Code +"'" +
  429. "and"+
  430. " region = '"+Region_Code +"'" +
  431. "and"+
  432. " province = '"+Province_Code +"'" +
  433. "and"+
  434. " city_municipality != '' "+
  435. "and"+
  436. " brgy_village = '' " +
  437. "and"+
  438. " street = '' ";
  439. try{
  440. tableMain.setQuery(Filter_By_Code);
  441. }catch(Exception exp){}
  442. }catch(Exception exp){}
  443. }
  444. });
  445. }
  446. return JC_province;
  447. }
  448.  
  449. private MyField getJTCity_MunCode(){
  450. if (JTCity_MunCode== null){
  451. JTCity_MunCode = new MyField(true,5);
  452. // JTCity_MunCode = new MyField(true,6);
  453. JTCity_MunCode.setLocation(new Point(160, 320));
  454. JTCity_MunCode.setSize(new Dimension(60, 20));
  455. JTCity_MunCode.setCapital(true);
  456. JTCity_MunCode.addKeyListener(new KeyListener() {
  457. public void keyPressed(KeyEvent e) {
  458. InputMap IMP = JTCity_MunCode.getInputMap(JTCity_MunCode.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  459. KeyStroke ent = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
  460. IMP.put(ent, "none");
  461. if (e.getKeyCode() == 10){
  462. try{
  463. if (jbtnadd.getText().toString().equalsIgnoreCase("Save") == true){
  464. if (JTCity_MunCode.getText().toString().length() < 2){
  465. JTCity_MunCode.setText("0"+JTCity_MunCode.getText().toString().trim());
  466. }
  467. queryexist();//check if the record is already exist or not
  468. if (exist == true){
  469. Message.messageInfo("Record Already Exist");
  470. JTCity_MunCode.grabFocus();
  471. }else{
  472. JTCity_MunDesc.grabFocus();
  473. }
  474. }else{
  475. JTCity_MunDesc.grabFocus();
  476. }
  477. }catch(Exception excp){}
  478. }//end of if
  479. }//end of keyPressed
  480.  
  481. public void keyReleased(KeyEvent arg0) {
  482. // TODO Auto-generated method stub
  483.  
  484. }
  485.  
  486. public void keyTyped(KeyEvent arg0) {
  487. // TODO Auto-generated method stub
  488.  
  489. }
  490. });
  491. JTCity_MunCode.addFocusListener(new FocusListener() {
  492. public void focusGained(FocusEvent arg0) {
  493. }
  494. public void focusLost(FocusEvent arg0) {
  495. try{
  496. if (jbtnadd.getText().toString().equalsIgnoreCase("Save") == true){
  497. if (JTCity_MunCode.getText().toString().length() < 2){
  498. JTCity_MunCode.setText("0"+JTCity_MunCode.getText().toString().trim());
  499. }
  500. queryexist();//check if the record is already exist or not
  501. if (exist == true){
  502. Message.messageInfo("Record Already Exist");
  503. JTCity_MunCode.grabFocus();
  504. }else{
  505. JTCity_MunDesc.grabFocus();
  506. }
  507. }else{
  508. JTCity_MunDesc.grabFocus();
  509. }
  510. }catch(Exception excp){}
  511. }
  512. });
  513. }
  514. return JTCity_MunCode;
  515. }
  516.  
  517. private MyField getJTCity_MunDesc(){
  518. if (JTCity_MunDesc == null){
  519. JTCity_MunDesc = new MyField(true,50);
  520. JTCity_MunDesc.setLocation(new Point(130, 350));
  521. JTCity_MunDesc.setSize(new Dimension(210, 20));
  522. JTCity_MunDesc.addKeyListener(new KeyListener() {
  523. public void keyPressed(KeyEvent e) {
  524.  
  525. }
  526.  
  527. public void keyReleased(KeyEvent arg0) {
  528. // TODO Auto-generated method stub
  529.  
  530. }
  531.  
  532. public void keyTyped(KeyEvent arg0) {
  533. // TODO Auto-generated method stub
  534.  
  535. }
  536. });
  537. }
  538. return JTCity_MunDesc;
  539. }
  540.  
  541. private JButton getjAdd() {
  542. if (jbtnadd == null) {
  543. jbtnadd = new JButton();
  544. jbtnadd.setText("Add");
  545. jbtnadd.setBounds(new Rectangle(30, 170, 90, 25));
  546. jbtnadd.setMnemonic('A');
  547. jbtnadd.addActionListener(new java.awt.event.ActionListener() {
  548. public void actionPerformed(java.awt.event.ActionEvent e) {
  549. if(jbtnadd.getText().toString().equalsIgnoreCase("Add") == true){
  550. jbtnadd.setMnemonic('A');
  551. try{
  552. String Filter_By_Code = "select city_municipality,description from addr_ref " +
  553. "where country = '"+Country_Code +"'" +
  554. "and"+
  555. " region = '"+Region_Code +"'" +
  556. "and"+
  557. " province = '"+Province_Code +"'" +
  558. "and"+
  559. " city_municipality != '' "+
  560. "and"+
  561. " brgy_village = '' " +
  562. "and"+
  563. " street = '' ";
  564. try{
  565. tableMain.setQuery(Filter_By_Code);
  566. }catch(Exception exp){}
  567. }catch(Exception exp){}
  568. Enabled();//enable all components
  569. }else if (jbtnadd.getText().toString().equalsIgnoreCase("Save") == true){
  570. jbtnadd.setMnemonic('S');
  571. String insert = DBConnect.Insert("addr_ref",
  572. "country,region,province,city_municipality,description",
  573. "'"+ DBConnect.clean(Country_Code)+"', " +
  574. "'"+ DBConnect.clean(Region_Code)+"', " +
  575. "'"+ DBConnect.clean(Province_Code)+"', " +
  576. "'"+ DBConnect.clean(JTCity_MunCode.getText().toString().trim())+"', " +
  577. "'"+ DBConnect.clean(JTCity_MunDesc.getText().toString().trim())+"'");
  578. if(JC_country.getSelectedItem().toString().equalsIgnoreCase("(Choose)")){
  579. Message.messageWarning("Please Choose Country");
  580. JC_country.grabFocus();
  581. }else if(JC_region.getSelectedItem().toString().equalsIgnoreCase("(Choose)")){
  582. Message.messageWarning("Please Choose Region");
  583. JC_region.grabFocus();
  584. }else if(JC_province.getSelectedItem().toString().equalsIgnoreCase("(Choose)")){
  585. Message.messageWarning("Please Choose Province");
  586. JC_province.grabFocus();
  587. }else if(JTCity_MunCode.getText().length()==0){
  588. Message.messageWarning("Please Enter Code");
  589. JTCity_MunCode.grabFocus();
  590. }else if(JTCity_MunDesc.getText().length()==0){
  591. Message.messageWarning("Please Enter Description");
  592. JTCity_MunDesc.grabFocus();
  593. }else{
  594. try{
  595. queryexist();//check if the record is already exist or not
  596. if (exist == true){
  597. Message.messageInfo(null,"Record Already Exist");
  598. JTCity_MunCode.grabFocus();
  599. }else{
  600. Statement stmt = mainform.dbConn.getConnection().createStatement();
  601. stmt.execute(DBConnect.beginTransaction());
  602. try{
  603. tableMain.setInsert(insert);
  604. tableMain.setQuery(defaultQuery);
  605. reload();
  606. }catch(SQLException sqle){
  607. Message.messageInfo(null,"Record Already Exist");
  608. JTCity_MunCode.grabFocus();
  609. }
  610. JOptionPane.showMessageDialog(null, "Record was successfully saved");
  611. // startup();
  612. startupAfterSaving();
  613. stmt.execute(DBConnect.commitTransaction());
  614. stmt.close();
  615. }
  616. }catch(Exception excp){}
  617. }
  618. }else if(jbtnadd.getText().toString().equalsIgnoreCase("Update") == true){
  619. try{
  620. jbtnadd.setMnemonic('U');
  621. String update = DBConnect.Update("addr_ref",
  622. " description = '"+DBConnect.clean(JTCity_MunDesc.getText().toString().trim())+"' ",
  623. " country = '"+DBConnect.clean(Country_Code)+"' and" +
  624. " region = '"+DBConnect.clean(Region_Code)+"' and" +
  625. " province = '"+DBConnect.clean(Province_Code)+"' and" +
  626. " city_municipality = '"+DBConnect.clean(JTCity_MunCode.getText().toString().trim())+"'" );
  627. if(JTCity_MunDesc.getText().length() == 0){
  628. Message.messageWarning("Please Enter Description");
  629. JTCity_MunDesc.grabFocus();
  630. }else{
  631. try{
  632. Statement stmt = mainform.dbConn.getConnection().createStatement();
  633. stmt.execute(DBConnect.beginTransaction());
  634. try {
  635. tableMain.setInsert(update);
  636. } catch (IllegalStateException e2) {
  637. } catch (SQLException e2) {
  638. }
  639. try {
  640. tableMain.setQuery(defaultQuery);
  641. } catch (IllegalStateException e2) {
  642. } catch (SQLException e2) {
  643. }
  644. reload();
  645. JOptionPane.showMessageDialog(null, "Record was successfully updated");
  646. try{
  647. // startup();
  648. startupAfterSaving();
  649. }catch (NullPointerException ill) { }
  650. stmt.execute(DBConnect.commitTransaction());
  651. stmt.close();
  652. }catch(Exception excp){}
  653. }
  654. }catch(Exception excp){}
  655. }
  656. }
  657. });
  658. }
  659. return jbtnadd;
  660. }
  661.  
  662. private JButton getjEdit() {
  663. if (jbtnedit == null) {
  664. jbtnedit = new JButton();
  665. jbtnedit.setText("Edit");
  666. jbtnedit.setBounds(new Rectangle(130, 170, 90, 25));
  667. jbtnedit.setMnemonic('E');
  668. jbtnedit.addActionListener(new java.awt.event.ActionListener() {
  669. public void actionPerformed(java.awt.event.ActionEvent e) {
  670. if(jbtnedit.getText().toString().equalsIgnoreCase("Edit") == true){
  671. try{
  672. mainform.desktop.add(new EditCityMunicipality());
  673. }catch(Exception excp){}
  674. }else if(jbtnedit.getText().toString().equalsIgnoreCase("Delete") == true){
  675. jbtnedit.setMnemonic('D');
  676. try{
  677. int setDialog = Message.messageYesNo("Are you sure you want to delete this record ?");
  678. if(JOptionPane.YES_OPTION == setDialog){
  679. String del = DBConnect.delete("addr_ref",
  680. " country = '"+DBConnect.clean(Country_Code)+"' and" +
  681. " region = '"+DBConnect.clean(Region_Code)+"' and" +
  682. " province = '"+DBConnect.clean(Province_Code)+"' and" +
  683. " city_municipality = '"+DBConnect.clean(JTCity_MunCode.getText().toString().trim())+"'" );
  684. try{
  685. Statement stmt = mainform.dbConn.getConnection().createStatement();
  686. stmt.execute(DBConnect.beginTransaction());
  687. try {
  688. tableMain.setInsert(del);
  689. } catch (IllegalStateException e2) {
  690. } catch (SQLException e2) {
  691. }
  692. try {
  693. tableMain.setQuery(defaultQuery);
  694. } catch (IllegalStateException e2) {
  695. } catch (SQLException e2) {
  696. }
  697. reload();
  698. JOptionPane.showMessageDialog(null, "Record was successfully Deleted");
  699. try{
  700. startup();
  701. }catch (NullPointerException ill) {}
  702. stmt.execute(DBConnect.commitTransaction());
  703. stmt.close();
  704. }catch (Exception exp) {}
  705. }
  706. }catch(Exception excp){}
  707. }
  708. }
  709. });
  710. }
  711. return jbtnedit;
  712. }
  713.  
  714. private JButton getjExit() {
  715. if (jbtnExit == null) {
  716. jbtnExit = new JButton();
  717. jbtnExit.setText("Cancel");
  718. jbtnExit.setBounds(new Rectangle(230, 170, 90, 25));
  719. jbtnExit.setMnemonic('C');
  720. jbtnExit.addActionListener(new java.awt.event.ActionListener() {
  721. public void actionPerformed(java.awt.event.ActionEvent e) {
  722. if(jbtnExit.getText().toString().equalsIgnoreCase("Cancel") == true){
  723. try{
  724. jbtnExit.setMnemonic('C');
  725. tableMain.setQuery(defaultQuery);
  726. startup();
  727. }catch(IllegalArgumentException ill){} catch (IllegalStateException e1) {
  728. // TODO Auto-generated catch block
  729. e1.printStackTrace();
  730. } catch (SQLException e1) {
  731. // TODO Auto-generated catch block
  732. e1.printStackTrace();
  733. }
  734. }
  735. }
  736. });
  737. }
  738. return jbtnExit;
  739. }
  740.  
  741. private static void queryCountry(){
  742. JC_country.addItem("(Choose)");
  743. try {
  744. String query = DBConnect.Select("addr_ref", "country, description",
  745. "region = ''", "country");
  746. Statement stmt = DBConnect.getConnection().createStatement();
  747. ResultSet rs = stmt.executeQuery(query);
  748. while (rs.next()){
  749. JC_country.addItem(rs.getString(1)+"-"+rs.getString(2));
  750. }
  751. rs.close();
  752. stmt.close();
  753. } catch (SQLException e) {
  754. e.printStackTrace();
  755. }
  756. }
  757.  
  758. private static void queryRegion(){
  759. JC_region.addItem("(Choose)");
  760. try {
  761. String query = DBConnect.Select("addr_ref", "region, description",
  762. "country ='"+Country_Code +"'" +
  763. " and region != '' " +
  764. "and province = ''", "region");
  765. Statement stmt = DBConnect.getConnection().createStatement();
  766. ResultSet rs = stmt.executeQuery(query);
  767.  
  768. while (rs.next()){
  769. JC_region.addItem(rs.getString(1)+"-"+rs.getString(2));
  770. }
  771. rs.close();
  772. stmt.close();
  773. } catch (SQLException e) {
  774. e.printStackTrace();
  775. }
  776. }
  777.  
  778. private static void queryProvince(){
  779. JC_province.addItem("(Choose)");
  780. try {
  781. String query = DBConnect.Select("addr_ref", "province, description",
  782. "country ='"+Country_Code +"'" +
  783. " and region = '"+Region_Code+"'" +
  784. " and province != '' " +
  785. "and city_municipality = ''", "province");
  786. Statement stmt = DBConnect.getConnection().createStatement();
  787. ResultSet rs = stmt.executeQuery(query);
  788. while (rs.next()){
  789. JC_province.addItem(rs.getString(1)+"-"+rs.getString(2));
  790. }
  791. rs.close();
  792. stmt.close();
  793. } catch (SQLException e) {
  794. e.printStackTrace();
  795. }
  796. }
  797.  
  798. private void queryexist(){
  799. try {
  800. String Filter_By_Code = "select city_municipality,description from addr_ref " +
  801. "where country = '"+Country_Code +"'" +
  802. "and"+
  803. " region = '"+Region_Code +"'" +
  804. "and"+
  805. " province = '"+Province_Code +"'" +
  806. "and"+
  807. " city_municipality = '"+JTCity_MunCode.getText().toString().trim()+"'" +
  808. "and"+
  809. " brgy_village = '' " +
  810. "and"+
  811. " street = '' ";
  812. Statement stmt = DBConnect.getConnection().createStatement();
  813. ResultSet rs = stmt.executeQuery(Filter_By_Code);
  814. if(rs.last() == false){
  815. exist = false;
  816. }else{
  817. exist = true;
  818. }
  819. rs.close();
  820. }
  821. catch(Exception e){
  822. e.printStackTrace();
  823. }
  824. }
  825.  
  826. static void getRecordE(){
  827. try{
  828. String query = " SELECT " +
  829. " country, region," +
  830. " province, city_municipality, description" +
  831. " FROM addr_ref " +
  832. " WHERE brgy_village = '' and street = '' " +
  833. " AND"+
  834. " country != '' " +
  835. " AND"+
  836. " region != '' " +
  837. " AND"+
  838. " province != '' " +
  839. " AND"+
  840. " city_municipality != '' ";
  841. Statement stmt = DBConnect.getConnection().createStatement();
  842. ResultSet rs;
  843. rs = mainform.stat.executeQuery(query);
  844. rs.next();
  845. rs.absolute(xRecordE);
  846. setFieldsE(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4),rs.getString(5));
  847. rs.close();
  848. stmt.close();
  849. }catch (Exception e) {
  850. }
  851. }
  852.  
  853. private static void setFieldsE(String country, String region, String province, String city, String desc){
  854. try{
  855. JC_country.setSelectedItem(queryCountry_Code_For_Editting(country));
  856. JC_region.setSelectedItem(queryRegion_Code_For_Editting(region));
  857. JC_province.setSelectedItem(queryProvince_Code_For_Editting(province));
  858. JTCity_MunCode.setText(city);
  859. JTCity_MunDesc.setText(desc);
  860. }catch (Exception e) {
  861. }
  862. }
  863.  
  864. private void startup(){
  865. queryCountry();
  866. jbtnadd.setText("Add");
  867. jbtnadd.setMnemonic('A');
  868. jbtnedit.setText("Edit");
  869. jbtnedit.setMnemonic('E');
  870. jbtnadd.setEnabled(true);
  871. jbtnedit.setEnabled(true);
  872. jbtnExit.setEnabled(true);
  873. JC_country.setSelectedIndex(0);
  874. JC_region.setSelectedIndex(0);
  875. JC_province.setSelectedIndex(0);
  876. JC_country.setEnabled(false);
  877. JC_region.setEnabled(false);
  878. JC_province.setEnabled(false);
  879. JTCity_MunCode.setText("");
  880. JTCity_MunDesc.setText("");
  881. JTCity_MunCode.setEnabled(false);
  882. JTCity_MunDesc.setEnabled(false);
  883. }
  884.  
  885. private void startupAfterSaving(){
  886. // queryCountry();
  887. jbtnadd.setText("Add");
  888. jbtnadd.setMnemonic('A');
  889. jbtnedit.setText("Edit");
  890. jbtnedit.setMnemonic('E');
  891. jbtnadd.setEnabled(true);
  892. jbtnedit.setEnabled(true);
  893. jbtnExit.setEnabled(true);
  894. // JC_country.setSelectedIndex(0);
  895. // JC_region.setSelectedIndex(0);
  896. // JC_province.setSelectedIndex(0);
  897. JC_country.setEnabled(false);
  898. JC_region.setEnabled(false);
  899. JC_province.setEnabled(false);
  900. JTCity_MunCode.setText("");
  901. JTCity_MunDesc.setText("");
  902. JTCity_MunCode.setEnabled(false);
  903. JTCity_MunDesc.setEnabled(false);
  904. }
  905.  
  906. private void Enabled(){
  907. jbtnadd.setText("Save");
  908. jbtnadd.setMnemonic('S');
  909. jbtnedit.setEnabled(false);
  910. jbtnExit.setText("Cancel");
  911. JC_country.setEnabled(true);
  912. JC_region.setEnabled(true);
  913. JC_province.setEnabled(true);
  914. JTCity_MunCode.setEnabled(true);
  915. JTCity_MunDesc.setEnabled(true);
  916. JTCity_MunCode.setText("");
  917. JTCity_MunDesc.setText("");
  918. }
  919.  
  920. private static String queryCountry_Code_For_Editting(String CountryCode ){
  921. String County_code_desc = null;
  922. String query = DBConnect.Select("addr_ref", "country, description",
  923. "country ='"+CountryCode+"'" +
  924. " and region = ''", "country");
  925. try {
  926. Statement stmt = DBConnect.getConnection().createStatement();
  927. ResultSet rs = stmt.executeQuery(query);
  928. if (rs.next()){
  929. County_code_desc = rs.getString(1)+"-"+rs.getString(2);
  930. }
  931. stmt.execute(DBConnect.commitTransaction());
  932. stmt.close();
  933. rs.close();
  934. }
  935. catch(Exception e1){
  936. System.out.println(e1.getMessage());
  937. Message.messageError("Insufficient Data!");
  938. }
  939. return County_code_desc;
  940. }
  941.  
  942. private static String queryRegion_Code_For_Editting(String RegionCode ){
  943. String Region_code_desc = null;
  944. String query = DBConnect.Select("addr_ref", "region, description",
  945. "country ='"+Country_Code +"'" +
  946. " and region = '"+RegionCode +"'" +
  947. " and province = ''", "region");
  948. try {
  949. Statement stmt = DBConnect.getConnection().createStatement();
  950. ResultSet rs = stmt.executeQuery(query);
  951. if (rs.next()){
  952. Region_code_desc = rs.getString(1)+"-"+rs.getString(2);
  953. }
  954. stmt.execute(DBConnect.commitTransaction());
  955. stmt.close();
  956. rs.close();
  957. }
  958. catch(Exception e1){
  959. System.out.println(e1.getMessage());
  960. Message.messageError("Insufficient Data!");
  961. }
  962. return Region_code_desc;
  963. }
  964.  
  965. private static String queryProvince_Code_For_Editting(String ProvinceCode ){
  966. String Province_code_desc = null;
  967. String query = DBConnect.Select("addr_ref", "province, description",
  968. "country ='"+Country_Code +"'" +
  969. " and region = '"+Region_Code+"'" +
  970. " and province = '"+ProvinceCode+"'" +
  971. " and city_municipality = ''", "province");
  972. try {
  973. Statement stmt = DBConnect.getConnection().createStatement();
  974. ResultSet rs = stmt.executeQuery(query);
  975. if (rs.next()){
  976. Province_code_desc = rs.getString(1)+"-"+rs.getString(2);
  977. }
  978. stmt.execute(DBConnect.commitTransaction());
  979. stmt.close();
  980. rs.close();
  981. }
  982. catch(Exception e1){
  983. System.out.println(e1.getMessage());
  984. Message.messageError("Insufficient Data!");
  985. }
  986. return Province_code_desc;
  987. }
  988. }//end of class
  989.  
  990. /*
  991.  * 05091987revilogadnul
  992.  * 2009
  993.  * Applied Ideas
  994.  * Maintenance for City Municipality
  995.  */
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC