1 Replies - 195 Views - Last Post: 17 April 2019 - 03:43 AM Rate Topic: -----

#1 awthz   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 70
  • Joined: 13-February 17

Multiple processes in different JFrame at the same time

Posted 17 April 2019 - 03:36 AM

Hey Guys.

Good day,

I was just wondering on how to make my JFrame activated even if I did choose another task to process other command.
For example I have 3 branches of ATM bank and would like to open first 1 and 2 ATM at the same time that can do the same task.
I did the Thread method however it doesnt work on me.
did I miss something on this?

This is the codes of the program

package task3;

import javax.swing.*;
import javax.swing.border.EmptyBorder;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;


public class units extends JFrame{
	private JLabel lblNewLabel;
	private JButton manilaButton;
	private JButton cebuButton;
	private JButton davaoButton;

	private JFrame frame;
	public static JTextField txtBankBranch;
	public JTextField amountTextField;
	public JTextField balanceTextField;
	private JButton depositButton;
	private JButton withdrawButton;
	private JButton cancelButton;
	public int balanceAmount;
	public int transAmount;
	public int result;

	

	


	
	public units() {
		
		super("BANKO DE ORO");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JLabel lblBdo = new JLabel("BDO");
		lblBdo.setForeground(new Color(0, 102, 153));
		lblBdo.setFont(new Font("Arial Black", Font.BOLD, 60));
		lblBdo.setHorizontalAlignment((int) CENTER_ALIGNMENT);
		contentPane.add(lblBdo, BorderLayout.NORTH);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.CENTER);
		
		JPanel panel_3 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_3.getLayout();
		panel_1.add(panel_3);
		
		JLabel lblChooseABranch = new JLabel("we find ways");
		panel_3.add(lblChooseABranch);
		lblChooseABranch.setFont(new Font("Tahoma", Font.PLAIN, 20));
		
		JPanel panel_4 = new JPanel();
		panel_1.add(panel_4);
		
		JLabel lblNewLabel = new JLabel("Pick branch for your transactions");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel_4.add(lblNewLabel);
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);
		
		
		
		manilaButton = new JButton("Manila Branch");
		manilaButton.setFont(new Font("Tahoma", Font.BOLD, 15));
		panel_2.add(manilaButton);
		
		cebuButton = new JButton("Cebu Branch");
		cebuButton.setFont(new Font("Tahoma", Font.BOLD, 15));
		panel_2.add(cebuButton);
		
		davaoButton = new JButton("Davao Branch");
		davaoButton.setFont(new Font("Tahoma", Font.BOLD, 15));
		panel_2.add(davaoButton);
		
		
		btnAct btn = new btnAct();
		manilaButton.addActionListener(btn);
		cebuButton.addActionListener(btn);
		//calthread.t3.start();
		davaoButton.addActionListener(btn);
		//calthread.t3.start();
		
		

	}
	
	
	public void initialize() {
		frame = new JFrame();
		frame.setSize(450,350);
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		int x = (screenSize.width - frame.getWidth())/2;
		int y = (screenSize.height - frame.getHeight())/2;
		frame.setLocation(x,y);		
		frame.setVisible(true);
	//	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel topPanel = new JPanel();
		frame.getContentPane().add(topPanel, BorderLayout.NORTH);
		
		JPanel subtop1 = new JPanel();
		topPanel.add(subtop1);
		
		lblNewLabel = new JLabel("BDO");
		lblNewLabel.setForeground(new Color(0, 102, 204));
		lblNewLabel.setFont(new Font("Arial", Font.BOLD, 50));
		subtop1.add(lblNewLabel);
		
		JPanel subtop2 = new JPanel();
		topPanel.add(subtop2);
		
		txtBankBranch = new JTextField("BANK Branch");
		txtBankBranch.setEditable(false);
		//txtBankBranch.setText("BANK BRANCH");
		txtBankBranch.setFont(new Font("Arial", Font.BOLD, 20));
		subtop2.add(txtBankBranch);
		txtBankBranch.setColumns(10);
		
		JPanel midPanel = new JPanel();
		frame.getContentPane().add(midPanel, BorderLayout.CENTER);
		
		JPanel submid2 = new JPanel();
		midPanel.add(submid2);
		
		JLabel lblNewLabel_2 = new JLabel("Account Balance");
		lblNewLabel_2.setFont(new Font("Arial", Font.PLAIN, 15));
		submid2.add(lblNewLabel_2);
		
		balanceTextField = new JTextField();
		balanceTextField.setText("6000");
		balanceTextField.setEditable(false);
		balanceTextField.setFont(new Font("Arial", Font.BOLD, 15));
		submid2.add(balanceTextField);
		balanceTextField.setColumns(15);
		String balance = balanceTextField.getText();
		balanceAmount = Integer.parseInt(balance);
		System.out.println(balanceAmount);
		
		
		
		JPanel submid3 = new JPanel();
		FlowLayout fl_submid3 = (FlowLayout) submid3.getLayout();
		fl_submid3.setVgap(20);
		midPanel.add(submid3);
		
		JLabel lblNewLabel_1 = new JLabel("Amount:");
		lblNewLabel_1.setFont(new Font("Arial", Font.BOLD, 20));
		midPanel.add(lblNewLabel_1);
		
		JPanel submid1 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) submid1.getLayout();
		flowLayout.setVgap(15);
		flowLayout.setHgap(75);
		submid1.setBackground(new Color(153, 153, 255));
		midPanel.add(submid1);
		
		amountTextField = new JTextField();
		amountTextField.setFont(new Font("Arial", Font.BOLD, 30));
		submid1.add(amountTextField);
		amountTextField.setColumns(10);
		
		//System.out.println(transAmount);
		
		JPanel botPanel = new JPanel();
		frame.getContentPane().add(botPanel, BorderLayout.SOUTH);
		
		withdrawButton = new JButton("Withdraw");
		withdrawButton.setFont(new Font("Arial", Font.BOLD, 15));
		botPanel.add(withdrawButton);
		
		depositButton = new JButton("Deposit");
		depositButton.setFont(new Font("Arial", Font.BOLD, 15));
		botPanel.add(depositButton);
		
		cancelButton = new JButton("Cancel");
		cancelButton.setFont(new Font("Arial", Font.BOLD, 15));
		botPanel.add(cancelButton);
		
		
		btnAct1 btn = new btnAct1();
		
		withdrawButton.addActionListener(btn);
		depositButton.addActionListener(btn);
		cancelButton.addActionListener(btn);
		 
		
		
	}
	
	
	private class btnAct implements ActionListener{

		calculate calthread = new calculate();
		
		//branch branchObj = new branch();

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

			if (e.getSource()==manilaButton) {
				//calthread.t3.start();
				initialize();
				txtBankBranch.setText(manilaButton.getLabel());
			

			} else if (e.getSource()==cebuButton) {
				initialize();
				//calthread.t3.start();
				txtBankBranch.setText(cebuButton.getLabel());
				
			} else if (e.getSource()==davaoButton) {
				initialize();
				//calthread.t3.start();
				txtBankBranch.setText(davaoButton.getLabel());
				
			}
		}	
	}
	
	
	
	private class btnAct1 implements ActionListener{
		calculate cal = new calculate();

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

			if (e.getSource()==withdrawButton) {
				if (amountTextField != null ) {
				cal.atm();
				}else {
					JOptionPane.showMessageDialog(null, "Sorry! You didn't put any amount");
					frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
				}
				
			} else if (e.getSource()==depositButton) {
				cal.atm1();
				//initialize();
				//txtBankBranch.setText(cebuButton.getLabel());
				//String balance1 = amountTextField.getText();
				//transAmount = Integer.parseInt(balance1);
				//result = balanceAmount + transAmount;
				//System.out.println(result);
				
			} else if (e.getSource()==cancelButton) {
				frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
				//initialize();
				//txtBankBranch.setText(davaoButton.getLabel());
				
			}
			//String result1 = Integer.toString(result);
			//balanceTextField.setText(result1);
		}	
	}
	
	public class Calc {

		//units unitObj = new units();
		
		public void withdraw() {
			result = balanceAmount - transAmount;
		}
		
		public void deposit() {
			result = balanceAmount + transAmount;
		}
		/*
		public void shit() {
			initialize();
			
		}
		*/
	}
	

	
	
	class calculate{
		
		Calc calc = new Calc();
		
		Thread t1 = new Thread(new Runnable() {

			@Override
			public void run() {
				calc.withdraw();
				
			}
			
		});
		
		Thread t2 = new Thread(new Runnable() {

			@Override
			public void run() {
				calc.deposit();
				
				
			}
			
		});
		/*
		Thread t3 = new Thread(new Runnable() {

			@Override
			public void run() {
				calc.shit();
				
			}
			
			
		});
		
		*/
		
		void atm() {
			
			calculate calthread = new calculate();
			
			
			String balance1 = amountTextField.getText();
			transAmount = Integer.parseInt(balance1);
			calthread.t1.start();
			//System.out.println(result);
			
			
			int returnValue = 0;
	    	returnValue = JOptionPane.showConfirmDialog(null, "Would you like to withdraw " + transAmount  +" ?", "Withdraw Transaction", JOptionPane.YES_NO_OPTION);
	    		
	    	if (returnValue == JOptionPane.YES_OPTION) {
	    		String resFinal = Integer.toString(result);
				balanceTextField.setText(resFinal);
				//System.out.println(result);
				JOptionPane.showMessageDialog(null, "Transaction Successful");
				amountTextField.setText("");
	   
	    	}
	    		
	    	else if (returnValue == JOptionPane.NO_OPTION) {
	    		//JOptionPane.showMessageDialog(null, "Thank you for using the program");
	    		amountTextField.setText("");
			
	    	}
			
		}
		
		void atm1() {
			
			calculate calthread = new calculate();
			
			
			String balance1 = amountTextField.getText();
			transAmount = Integer.parseInt(balance1);
			calthread.t2.start();
			String resFinal = Integer.toString(result);
			

			
			int returnValue = 0;
	    	returnValue = JOptionPane.showConfirmDialog(null, "Would you like to deposit " + transAmount  +" ?", "Deposit Transaction", JOptionPane.YES_NO_OPTION);
	    		
	    	if (returnValue == JOptionPane.YES_OPTION) {
	    		System.out.println(result);
				balanceTextField.setText(resFinal);
				System.out.println(result);
				JOptionPane.showMessageDialog(null, "Transaction Successful");
				amountTextField.setText("");
	   
	    	}
	    		
	    	else if (returnValue == JOptionPane.NO_OPTION) {
	    		//JOptionPane.showMessageDialog(null, "Thank you for using the program");
	    		amountTextField.setText("");
			
	    	}
			
		}
		
	}
	
	
	
	
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Multiple processes in different JFrame at the same time

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3623
  • View blog
  • Posts: 16,656
  • Joined: 20-September 08

Re: Multiple processes in different JFrame at the same time

Posted 17 April 2019 - 03:43 AM

Why have you got the unnecessary (and unreal) burden of multi-tasking? How many ATMs have you used where you can make a deposit and a withdrawal at the same time?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1