Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to merge my OO with GUI

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to merge my OO with GUI

    okay i'm rather a freshman to java programming, and is self learning by the way.
    i recently learn how to make GUI, and now i'm trying to learn how to merge the function into the GUI.
    the readwrite program was written earlier on and the learningGUIi is the latest gui i done wanting to merge with.
    so how do i get function in readwrite to merge the learning GUI.
    the only function i manage to done is the exiting, the rest seems complicated to me.
    so can anyone teach me ?

    import java.util.*;
    import java.io.*;
     
    public class readwrite
    {
     
    	public static void main(String[] args) 
    		throws FileNotFoundException{
    		int a = 0, b = 0, mode=0, line=0;
    		//read files
    		String tempstr;
    		String[][] data = new String[1000][5];
    		Scanner myscan = new Scanner(new File("customerRecord.txt"));		//read data from file, only once
    		while (myscan.hasNextLine()) 
    		{
    			tempstr = myscan.nextLine();
    			data[a] = tempstr.split(";");
    			a = a + 1;
    		}
    		myscan.close();
     
    		int[][] order = new int [line][2];		//initial order buffer
    		for (int i=0; i< line; i++)
    			for (int j=0; j<2; j++){
    				order[i][j] = 0;
    			}
     
    		String num;			//tempbuffer for menu selection input
    		Scanner cin = new Scanner(System.in);
     
    		//continuously show menu
    		while (b == 0) {	
    			System.out.println("-----------------------------------");
        		System.out.println("read/write application");	
        		System.out.println("-----------------------------------");
        		System.out.println("1. Log In");		//ok
        		System.out.println("2. Sign Up");			//ok
        		System.out.println("3. Exit the system");			//ok
        		System.out.println("-----------------------------------");
        		System.out.print("Choose an option between 1 to 3: ");
     
        		num = cin.nextLine();
        		if (num.length() == 1) {
        			char c = num.charAt(0);
        			switch (c){
     
        				case '1':
        								if (existcus(data, a, line, order)==true)
        									mode = 0;	
        							else
        								System.out.println("");
        							break;
        				case '2':	
        								if (newcus(data, a, line, order)==true)
        								{
        									a = a+1;
        									mode = 0;
        								}
        							else
        								System.out.println(""); 
        							break;
     
        				case '3':	System.out.println("Bye-bye!");
        							b = 1; 
        							break;
        				default :	System.out.println("Input error, please try again");
        							break;
        			} 	
        		}
        		else
        			System.out.println("Input error, please try again");
        		waitanykey();
    		}
    	}
     
     
    	public static boolean existcus(String[][] data, int a, int line, int[][] order)
    	{
    		Scanner cin = new Scanner(System.in);
    		Formatter fmt = new Formatter();
    		String temp, name, passwd, addr, contact;
    		int num = -1, b = -1;
    		System.out.println("-----------------------------------");
        	System.out.println("    ***Customer Information***");	
        	System.out.println("-----------------------------------");
    		System.out.print("Please enter your username: ");
    		name = cin.nextLine();
    		System.out.print("Please enter your password: ");
    		passwd = cin.nextLine();
    		if (name.length() > 0 && passwd.length() > 0)
    		{
    			for (int i=0; i<a; i++)
    			{
    				if (name.equals(data[i][1]) && passwd.equals(data[i][2]))
    					{
    					num = i;
    					break;
    					}
    			}
    			if (num != -1)
    			{
    				System.out.println("Your Information:\n");
    				fmt.format("%1$-30.30s %2$s\n", "Name:", data[num][0]);
    				fmt.format("%1$-30.30s %2$s\n", "Address:", data[num][3]);
    				fmt.format("%1$-30.30s %2$s", "Contact:", data[num][4]);
    				System.out.println(fmt);
    				while (b == -1)
    				{
    					System.out.println("\nPlease enter 0 to confirm your address and contact.");
    					System.out.println("Please enter 1 to change your address and contact.");
    					System.out.print("Your choice: ");
    					temp = cin.nextLine();
    					if (temp.length() == 1 )
    					{
    						switch (temp.charAt(0))
    						{
    							case '0':	System.out.println("Thank yout for providing yout information.");
    										System.out.println("Thanks you.");
    										System.out.println("Now you can set another order.");
    										//initial the order lists
    										for (int i=0; i< line; i++)
    											for (int j=0; j<2; j++)
    											{
    												order[i][j] = 0;
    											}
    										b = 0;
    										break;
    							case '1':	System.out.print("Address: ");
    										addr = cin.nextLine();
    										System.out.print("Contact: ");
    										contact = cin.nextLine();
    										data[num][3] = addr;
    										data[num][4] = contact;
    										//change the file
    										try
    										{
    											FileWriter fout = new FileWriter(new File("customerRecord.txt"));
    											PrintWriter cout = new PrintWriter(fout);
    											for (int i=0; i< a; i++)
    											{
    												cout.println(data[i][0]+";"+data[i][1]+";"+data[i][2]+";"+data[i][3]+";"+data[i][4]);
    											}
    											cout.close();
    											fout.close();
    										}
    										catch(Exception e4)
    										{
    											System.out.println("File writing failed.");
    										}
     
    										break;
    							default:	System.out.println("Sorry. Invalid Option. Please enter again.\n");
    										break;
    						}
    					}
    					else
    						System.out.println("Sorry. Invalid Option. Please enter again.\n");
    				}
     
    			}
    			else
    			{
    				System.out.println("There's no such username/password.");
    				System.out.println("Please try Option 3 again from the main menu.");
    				return false;
    			} 
    			return true;			
    		}
    		else
    		{
    			System.out.println("There's no such username/password.");
    			System.out.println("Please try Option 3 again from the main menu.");
    			return false;			
    		}
    	}	
     
    	public static boolean newcus(String[][] data, int a, int line, int[][] order)
    	{
    		Scanner cin = new Scanner(System.in);
    		String temp, name, usrname, passwd, addr, contact;
    		int b = 0;
    		System.out.println("-----------------------------------");
        	System.out.println("       ***New Customer***");	
        	System.out.println("-----------------------------------");	
        	System.out.println("Please provide us with your information.\n");
        	while (b == 0)
        	{
        		System.out.print("Name: ");
        		name = cin.nextLine();
        		System.out.print("Username: ");
        		usrname = cin.nextLine();
        		System.out.print("Password: ");
        		passwd = cin.nextLine();
        		System.out.print("Address: ");
        		addr = cin.nextLine();
        		System.out.print("Contact Number: ");
        		contact = cin.nextLine();
        		System.out.println("\nPlease enter 0 to confirm, enter other words to make changes");
        		System.out.print("Your choice: ");
        		temp = cin.nextLine();
        		if (temp.length() == 1 )
        		{
        			if (temp.charAt(0) == '0')
        			{
        				b = 1;
        				//change file
        				try
        				{
    						FileWriter fout = new FileWriter(new File("customerRecord.txt"), true);
    						PrintWriter cout = new PrintWriter(fout, true);
     
    						cout.append(name+";"+usrname+";"+passwd+";"+addr+";"+contact+"\n");
     
    						cout.close();
    						fout.close();
    					}
    					catch(Exception e4)
    					{
    						System.out.println("File writing failed.");
    					}
    					//change current array
    					data[a][0] = name;
    					data[a][1] = usrname;
    					data[a][2] = passwd;
    					data[a][3] = addr;
    					data[a][4] = contact;
        			}
        		}
        	}
        	System.out.println("-----------------------------------");
        	System.out.println("Thank you for providing us with your information.");
        	System.out.println("Please remember your username and password as these will be needed for furture use.");
        	for (int i=0; i< line; i++)
    			for (int j=0; j<2; j++)
    			{
    				order[i][j] = 0;
    			}
        	return true;
    	}	
     
    	//to pause the screen
    	public static void waitanykey(){
    		System.out.print("Pls press ENTER to continue...");
    		try {
    			System.in.read();	
    		}
    		catch (java.io.IOException ioe) {}
    	}
    }
    //end of program

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class learningGUI extends JPanel 
    {
     
     
    	public static void main(String[] args) 
    	{
    		JFrame frame = new JFrame("Merging with GUI");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		JPanel main = new JPanel();
    		main.setLayout(new GridLayout(3, 1, 8, 8));
    		main.setBorder(BorderFactory.createLineBorder(Color.black));
     
    		JButton Exit = new JButton("Exit");
    		Exit.setPreferredSize(new Dimension(150, 30));
    		Exit.addActionListener(new exitListener());
     
    		JPanel orderpanel1p = new JPanel();
     
    		orderpanel1p.setLayout(new GridLayout(13, 1, 1, 1));
    		orderpanel1p.setBorder(BorderFactory.createTitledBorder(""));
    		orderpanel1p.setBackground(Color.lightGray);
    		JPanel orderpanel2a = new JPanel(); orderpanel2a.setBackground(Color.lightGray); orderpanel1p.add(orderpanel2a);
     
    		JPanel orderpanel2b = new JPanel(); orderpanel2b.setLayout(new GridLayout(1, 4, 1, 1));
    		JLabel f1 = new JLabel(" Username"); orderpanel2b.setBackground(Color.lightGray);
    		JTextField loginusername = new JTextField(20);
    		JPanel q1 = new JPanel(); JPanel w1 = new JPanel(); JPanel w2 = new JPanel(); JPanel w3 = new JPanel(); 
    		q1.setBackground(Color.lightGray); w1.setBackground(Color.lightGray); w2.setBackground(Color.lightGray); w3.setBackground(Color.lightGray);
    		orderpanel2b.add(f1); orderpanel2b.add(w1); orderpanel2b.add(loginusername); orderpanel2b.add(q1); orderpanel2b.add(w2); orderpanel2b.add(w3);
    		orderpanel1p.add(orderpanel2b);
     
    		JPanel orderpanel2c = new JPanel(); orderpanel2c.setLayout(new GridLayout(1, 4, 1, 1));
    		JLabel f2 = new JLabel(" Password"); orderpanel2c.setBackground(Color.lightGray);
    		JTextField loginpassword = new JTextField(8);
    		JPanel q2 = new JPanel(); JPanel w4 = new JPanel(); JPanel w5 = new JPanel(); JPanel w6 = new JPanel(); 
    		q2.setBackground(Color.lightGray); w4.setBackground(Color.lightGray); w5.setBackground(Color.lightGray); w6.setBackground(Color.lightGray);
    		orderpanel2c.add(f2); orderpanel2c.add(w4); orderpanel2c.add(loginpassword); orderpanel2c.add(q2); orderpanel2c.add(w5); orderpanel2c.add(w6);
    		orderpanel1p.add(orderpanel2c);
     
    		JPanel orderpanel2d = new JPanel(); orderpanel2d.setLayout(new GridLayout(1, 4, 1, 1)); orderpanel2d.setBackground(Color.lightGray);
    		JButton login = new JButton("Log In");
    		//login.addActionListener(new loginButtonListener());
    		login.setPreferredSize(new Dimension(100, 20));
    		JPanel q3 = new JPanel(); JPanel q4 = new JPanel();	JPanel q5 = new JPanel(); JPanel q6 = new JPanel(); JPanel q7 = new JPanel(); JPanel q8 = new JPanel();
    		orderpanel2d.add(login);
    		orderpanel2d.add(q3); orderpanel2d.add(q4);	orderpanel2d.add(q6); orderpanel2d.add(q7); orderpanel2d.add(q8);
    		q3.setBackground(Color.lightGray); q4.setBackground(Color.lightGray); q5.setBackground(Color.lightGray); q6.setBackground(Color.lightGray); q7.setBackground(Color.lightGray); q8.setBackground(Color.lightGray);
    		orderpanel1p.add(orderpanel2d);
     
    		JPanel orderpanel2e = new JPanel(); orderpanel2e.setBackground(Color.lightGray);orderpanel1p.add(orderpanel2e);
     
    		JPanel orderpanel2f = new JPanel(); orderpanel2f.setLayout(new GridLayout(1, 4, 1, 1));
    		JLabel f3 = new JLabel(" Name "); orderpanel2f.setBackground(Color.lightGray);
    		JTextField name = new JTextField(20);
    		JPanel q9 = new JPanel(); JPanel w7 = new JPanel(); JPanel w8 = new JPanel(); JPanel w9 = new JPanel();
    		q9.setBackground(Color.lightGray); w7.setBackground(Color.lightGray); w8.setBackground(Color.lightGray); w9.setBackground(Color.lightGray);
    		orderpanel2f.add(f3); orderpanel2f.add(w7); orderpanel2f.add(name); orderpanel2f.add(q9); orderpanel2f.add(w8); orderpanel2f.add(w9);
    		orderpanel1p.add(orderpanel2f);
     
    		JPanel orderpanel2g = new JPanel(); orderpanel2g.setLayout(new GridLayout(1, 4, 1, 1));
    		JLabel f4 = new JLabel(" Username "); orderpanel2g.setBackground(Color.lightGray);
    		JTextField uname = new JTextField(20);
    		JPanel q10 = new JPanel(); JPanel w10 = new JPanel(); JPanel w11 = new JPanel(); JPanel w12 = new JPanel(); 
    		q10.setBackground(Color.lightGray); w10.setBackground(Color.lightGray); w11.setBackground(Color.lightGray); w12.setBackground(Color.lightGray);
    		orderpanel2g.add(f4); orderpanel2g.add(w10); orderpanel2g.add(uname); orderpanel2g.add(q10); orderpanel2g.add(w11); orderpanel2g.add(w12);
    		orderpanel1p.add(orderpanel2g);
     
    		JPanel orderpanel2h = new JPanel(); orderpanel2h.setLayout(new GridLayout(1, 4, 1, 1));
    		JLabel f5 = new JLabel(" Password "); orderpanel2h.setBackground(Color.lightGray);
    		JTextField pword = new JTextField(20);
    		JPanel q11 = new JPanel(); JPanel w13 = new JPanel(); JPanel w14 = new JPanel(); JPanel w15 = new JPanel();
    		q11.setBackground(Color.lightGray); w13.setBackground(Color.lightGray); w14.setBackground(Color.lightGray); w15.setBackground(Color.lightGray);
    		orderpanel2h.add(f5); orderpanel2h.add(w13); orderpanel2h.add(pword); orderpanel2h.add(q11); orderpanel2h.add(w14); orderpanel2h.add(w15);
    		orderpanel1p.add(orderpanel2h);
     
    		JPanel orderpanel2i = new JPanel(); orderpanel2i.setLayout(new GridLayout(1, 4, 0, 0));
    		JLabel f6 = new JLabel(" Address "); orderpanel2i.setBackground(Color.lightGray);
    		JTextField address = new JTextField(50);
    		JPanel q12 = new JPanel(); q12.setBackground(Color.lightGray);
    		orderpanel2i.add(f6); orderpanel2i.add(address); orderpanel2i.add(q12);
    		orderpanel1p.add(orderpanel2i);
     
    		JPanel orderpanel2j = new JPanel(); orderpanel2j.setLayout(new GridLayout(1, 4, 1, 1));
    		JLabel f7 = new JLabel(" Contact "); orderpanel2j.setBackground(Color.lightGray);
    		JTextField contact = new JTextField(20);
    		JPanel q13 = new JPanel(); JPanel w16 = new JPanel(); JPanel w17 = new JPanel(); JPanel w18 = new JPanel(); 
    		q13.setBackground(Color.lightGray); w16.setBackground(Color.lightGray); w17.setBackground(Color.lightGray); w18.setBackground(Color.lightGray);
    		orderpanel2j.add(f7); orderpanel2j.add(w16); orderpanel2j.add(contact); orderpanel2j.add(q13); orderpanel2j.add(w17); orderpanel2j.add(w18); 
    		orderpanel1p.add(orderpanel2j);
     
    		JPanel orderpanel2k = new JPanel(); orderpanel2k.setBackground(Color.lightGray);orderpanel1p.add(orderpanel2k);
     
    		JPanel orderpanel2l = new JPanel(); orderpanel2l.setBackground(Color.lightGray); orderpanel2l.setLayout(new GridLayout(1, 1, 1, 1));
    		JLabel f8 = new JLabel(" Press the confirm button when you are satisfied that all information are correct.", SwingConstants.LEFT);
    		orderpanel2l.add(f8);
    		orderpanel1p.add(orderpanel2l);
     
    		JPanel orderpanel2m = new JPanel(); orderpanel2m.setLayout(new GridLayout(1, 4, 1, 1)); orderpanel2m.setBackground(Color.lightGray);
    		JButton confirm = new JButton("Confirm");
    		JPanel q14 = new JPanel(); JPanel q15 = new JPanel(); JPanel q16 = new JPanel(); JPanel q17 = new JPanel(); JPanel q18 = new JPanel(); orderpanel2m.add(confirm);
    		q14.setBackground(Color.lightGray); q15.setBackground(Color.lightGray); q16.setBackground(Color.lightGray);
    		q17.setBackground(Color.lightGray); q18.setBackground(Color.lightGray);
    		orderpanel2m.add(q14); orderpanel2m.add(Exit); orderpanel2m.add(q16); orderpanel2m.add(q17); orderpanel2m.add(q18); 
    		orderpanel1p.add(orderpanel2m);
     
    		main.add(orderpanel1p);
     
    		frame.add(orderpanel1p);
    		frame.pack();
    		frame.setSize(750, 350);
    		frame.setVisible(true);
     
    	}
     
    public static class exitListener implements ActionListener 
    {
    	public void actionPerformed(ActionEvent event) 
    	{
    		System.exit(0);
    	}
    }
     
     
     
     
    }


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to merge my OO with GUI

    Hi Firstly to get to grips with GUI's use netbeans or eclipse (with the jigloo GUI plugin) What you then aim to do is call methods just like you would in your main program. To do this you could add the following code to the click event of a button.

    readwrite readWrite = new readwrite(); // add any args that are needed between the ()
    readWrite. newcus(data, a, line, order);

    So the program always uses the same class decalre the class reawrite as public so the entire scope of the class can access it then just call readWrite where appropriate.

    On a side note always ensure class's start with capital letters and variables with lowercase with the second word being uppercase such as 'readWrite'; hope this helps a bit, cheers

Similar Threads

  1. Merge 2 Sorted Text Files into a Third
    By Epyllion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 10th, 2010, 07:24 PM