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 9 of 9

Thread: how to split a jframe as parent class with the class of actionListener, itemListener, etc...

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to split a jframe as parent class with the class of actionListener, itemListener, etc...

    I'm working on a program using GUI. My problem is that I'm reaching **1000 Line** and this is stressful because all my code are in one file. I'm not liking this. So I research the internet about Inheritance. However, I'm not sure how this can help me in my problem because all what I know about Inheritance that Inherit everything in the parent class. However, when I try to use a variables in the child class to override it in the parent class I can't for example let say that I have in the parent class something like this:

    JButton getButton = new JButton("Enter");

    now when I go to the child class. I want to Inherit and use this variable so I can use the ActionListener on the getButton and override for the parent class, but it's not working.

    So could any tell me what I'm I doing wrong here, or this not the right way to do it ?

    More info about my code:

    This is my code:

     
    public class H_Store extends JFrame {
     
    	private String s ="Kinds: ";
    	private JButton calculateButton;
    	private JPanel mainPane;
    	private JPanel getProfitPanel;
    	private JTextField ground_M_QTextField;
    	private JTextField chuck_M_QTextField;
    	private JTextField rib_M_QTextField;
    	private JTextField steak_M_QTextField;
    	private JTextField flank_M_QTextField;
    	private JTextField round_M_QTextField;
    	private JTextField ground_M_WTextField;
    	private JTextField chuck_M_WTextField;
    	private JTextField rib_M_WTextField;
    	private JTextField steak_M_WTextField;
    	private JTextField flank_M_WTextField;
    	private JTextField round_M_WTextField;
    	private JTextField textField_13;
    	private JTextField textField_14;
    	private JTextField textField_15;
    	private JTextField textField_16;
    	private JTextField textField_17;
    	private JTextField textField_18;
    	private JTextField textField_19;
    	private JTextField textField_20;
    	private JTextField textField_21;
    	private JTextField textField_22;
    	private JTextField textField_23;
    	private JTextField textField_24;
    	private JTextField textField_26;
    	private JTextField textField_27;
    	private JTextField textField_28;
    	private JTextField textField_29;
    	private JTextField textField_30;
    	private JTextField textField_31;
    	private JTextField at_M_QTextField;
    	private JTextField at_M_WTextField;
    	private JTextField textField_12;
    	private JTextField textField_34;
    	private JTextField textField_35;
    	private JTextField otherThings_AnotherTypeTextField;
    	private JTextField textField_36;
    	private JTextArea txtrWriteYourComment;
    	private JTextField at_meat_TextField;
    	private JLabel lblPrice;
    	private JTextField ground_M_PTextField;
    	private JTextField rib_M_PTextField;
    	private JTextField chuck_M_PTextField;
    	private JTextField steak_M_PTextField;
    	private JTextField flank_M_PTextField;
    	private JTextField round_M_PTextField;
    	private JTextField at_M_PTextField;
    	private JLabel label_5;
    	private JTextField textField_7;
    	private JTextField textField_8;
    	private JTextField textField_9;
    	private JTextField textField_10;
    	private JTextField textField_11;
    	private JTextField textField_25;
    	private JTextField textField_32;
    	private JLabel label_6;
    	private JTextField textField_33;
    	private JTextField textField_37;
    	private JTextField textField_38;
    	private JTextField textField_39;
    	private JTextField textField_40;
    	private JTextField textField_41;
    	private JTextField textField_42;
    	private JButton getProfitJButton;
    	private JFileChooser profitJFileChooser;
    	private JPanel profitPanel;
    	private JTextField profitFileTextField;
    	private JButton profitFileButton;
    	private JButton sellFileButtton;
     
    	private JCheckBox groundMeatCheckBox;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					H_Store frame = new H_Store();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public H_Store() {
    		super("Hussin Store");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 1378, 657);
     
    		JMenuBar menuBar = new JMenuBar();
    		setJMenuBar(menuBar);
     
    		JMenu mnFile = new JMenu("File");
    		menuBar.add(mnFile);
     
    		JMenu mnHelp = new JMenu("Help");
    		menuBar.add(mnHelp);
    		mainPane = new JPanel();
    		mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(mainPane);
    		mainPane.setLayout(new GridLayout(0, 2, 0, 0));
     
     
    		JPanel meetPanel = new JPanel();
    		meetPanel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
    		mainPane.add(meetPanel);
     
    		JLabel meatLabel = new JLabel("*Meat*");
    		meatLabel.setBounds(546, 101, 79, 77);
    		meatLabel.setHorizontalAlignment(SwingConstants.LEFT);
    		meatLabel.setFont(new Font("Times New Roman", Font.PLAIN, 25));
     
    		///////////////////
     
    		JCheckBox groundMeatCheckBox = new JCheckBox("Ground ");
     
    		groundMeatCheckBox.setBounds(8, 71, 113, 25);
    		groundMeatCheckBox.setVerticalAlignment(SwingConstants.BOTTOM);
    		groundMeatCheckBox.setHorizontalAlignment(SwingConstants.LEFT);
    		meetPanel.setLayout(null);
    		meetPanel.add(meatLabel);
    		meetPanel.add(groundMeatCheckBox);
     
    		JCheckBox chuckMeatCheckBox = new JCheckBox("Chuck");
    		chuckMeatCheckBox.setBounds(8, 126, 113, 25);
    		meetPanel.add(chuckMeatCheckBox);
     
    		JCheckBox ribMeatCheckBox = new JCheckBox("Rib");
    		ribMeatCheckBox.setBounds(8, 101, 113, 25);
    		meetPanel.add(ribMeatCheckBox);
     
    		JCheckBox steakMeatCheckBox = new JCheckBox("Steak");
    		steakMeatCheckBox.setBounds(8, 153, 120, 25);
    		meetPanel.add(steakMeatCheckBox);
     
    		JCheckBox flankMeatCheckBox = new JCheckBox("Flank");
    		flankMeatCheckBox.setBounds(8, 183, 113, 25);
    		meetPanel.add(flankMeatCheckBox);
     
    		JCheckBox roundMeatCheckBox = new JCheckBox("Round");
    		roundMeatCheckBox.setBounds(8, 213, 113, 25);
    		meetPanel.add(roundMeatCheckBox);
     
    		ground_M_QTextField = new JTextField(10);
    		ground_M_QTextField.setText("0");
    		ground_M_QTextField.setBounds(155, 76, 18, 16);
    		meetPanel.add(ground_M_QTextField);
    		ground_M_QTextField.setColumns(10);
     
    		chuck_M_QTextField = new JTextField();
    		chuck_M_QTextField.setText("0");
    		chuck_M_QTextField.setColumns(10);
    		chuck_M_QTextField.setBounds(155, 105, 18, 16);
    		meetPanel.add(chuck_M_QTextField);
     
    		rib_M_QTextField = new JTextField();
    		rib_M_QTextField.setText("0");
    		rib_M_QTextField.setColumns(10);
    		rib_M_QTextField.setBounds(155, 130, 18, 16);
    		meetPanel.add(rib_M_QTextField);
     
    		steak_M_QTextField = new JTextField();
    		steak_M_QTextField.setText("0");
    		steak_M_QTextField.setColumns(10);
    		steak_M_QTextField.setBounds(155, 157, 18, 16);
    		meetPanel.add(steak_M_QTextField);
     
    		flank_M_QTextField = new JTextField();
    		flank_M_QTextField.setText("0");
    		flank_M_QTextField.setColumns(10);
    		flank_M_QTextField.setBounds(155, 187, 18, 16);
    		meetPanel.add(flank_M_QTextField);
     
    		round_M_QTextField = new JTextField();
    		round_M_QTextField.setText("0");
    		round_M_QTextField.setColumns(10);
    		round_M_QTextField.setBounds(155, 217, 18, 16);
    		meetPanel.add(round_M_QTextField);
     
    		JLabel lblType = new JLabel("Type:");
    		lblType.setFont(new Font("Tahoma", Font.BOLD, 13));
    		lblType.setBounds(8, 46, 44, 16);
    		meetPanel.add(lblType);
     
    		JLabel lblNewLabel = new JLabel("Quantity:");
    		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
    		lblNewLabel.setBounds(155, 46, 61, 16);
    		meetPanel.add(lblNewLabel);
     
    		ground_M_WTextField = new JTextField();
    		ground_M_WTextField.setText("0.00");
    		ground_M_WTextField.setColumns(10);
    		ground_M_WTextField.setBounds(291, 73, 36, 16);
    		meetPanel.add(ground_M_WTextField);
     
    		chuck_M_WTextField = new JTextField();
    		chuck_M_WTextField.setText("0.00");
    		chuck_M_WTextField.setColumns(10);
    		chuck_M_WTextField.setBounds(291, 105, 36, 16);
    		meetPanel.add(chuck_M_WTextField);
     
    		rib_M_WTextField = new JTextField();
    		rib_M_WTextField.setText("0.00");
    		rib_M_WTextField.setColumns(10);
    		rib_M_WTextField.setBounds(291, 130, 36, 16);
    		meetPanel.add(rib_M_WTextField);
     
    		steak_M_WTextField = new JTextField();
    		steak_M_WTextField.setText("0.00");
    		steak_M_WTextField.setColumns(10);
    		steak_M_WTextField.setBounds(291, 157, 36, 16);
    		meetPanel.add(steak_M_WTextField);
     
    		flank_M_WTextField = new JTextField();
    		flank_M_WTextField.setText("0.00");
    		flank_M_WTextField.setColumns(10);
    		flank_M_WTextField.setBounds(291, 187, 36, 16);
    		meetPanel.add(flank_M_WTextField);
     
    		round_M_WTextField = new JTextField();
    		round_M_WTextField.setText("0.00");
    		round_M_WTextField.setColumns(10);
    		round_M_WTextField.setBounds(291, 217, 36, 16);
    		meetPanel.add(round_M_WTextField);
     
    		JLabel lblWeightlp = new JLabel("(LP) Weight: ");
    		lblWeightlp.setFont(new Font("Tahoma", Font.BOLD, 13));
    		lblWeightlp.setBounds(291, 46, 86, 16);
    		meetPanel.add(lblWeightlp);
     
    		at_M_QTextField = new JTextField();
    		at_M_QTextField.setText("0");
    		at_M_QTextField.setColumns(10);
    		at_M_QTextField.setBounds(155, 251, 18, 16);
    		meetPanel.add(at_M_QTextField);
     
    		at_M_WTextField = new JTextField();
    		at_M_WTextField.setText("0.00");
    		at_M_WTextField.setColumns(10);
    		at_M_WTextField.setBounds(291, 251, 36, 16);
    		meetPanel.add(at_M_WTextField);
     
    		at_meat_TextField = new JTextField();
    		at_meat_TextField.setText("Another Type..");
    		at_meat_TextField.setColumns(10);
    		at_meat_TextField.setBounds(8, 250, 98, 24);
    		meetPanel.add(at_meat_TextField);
     
    		lblPrice = new JLabel("(LP)Price:");
    		lblPrice.setFont(new Font("Tahoma", Font.BOLD, 13));
    		lblPrice.setBounds(446, 46, 73, 16);
    		meetPanel.add(lblPrice);
     
    		ground_M_PTextField = new JTextField();
    		ground_M_PTextField.setText("0.00");
    		ground_M_PTextField.setColumns(10);
    		ground_M_PTextField.setBounds(445, 71, 36, 16);
    		meetPanel.add(ground_M_PTextField);
     
    		rib_M_PTextField = new JTextField();
    		rib_M_PTextField.setText("0.00");
    		rib_M_PTextField.setColumns(10);
    		rib_M_PTextField.setBounds(445, 103, 36, 16);
    		meetPanel.add(rib_M_PTextField);
     
    		chuck_M_PTextField = new JTextField();
    		chuck_M_PTextField.setText("0.00");
    		chuck_M_PTextField.setColumns(10);
    		chuck_M_PTextField.setBounds(445, 128, 36, 16);
    		meetPanel.add(chuck_M_PTextField);
     
    		steak_M_PTextField = new JTextField();
    		steak_M_PTextField.setText("0.00");
    		steak_M_PTextField.setColumns(10);
    		steak_M_PTextField.setBounds(445, 155, 36, 16);
    		meetPanel.add(steak_M_PTextField);
     
    		flank_M_PTextField = new JTextField();
    		flank_M_PTextField.setText("0.00");
    		flank_M_PTextField.setColumns(10);
    		flank_M_PTextField.setBounds(445, 185, 36, 16);
    		meetPanel.add(flank_M_PTextField);
     
    		round_M_PTextField = new JTextField();
    		round_M_PTextField.setText("0.00");
    		round_M_PTextField.setColumns(10);
    		round_M_PTextField.setBounds(445, 215, 36, 16);
    		meetPanel.add(round_M_PTextField);
     
    		at_M_PTextField = new JTextField();
    		at_M_PTextField.setText("0.00");
    		at_M_PTextField.setColumns(10);
    		at_M_PTextField.setBounds(445, 249, 36, 16);
    		meetPanel.add(at_M_PTextField);
     
    		JPanel chikenPanel = new JPanel();
    		chikenPanel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, Color.BLUE, null, null));
    		mainPane.add(chikenPanel);
    		chikenPanel.setLayout(null);
     
    		JLabel lblChiken = new JLabel("*Chiken*");
    		lblChiken.setBounds(543, 104, 120, 77);
    		lblChiken.setFont(new Font("Times New Roman", Font.PLAIN, 25));
    		chikenPanel.add(lblChiken);
     
    		JCheckBox whole_C_CheckBox = new JCheckBox("Whole");
    		whole_C_CheckBox.setVerticalAlignment(SwingConstants.BOTTOM);
    		whole_C_CheckBox.setHorizontalAlignment(SwingConstants.LEFT);
    		whole_C_CheckBox.setBounds(12, 66, 113, 25);
    		chikenPanel.add(whole_C_CheckBox);
     
    		JCheckBox half_C_CheckBox = new JCheckBox("Half");
    		half_C_CheckBox.setBounds(12, 96, 113, 25);
    		chikenPanel.add(half_C_CheckBox);
     
    		JCheckBox breast_C_CheckBox = new JCheckBox("Breast");
    		breast_C_CheckBox.setBounds(12, 126, 113, 25);
    		chikenPanel.add(breast_C_CheckBox);
     
    		JCheckBox wings_C_CheckBox = new JCheckBox("Wings");
    		wings_C_CheckBox.setBounds(12, 156, 120, 25);
    		chikenPanel.add(wings_C_CheckBox);
     
    		JCheckBox liver_C_CheckBox = new JCheckBox("Liver");
    		liver_C_CheckBox.setBounds(12, 186, 113, 25);
    		chikenPanel.add(liver_C_CheckBox);
     
    		JCheckBox heart_C_CheckBox = new JCheckBox("Heart");
    		heart_C_CheckBox.setBounds(12, 216, 113, 25);
    		chikenPanel.add(heart_C_CheckBox);
     
    		textField_13 = new JTextField();
    		textField_13.setText("0");
    		textField_13.setColumns(10);
    		textField_13.setBounds(170, 71, 25, 16);
    		chikenPanel.add(textField_13);
     
    		textField_14 = new JTextField();
    		textField_14.setText("0");
    		textField_14.setColumns(10);
    		textField_14.setBounds(170, 97, 25, 16);
    		chikenPanel.add(textField_14);
     
    		textField_15 = new JTextField();
    		textField_15.setText("0");
    		textField_15.setColumns(10);
    		textField_15.setBounds(170, 130, 25, 16);
    		chikenPanel.add(textField_15);
     
    		textField_16 = new JTextField();
    		textField_16.setText("0");
    		textField_16.setColumns(10);
    		textField_16.setBounds(170, 160, 25, 16);
    		chikenPanel.add(textField_16);
     
    		textField_17 = new JTextField();
    		textField_17.setText("0");
    		textField_17.setColumns(10);
    		textField_17.setBounds(170, 190, 25, 16);
    		chikenPanel.add(textField_17);
     
    		textField_18 = new JTextField();
    		textField_18.setText("0");
    		textField_18.setColumns(10);
    		textField_18.setBounds(170, 220, 25, 16);
    		chikenPanel.add(textField_18);
     
    		textField_19 = new JTextField();
    		textField_19.setText("0.00");
    		textField_19.setColumns(10);
    		textField_19.setBounds(320, 220, 38, 16);
    		chikenPanel.add(textField_19);
     
    		textField_20 = new JTextField();
    		textField_20.setText("0.00");
    		textField_20.setColumns(10);
    		textField_20.setBounds(320, 190, 38, 16);
    		chikenPanel.add(textField_20);
     
    		textField_21 = new JTextField();
    		textField_21.setText("0.00");
    		textField_21.setColumns(10);
    		textField_21.setBounds(320, 157, 38, 16);
    		chikenPanel.add(textField_21);
     
    		textField_22 = new JTextField();
    		textField_22.setText("0.00");
    		textField_22.setColumns(10);
    		textField_22.setBounds(320, 130, 38, 16);
    		chikenPanel.add(textField_22);
     
    		textField_23 = new JTextField();
    		textField_23.setText("0.00");
    		textField_23.setColumns(10);
    		textField_23.setBounds(320, 100, 38, 16);
    		chikenPanel.add(textField_23);
     
    		textField_24 = new JTextField();
    		textField_24.setText("0.00");
    		textField_24.setColumns(10);
    		textField_24.setBounds(320, 71, 38, 16);
    		chikenPanel.add(textField_24);
     
    		JLabel label = new JLabel("Type:");
    		label.setFont(new Font("Tahoma", Font.BOLD, 13));
    		label.setBounds(12, 41, 56, 16);
    		chikenPanel.add(label);
     
    		JLabel label_1 = new JLabel("Quantity:");
    		label_1.setFont(new Font("Tahoma", Font.BOLD, 13));
    		label_1.setBounds(170, 41, 75, 16);
    		chikenPanel.add(label_1);
     
    		JLabel label_2 = new JLabel("(LP) Weight: ");
    		label_2.setFont(new Font("Tahoma", Font.BOLD, 13));
    		label_2.setBounds(320, 41, 92, 16);
    		chikenPanel.add(label_2);
     
    		textField_12 = new JTextField();
    		textField_12.setText("Another Type..");
    		textField_12.setColumns(10);
    		textField_12.setBounds(12, 250, 99, 24);
    		chikenPanel.add(textField_12);
     
    		textField_34 = new JTextField();
    		textField_34.setText("0");
    		textField_34.setColumns(10);
    		textField_34.setBounds(170, 251, 25, 16);
    		chikenPanel.add(textField_34);
     
    		textField_35 = new JTextField();
    		textField_35.setText("0.00");
    		textField_35.setColumns(10);
    		textField_35.setBounds(320, 251, 38, 16);
    		chikenPanel.add(textField_35);
     
    		label_5 = new JLabel("Price:");
    		label_5.setFont(new Font("Tahoma", Font.BOLD, 13));
    		label_5.setBounds(477, 41, 43, 16);
    		chikenPanel.add(label_5);
     
    		textField_7 = new JTextField();
    		textField_7.setText("0.00");
    		textField_7.setColumns(10);
    		textField_7.setBounds(477, 71, 36, 16);
    		chikenPanel.add(textField_7);
     
    		textField_8 = new JTextField();
    		textField_8.setText("0.00");
    		textField_8.setColumns(10);
    		textField_8.setBounds(477, 100, 36, 16);
    		chikenPanel.add(textField_8);
     
    		textField_9 = new JTextField();
    		textField_9.setText("0.00");
    		textField_9.setColumns(10);
    		textField_9.setBounds(476, 130, 36, 16);
    		chikenPanel.add(textField_9);
     
    		textField_10 = new JTextField();
    		textField_10.setText("0.00");
    		textField_10.setColumns(10);
    		textField_10.setBounds(476, 160, 36, 16);
    		chikenPanel.add(textField_10);
     
    		textField_11 = new JTextField();
    		textField_11.setText("0.00");
    		textField_11.setColumns(10);
    		textField_11.setBounds(477, 190, 36, 16);
    		chikenPanel.add(textField_11);
     
    		textField_25 = new JTextField();
    		textField_25.setText("0.00");
    		textField_25.setColumns(10);
    		textField_25.setBounds(477, 220, 36, 16);
    		chikenPanel.add(textField_25);
     
    		textField_32 = new JTextField();
    		textField_32.setText("0.00");
    		textField_32.setColumns(10);
    		textField_32.setBounds(477, 254, 36, 16);
    		chikenPanel.add(textField_32);
     
    		JPanel otherThingsPanel = new JPanel();
    		otherThingsPanel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
    		mainPane.add(otherThingsPanel);
    		otherThingsPanel.setLayout(null);
     
    		JLabel lblOtherThings = new JLabel("*Other Things*");
    		lblOtherThings.setBounds(421, 117, 176, 42);
    		lblOtherThings.setFont(new Font("Tahoma", Font.PLAIN, 25));
    		otherThingsPanel.add(lblOtherThings);
     
    		JLabel label_3 = new JLabel("Type:");
    		label_3.setFont(new Font("Tahoma", Font.BOLD, 13));
    		label_3.setBounds(16, 46, 65, 16);
    		otherThingsPanel.add(label_3);
     
    		JCheckBox chckbxCheese = new JCheckBox("Cheese");
    		chckbxCheese.setVerticalAlignment(SwingConstants.BOTTOM);
    		chckbxCheese.setHorizontalAlignment(SwingConstants.LEFT);
    		chckbxCheese.setBounds(12, 71, 113, 25);
    		otherThingsPanel.add(chckbxCheese);
     
    		JCheckBox chckbxBread = new JCheckBox("Bread");
    		chckbxBread.setBounds(12, 101, 113, 25);
    		otherThingsPanel.add(chckbxBread);
     
    		JCheckBox chckbxRice = new JCheckBox("Rice");
    		chckbxRice.setBounds(12, 131, 113, 25);
    		otherThingsPanel.add(chckbxRice);
     
    		JCheckBox chckbxBeefBurger = new JCheckBox("Burger ");
    		chckbxBeefBurger.setBounds(12, 161, 120, 25);
    		otherThingsPanel.add(chckbxBeefBurger);
     
    		JCheckBox chckbxChickenBurger = new JCheckBox("Kebab");
    		chckbxChickenBurger.setBounds(12, 191, 113, 25);
    		otherThingsPanel.add(chckbxChickenBurger);
     
    		JCheckBox chckbxFalafel = new JCheckBox("Falafel");
    		chckbxFalafel.setBounds(12, 221, 113, 25);
    		otherThingsPanel.add(chckbxFalafel);
     
    		textField_26 = new JTextField();
    		textField_26.setText("0");
    		textField_26.setColumns(10);
    		textField_26.setBounds(165, 225, 25, 16);
    		otherThingsPanel.add(textField_26);
     
    		textField_27 = new JTextField();
    		textField_27.setText("0");
    		textField_27.setColumns(10);
    		textField_27.setBounds(165, 195, 25, 16);
    		otherThingsPanel.add(textField_27);
     
    		textField_28 = new JTextField();
    		textField_28.setText("0");
    		textField_28.setColumns(10);
    		textField_28.setBounds(165, 162, 25, 16);
    		otherThingsPanel.add(textField_28);
     
    		textField_29 = new JTextField();
    		textField_29.setText("0");
    		textField_29.setColumns(10);
    		textField_29.setBounds(165, 132, 25, 16);
    		otherThingsPanel.add(textField_29);
     
    		textField_30 = new JTextField();
    		textField_30.setText("0");
    		textField_30.setColumns(10);
    		textField_30.setBounds(165, 102, 25, 16);
    		otherThingsPanel.add(textField_30);
     
    		textField_31 = new JTextField();
    		textField_31.setText("0");
    		textField_31.setColumns(10);
    		textField_31.setBounds(165, 76, 25, 16);
    		otherThingsPanel.add(textField_31);
     
    		JLabel label_4 = new JLabel("Quantity:");
    		label_4.setFont(new Font("Tahoma", Font.BOLD, 13));
    		label_4.setBounds(165, 46, 65, 16);
    		otherThingsPanel.add(label_4);
     
    		otherThings_AnotherTypeTextField = new JTextField();
    		otherThings_AnotherTypeTextField.setText("Another Type..");
    		otherThings_AnotherTypeTextField.setColumns(10);
    		otherThings_AnotherTypeTextField.setBounds(16, 250, 93, 24);
    		otherThingsPanel.add(otherThings_AnotherTypeTextField);
     
    		textField_36 = new JTextField();
    		textField_36.setText("0");
    		textField_36.setColumns(10);
    		textField_36.setBounds(165, 254, 25, 16);
    		otherThingsPanel.add(textField_36);
     
    		label_6 = new JLabel("Price:");
    		label_6.setFont(new Font("Tahoma", Font.BOLD, 13));
    		label_6.setBounds(310, 46, 56, 16);
    		otherThingsPanel.add(label_6);
     
    		textField_33 = new JTextField();
    		textField_33.setText("0.00");
    		textField_33.setColumns(10);
    		textField_33.setBounds(309, 71, 36, 16);
    		otherThingsPanel.add(textField_33);
     
    		textField_37 = new JTextField();
    		textField_37.setText("0.00");
    		textField_37.setColumns(10);
    		textField_37.setBounds(309, 103, 36, 16);
    		otherThingsPanel.add(textField_37);
     
    		textField_38 = new JTextField();
    		textField_38.setText("0.00");
    		textField_38.setColumns(10);
    		textField_38.setBounds(309, 128, 36, 16);
    		otherThingsPanel.add(textField_38);
     
    		textField_39 = new JTextField();
    		textField_39.setText("0.00");
    		textField_39.setColumns(10);
    		textField_39.setBounds(309, 155, 36, 16);
    		otherThingsPanel.add(textField_39);
     
    		textField_40 = new JTextField();
    		textField_40.setText("0.00");
    		textField_40.setColumns(10);
    		textField_40.setBounds(309, 185, 36, 16);
    		otherThingsPanel.add(textField_40);
     
    		textField_41 = new JTextField();
    		textField_41.setText("0.00");
    		textField_41.setColumns(10);
    		textField_41.setBounds(309, 215, 36, 16);
    		otherThingsPanel.add(textField_41);
     
    		textField_42 = new JTextField();
    		textField_42.setText("0.00");
    		textField_42.setColumns(10);
    		textField_42.setBounds(309, 249, 36, 16);
    		otherThingsPanel.add(textField_42);
     
    		JPanel calculationPanel = new JPanel();
    		calculationPanel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
    		mainPane.add(calculationPanel);
    		calculationPanel.setLayout(null);
     
    		JLabel lblCalculation = new JLabel("*Calculation*");
    		lblCalculation.setBounds(212, 12, 148, 29);
    		lblCalculation.setFont(new Font("Times New Roman", Font.PLAIN, 25));
    		calculationPanel.add(lblCalculation);
     
    		// Calculation everything
     
    		JButton calculateButton = new JButton("Calculate");
    		calculateButton.addActionListener(new CalcButtonListener());
     
    		calculateButton.setFont(new Font("Tahoma", Font.PLAIN, 25));
    		calculateButton.setBounds(340, 177, 313, 97);
    		calculationPanel.add(calculateButton);
     
    		txtrWriteYourComment = new JTextArea();
    		txtrWriteYourComment.setRows(1);
    		txtrWriteYourComment.setFont(new Font("Courier New", Font.PLAIN, 12));
    		txtrWriteYourComment.setText("Write Your Comment here.....");
    		txtrWriteYourComment.setBounds(12, 54, 641, 111);
    		calculationPanel.add(txtrWriteYourComment);
     
    		getProfitJButton = new JButton("Get Profit");
    		getProfitJButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
     
    				JFrame frame2 = new JFrame("Profit");
    			    frame2.setVisible(true);
    			    frame2.setSize(350,200);
    			    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		    
    			    profitPanel = new JPanel();
    			    profitPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    				setContentPane(profitPanel);
    				profitPanel.setLayout(new GridLayout(0, 2, 0, 0));
    			    frame2.getContentPane().add(profitPanel);			    
     
     
    			    profitFileButton = new JButton("Porfit File");
     
    			    profitFileTextField = new JTextField();
     
     
    			    getProfitJButton.addActionListener(new ActionListener() {
    					public void actionPerformed(ActionEvent e) {
     
    					    profitJFileChooser = new JFileChooser();				
    						profitJFileChooser.showOpenDialog(null);				
    						File f = profitJFileChooser.getSelectedFile();
    						String fileName = f.getAbsolutePath();
    						profitFileTextField.setText(fileName);		
     
    					}
    			    });
     
    			    profitPanel.add(profitFileButton);
    			    profitPanel.add(profitFileTextField);
     
    			    sellFileButtton = new JButton("Sell File");
    			    profitPanel.add(sellFileButtton);
     
     
    			}
    		});
    		getProfitJButton.setFont(new Font("Tahoma", Font.PLAIN, 25));
    		getProfitJButton.setBounds(12, 177, 316, 97);
    		calculationPanel.add(getProfitJButton);
     
    		///////////////////////////////////////////
     
    		theholder holder = new theholder();
    		groundMeatCheckBox.addItemListener(holder);
     
     
     
    	}
     
    	private class CalcButtonListener implements ActionListener{
     
    		// vairbles for the ground Meat check box
    		private double total_GM;
    		private double weightPrice_1;
    		private String stringQ;
    		private String stringW;
    		private String stringP;
    		private int meat_Q1;
    		private double meat_W1;
    		private double meat_P1;
     
    		public void actionPerformed(ActionEvent e){
     
    			total_GM = 0;
     
    			// The ground Meat check box calculation  
     
    				stringQ = ground_M_QTextField.getText();
    				meat_Q1 = Integer.parseInt(stringQ);
     
    				stringW = ground_M_WTextField.getText();
    				meat_W1 = Double.parseDouble(stringW);
     
    			 	stringP = ground_M_PTextField.getText();
    			 	meat_P1 = Double.parseDouble(stringP);
     
    			 	weightPrice_1 = meat_W1 * meat_P1;
    			 	total_GM += weightPrice_1  * meat_Q1;
     
    			// The ground Meat check box calculation /////////
     
    			 /*	
    			 	stringQ = ground_M_QTextField.getText();
    				meat_Q1 = Integer.parseInt(stringQ);
     
    				stringW = ground_M_WTextField.getText();
    				meat_W1 = Double.parseDouble(stringW);
     
    			 	stringP = ground_M_PTextField.getText();
    			 	meat_P1 = Double.parseDouble(stringP);
     
    			 	weightPrice_1 = meat_W1 * meat_P1;
    			 	total_GM += weightPrice_1  * meat_Q1;
     
    		    // The ground Meat check box calculation  ///////////////
     
    			 	stringQ = ground_M_QTextField.getText();
    				meat_Q1 = Integer.parseInt(stringQ);
     
    				stringW = ground_M_WTextField.getText();
    				meat_W1 = Double.parseDouble(stringW);
     
    			 	stringP = ground_M_PTextField.getText();
    			 	meat_P1 = Double.parseDouble(stringP);
     
    			 	weightPrice_1 = meat_W1 * meat_P1;
    			 	total_GM += weightPrice_1  * meat_Q1;
     
    		   // The ground Meat check box calculation  /////////////
     
    		   // The ground Meat check box calculation  /////////////////
     
    	       // The ground Meat check box calculation  ///////////////
     
    		   // The ground Meat check box calculation //////////////// 
     
    			 */	
     
    				 JOptionPane.showMessageDialog(null, total_GM + "\n" + s);
     
     
    		}
     
    	}
     
    	private class theholder implements ItemListener{
     
    		public void itemStateChanged (ItemEvent e){
     
     
    			if(groundMeatCheckBox.isSelected())
    			s = groundMeatCheckBox.getText();
     
    		}
    	}

    What I want to do exactly is to take the last code into another class or do something with it so I can use it in the Parent class, in other word any math calculation method or class I want them outside the Parent class.

    Note: I mean this code :

     
        private class CalcButtonListener implements ActionListener{
     
            		// vairbles for the ground Meat check box
            		private double total_GM;
            		private double weightPrice_1;
            		private String stringQ;
            		private String stringW;
            		private String stringP;
            		private int meat_Q1;
            		private double meat_W1;
            		private double meat_P1;
     
            		public void actionPerformed(ActionEvent e){
     
            			total_GM = 0;
     
            			// The ground Meat check box calculation  
     
            				stringQ = ground_M_QTextField.getText();
            				meat_Q1 = Integer.parseInt(stringQ);
     
            				stringW = ground_M_WTextField.getText();
            				meat_W1 = Double.parseDouble(stringW);
     
            			 	stringP = ground_M_PTextField.getText();
            			 	meat_P1 = Double.parseDouble(stringP);
     
            			 	weightPrice_1 = meat_W1 * meat_P1;
            			 	total_GM += weightPrice_1  * meat_Q1;
     
     
            				 JOptionPane.showMessageDialog(null, total_GM + "\n" + s);
     
     
            		}
     
            	}


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I would move the ActionListener into its own file and then make it take your main Class as an argument to it's constructor. This also means that you need to make getter methods for all the fields you need to access, but you get your calculation code moved out of the GUI code.

    Also for a GUI project of this size I would probably use a GUI builder like Netbeans, just to make layouting easier.

    Rolf

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to split a jframe as parent class with the class of actionListener, itemListener, etc...

    Welcome to the Forum! Thanks for taking the time to learn how to post your code correctly.

    You should understand inheritance, breaking code into separate classes, and several other basic OOP concepts before coding a Java GUI. If you'd like material to improve your Java knowledge foundation, just ask.

    I disagree with Rolf on the use of the Netbeans GUIBuilder for building your interface, but that's possibly a personal preference we simply disagree on and don't need to argue. I am certain you'll understand the code better, it will be more maintainable and more easily modifiable if you code it by hand, without the aid of the GUIBuilder.

    Whichever you decide, good luck!

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to Make a GUI program in separates files.

    Hello everyone.
    I want to make my GUI code in separates files. I'm reaching **1000 Line** in my program and this is stressful because all my code are in one file

    Will to make long story short I'm working in a big program, so I will put an example to show you what I need.

    First: This is an example code:

     
    public class world extends JFrame {
     
    	private JPanel contentPane;
    	private JTextField textField;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					world frame = new world();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public world() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
     
    		textField = new JTextField();
    		textField.setBounds(0, 0, 427, 89);
    		contentPane.add(textField);
    		textField.setColumns(10);
     
    		JButton btnNewButton = new JButton("New button");
    		btnNewButton.setBounds(5, 137, 422, 111);
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
     
    				 String s = textField.getText();
    			     JOptionPane.showMessageDialog(null, s);
    			}
    		});
    		contentPane.add(btnNewButton);
    	}
     
    }

    Second: As you can see all the methods and everything in one file, and that not go for me.

    Third: I want this code out of the parents class (main class), I don't know how, is there a way to be inherited , or make a new class, or an interface class I need a way to my function or math outside the main method.

     
     
    btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
     
    				 String s = textField.getText();
    			     JOptionPane.showMessageDialog(null, s);
    			}
    		});

    please give a written code if you are answering my question because I'm frustrated. I ask and ask, but I get completely different answer to what I need.

    Thank you.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to split a jframe as parent class with the class of actionListener, itemListener, etc...

    Please don't post multiple threads on the same topic. Keep working it in a single thread. If you're not getting the answer you want or enough of a response, add more info, your latest code, etc.

    By convention, Java class names begin with capital letters. This is not only important stylistically but in making your code more readable and easily understood.

    As for your questions, that doesn't look like 1,000 lines, but I suppose the general question of how to split classes apart is a good one. Here's how you might split the main() method into its own class:
    // a class with a main() method to start the
    // class with a JFrame
    public class MainClass
    {
        // move your main() method here
        public static void main( String[] args )
        {
     
        } // end method main()
     
    } // end class MainClass
     
    // then, in a separate file . . .
     
    public class World extends JFrame
    {
     
    } // end class World
    Then to break out the ActionListener from an anonymous inner class:
    // splitting out the action listener, also in a separate file . . .
    public class MyButtonListener implements ActionListener
    {
        // move the actionPerformed() method here
     
    } // end class MyButtonListener
    Go forth and separate . . .

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to split a jframe as parent class with the class of actionListener, itemListener, etc...

    Quote Originally Posted by GregBrannon View Post
    Please don't post multiple threads on the same topic. Keep working it in a single thread. If you're not getting the answer you want or enough of a response, add more info, your latest code, etc.

    By convention, Java class names begin with capital letters. This is not only important stylistically but in making your code more readable and easily understood.
    sorry for putting another one threads, and I will take your advise about the class name.


    Quote Originally Posted by
    As for your questions, that doesn't look like 1,000 lines, but I suppose the general question of how to split classes apart is a good one. Here's how you might split the main() method into its own class:
    [highlight=java
    // a class with a main() method to start the
    // class with a JFrame
    public class MainClass
    {
    // move your main() method here
    public static void main( String[] args )
    {

    } // end method main()

    } // end class MainClass

    // then, in a separate file . . .

    public class World extends JFrame
    {

    } // end class World[/highlight]
    will I did what you said. as you can see in this part of the code of the main method:
    public class test_1 extends JFrame {
     
    	private  JButton hereButton;
    	private JPanel contentPane;
    	private static  JTextField name;
     
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
     
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					test_1 frame = new test_1();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
     
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public test_1() {
     
    		final showName nn = new showName();
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
     
    		name = new JTextField("");
    		name.setBounds(5, 5, 422, 67);
    		name.setFont(new Font("Tw Cen MT", Font.PLAIN, 23));
    		name.setHorizontalAlignment(SwingConstants.CENTER);
    		contentPane.add(name);
    		name.setColumns(10);
     
     
    		hereButton = new JButton("Your Name is");		
    		hereButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
     
    			  nn.actionPerformed(e); // I also tried to put (null) instead of (e) 
                                                                // however nothing happened 			 
     
    			}
    		});
    		hereButton.setBounds(5, 158, 422, 90);
    		contentPane.setLayout(null);
    		contentPane.add(hereButton);
     
    	}
     
    }

    and this my other class that has the actionListener method:
     
    public class showName implements ActionListener {
     
    	protected JTextField tf = new JTextField("");
    	protected String s;
     
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
     
    		 s = tf.getText();
    		JOptionPane.showMessageDialog(null,s);
     
    	}


    Quote Originally Posted by
    Then to break out the ActionListener from an anonymous inner class:
    [highlight=java
    // splitting out the action listener, also in a separate file . . .
    public class MyButtonListener implements ActionListener
    {
    // move the actionPerformed() method here

    } // end class MyButtonListener[/highlight]
    Go forth and separate . . .
    will everything working good however, there is on small problem one I press the Button its give me the
    JOptionPane without any words or anythings even when I write in the JTextField, So could you tell me please what I'm I missing here in my code.

    Thank you and so for the too many questions.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to split a jframe as parent class with the class of actionListener, itemListener, etc...

    And still your class names begin with lower-case letters. I thought you were going to correct that.

    Here is your code, minimally modified with comments to describe changes.
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import javax.swing.border.EmptyBorder;
     
    public class TestClass extends JFrame
    {
        private JButton hereButton;
        private JPanel contentPane;
        private JTextField name;
     
     
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
     
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        TestClass frame = new TestClass();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
     
        }
     
        /**
         * Create the frame.
         */
        public TestClass()
        {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
     
            name = new JTextField("");
            name.setBounds(5, 5, 422, 67);
            name.setFont(new Font("Tw Cen MT", Font.PLAIN, 23));
            name.setHorizontalAlignment(SwingConstants.CENTER);
            contentPane.add(name);
            name.setColumns(10);
     
     
            hereButton = new JButton("Your Name is");
     
            // the anonymous inner class contruction was removed
            // in favor of using the separate class
            hereButton.addActionListener( new ShowName( name ) );
     
            hereButton.setBounds(5, 158, 422, 90);
            contentPane.setLayout(null);
            contentPane.add(hereButton);
     
        }
     
    }
     
    // this class is currently constructed to be in a separate file, but it
    // could be in the same file if 'public' was removed, or it could be an
    // inner class
    public class ShowName implements ActionListener
    {
        protected String s;
        private JTextField name;
     
        // this constructor accepts a JTextField object from
        // the calling class and assigns it to a local instance
        public ShowName( JTextField name )
        {
            this.name = name;
        }
     
     
        @Override
        public void actionPerformed(ActionEvent e)
        {
            JOptionPane.showMessageDialog( null, name.getText() );
        }
    }

  8. #8
    Junior Member
    Join Date
    Jan 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to split a jframe as parent class with the class of actionListener, itemListener, etc...

    WoW, I really don't know what to say. I all most said to myself I wont get it. However, you give me the right hope of how to do it, and other thing I did not mean not use a capital letters for classes names I just when I'm focused in something about programming everything have to wait.

    Real thank you so much.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: how to split a jframe as parent class with the class of actionListener, itemListener, etc...

    You're welcome.

Similar Threads

  1. Replies: 149
    Last Post: February 19th, 2013, 10:04 PM
  2. Replies: 0
    Last Post: February 15th, 2013, 05:24 PM
  3. Replies: 1
    Last Post: February 14th, 2013, 07:30 PM
  4. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM
  5. Access and set variable in parent class through child
    By java_newbie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 19th, 2011, 11:44 PM

Tags for this Thread