Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Tolls

Thread: Optimizing code help

  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Optimizing code help

    Hello, I am making a prototype for a human resource program. I have a simple interface for creating, displaying, deleting and saving workers. When I click Add button a new JFrame opens but I wanted a simultaneous check for text fileds if they are filled so I started a new thread to check if the fields are filled. I need a java advanced coder to look at code and tell me if I am on the right direction.

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JList;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.text.*;
    import javax.swing.JFormattedTextField;
    
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowListener;
    import java.awt.event.WindowEvent;
    
    import java.awt.Rectangle;
    
    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    import java.io.PrintWriter;
    
    import java.util.ArrayList;
    import java.util.Set;
    
    public class MainFrame extends JFrame {
    				
    				JList list; // to serialize it
    				
    		
    				public MainFrame() {
    						
    						//call JFrame();
    						super();
    						setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    						setTitle("Payment program testing");
    						setSize(600, 320);
    						
    						//Buttons panel
    						JPanel buttonPanel = new JPanel();
    						
    						JButton addButton = new JButton("Add new person");
    						addButton.addActionListener(new ActionListener() {
    								@Override
    								public void actionPerformed(ActionEvent e) {
    										//iniate new thread here
    										System.out.println("Adding...");
    										JFrame adding = new JFrame();
    										//enable control on closing
    										adding.addWindowListener(new WindowListener() {
    												public void windowDeactivated(WindowEvent e) {}
    												public void windowOpened(WindowEvent e) {}
    												public void windowIconified(WindowEvent e) {}
    												public void windowDeiconified(WindowEvent e) {}
    												public void windowClosing(WindowEvent e) {}
    												public void windowActivated(WindowEvent e) {}
    												@Override
    												public void windowClosed(WindowEvent we) {
    														Set<Thread> st = Thread.getAllStackTraces().keySet();
    														Thread[] threads = st.toArray(new Thread[st.size()]);
    														for (int i=0; i < threads.length; i++) {
    																if ( threads[i].getName().equals("ADD_THREAD") ) {
    																		System.out.println(threads[i].getName() );
    																		
    																		if ( threads[i].isAlive() ) {
    																				System.out.println(threads[i].getName()+" is alive");
    																				try { threads[i].stop(); } catch (Exception tre) { }
    																		}
    				
    																}
    														
    														}
    												}
    										});
    														
    														
    														
    										adding.setLayout(new BorderLayout());
    										adding.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    										adding.setSize(320,240);
    										JPanel inputWorker = new JPanel(new GridLayout(0,1));
    										final JLabel fnamel = new JLabel("First name:");
    										final JLabel snamel = new JLabel("Second name: ");
    										final JLabel jobl = new JLabel("Job: ");
    										final JFormattedTextField fname1 = new JFormattedTextField();
    										fname1.setColumns(10);
    										final JFormattedTextField sname1 = new JFormattedTextField();
    										sname1.setColumns(10);
    										String[] occs = {"worker", "Maintenance", "engeneer", "support"};
    										JComboBox job = new JComboBox(occs);
    										//button event setup
    										final JButton addButton1 = new JButton("Add");
    										addButton1.setEnabled(false);
    										addButton1.addActionListener(new ActionListener() {
    												
    												@Override
    												public void actionPerformed(ActionEvent saveenvnt) {
    														try {
    																PrintWriter outf = new PrintWriter("empl.txt");
    																outf.println(fname1.getText()+" "+sname1.getText());
    																outf.close();
    														} catch (Exception saveex) { };
    												}
    										});
    														
    														
    										inputWorker.add(fnamel);
    										inputWorker.add(fname1);
    										inputWorker.add(snamel);
    										inputWorker.add(sname1);
    										inputWorker.add(jobl);
    										inputWorker.add(job);
    										inputWorker.add(addButton1);
    										adding.add(inputWorker);
    										adding.setVisible(true);
    										
    										
    										try {
    												new Thread(new Runnable() {
    														@Override
    														public void run() {
    																Thread.currentThread().setName("ADD_THREAD");
    																while (
    																		sname1.getText().equals("") &
    																		fname1.getText().equals("")
    																) { /*
    																		System.err.println("IInput text in fields");
    																    System.out.println("Thread is "+Thread.currentThread().toString());
    																		*/ }
    																addButton1.setEnabled(true);
    																
    														}
    												}).start();
    										} catch (Exception eeee) {}
    										  finally {   }
    										
    								}
    						});
    						
    						JButton viewButton = new JButton("View personel");
    						viewButton.addActionListener(new ActionListener() {
    								@Override
    								public void actionPerformed(ActionEvent e) {
    										//iniate new thread here
    										System.out.println("Button clicked "+e);
    								}
    						});
    						
    						
    						JButton removeButton = new JButton("Delete a person");
    						removeButton.addActionListener(new ActionListener() {
    								@Override
    								public void actionPerformed(ActionEvent e) {
    										//iniate new thread here
    								}
    						});
    						
    						
    						JButton saveButton = new JButton("Record database");
    						saveButton.addActionListener(new ActionListener() {
    								@Override
    								public void actionPerformed(ActionEvent e) {
    										//iniate new thread here
    										System.out.println("Saving object...");
    										
    										try {
    												ObjectOutputStream os = new ObjectOutputStream(
    												new FileOutputStream("TMP.ser"));
    												os.writeObject(list);
    												os.close();
    										} catch (Exception ex) { };
    								}
    						});
    						
    				
    						buttonPanel.add(addButton);
    						buttonPanel.add(viewButton);
    						buttonPanel.add(removeButton);
    						buttonPanel.add(saveButton);
    						//
    						/* JText area */
    						JPanel listPanel = new JPanel();
    						
    						String[] test1 = {"Ilian Zapryanov, worker", 
    								"test bot , worker 2"
    								};
    						list = new JList(test1);
    						list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    				
    						listPanel.add(list);
    						
    						
    						//the layout is FlowLayout
    						setLayout(new FlowLayout());
    						add(buttonPanel);
    						add(listPanel, BorderLayout.CENTER);
    						//set visible
    						setLocationRelativeTo(null); //center it
    						
    								
    						setVisible(true);
    						
    								
    				}
    				
    				
    				public static void main(String[] args) {
    						MainFrame mf = new MainFrame();
    						
    				}
    }

  2. #2
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Optimizing code help

    Bump that. Please, if you think it`s optimized enough - reply that it does not need refactory.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Optimizing code help

    A large section of it appears to be commented out. It doesn't look like it would even compile. Why are you worried about optimization? Worry about that if it compiles and runs and you can demonstrate that its performance isn't good enough.
    Get in the habit of using standard Java naming conventions!

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Optimizing code help

    The commenting out is the forum softare mucking it up.
    It's not actually commented out.
    I've seen this a few times, and it looks like it's to do with the /* ... */ sometimes confusing it.
    heatblazer likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Optimizing code help

    I am interested if the new thread is needed in my case. I mean - I`ve started a thread so it checks until window is closed or fields are filled. Was that a good idea or there is a better solution?

Similar Threads

  1. Help optimizing simple program.
    By whileloop in forum New To Java
    Replies: 4
    Last Post: 04-18-2012, 03:50 PM
  2. Optimizing paiontComponent()
    By EricB in forum Java 2D
    Replies: 1
    Last Post: 11-05-2011, 07:29 PM
  3. Still need some optimizing help for my game.
    By Alerhau in forum New To Java
    Replies: 5
    Last Post: 09-25-2011, 08:18 AM
  4. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  5. Quick Optimizing question
    By sgthale in forum New To Java
    Replies: 6
    Last Post: 04-15-2011, 01:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •