1 Replies - 157 Views - Last Post: 30 June 2012 - 09:58 AM Rate Topic: -----

#1 Lejzabo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-June 12

frames communicating or what

Posted 30 June 2012 - 08:22 AM

ok so i got 2 classes and i want them to communicate..
the first one is osnova.java and the other is toplo.java. There will be 4 or 5 more frames. so here we go.:)

Osnova.java this one works fine i'm just pasting it for you to see how oh and i'm not very good at this so there might be
some things you'll go:"why would he do that?!" but its ok.:D
package veliko;

import java.awt.EventQueue;

public class osnova {

	public String tabela[][] = new String[21][4];
	static JFrame frame;
	private JTextField textField_1;
	public  JTextArea textArea = new JTextArea();
	public  JTextField textField = new JTextField();
	public  Integer skupek=0;
	public String notranji="";
	public  float fl=0;
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					osnova window = new osnova();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public osnova() {
		initialize();
		napolni();
	
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
	
		
		frame = new JFrame();
		frame.getContentPane().setBackground(new Color(102, 255, 204));
		frame.setBounds(100, 100, 450, 669);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		textArea.setFont(new Font("Trebuchet MS", Font.PLAIN, 13));
		textArea.setBounds(71, 165, 218, 362);
		frame.getContentPane().add(textArea);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(31, 119, 15, 14);
		frame.getContentPane().add(lblId);
		
		
		textField.setBounds(71, 116, 218, 20);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("Dodaj");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dodej();
			}
		});
		btnNewButton.setBounds(322, 115, 89, 23);
		frame.getContentPane().add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			//frame.setVisible(false);
			Toplo Toplo_g=new Toplo();
			Toplo_g.frame.setVisible(true);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon("C:\\Users\\Tomo\\Desktop\\png\\48x48\\Coffee.png"));
		btnNewButton_1.setBounds(322, 165, 89, 66);
		frame.getContentPane().add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("New button");
		btnNewButton_2.setBounds(322, 265, 89, 66);
		frame.getContentPane().add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("New button");
		btnNewButton_3.setBounds(322, 364, 89, 66);
		frame.getContentPane().add(btnNewButton_3);
		
		JButton btnNewButton_4 = new JButton("New button");
		btnNewButton_4.setBounds(322, 463, 89, 66);
		frame.getContentPane().add(btnNewButton_4);
		
		JLabel lblSkupaj = new JLabel("SKUPAJ:");
		lblSkupaj.setBounds(71, 541, 89, 14);
		frame.getContentPane().add(lblSkupaj);
		
		textField_1 = new JTextField();
		textField_1.setBounds(203, 540, 86, 20);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnNewButton_5 = new JButton("New button");
		btnNewButton_5.setBounds(71, 580, 89, 23);
		frame.getContentPane().add(btnNewButton_5);
		
		JButton btnNewButton_6 = new JButton("New button");
		btnNewButton_6.setBounds(200, 580, 89, 23);
		frame.getContentPane().add(btnNewButton_6);
		
	}
	public void dodej(){
		String text = textField.getText();
		String notranji=textArea.getText();
		Boolean napaka=true;
		for (int i=0;i<21;i++){
			if (text.equals(tabela[i][1])){	
				for (int j=2;j<3;j++){						
					textArea.setText(notranji+tabela[i][j]+"\t"+"\t"+tabela[i][j+1]);						
					notranji=textArea.getText();
					napaka=false;
					Skupek(i);												
				}
			}																
		}
		if(napaka==true){
			JOptionPane.showMessageDialog(frame, "POZOR! IDji gredo od 00001 do 00020!");				
		}
		else{
			
			textArea.setText(notranji+"\n");
		}
	}

	public void Skupek(int i){
		String skupek_str;
		Float skupek_fl;
		fl=Float.parseFloat(tabela[i][3])*100;
		skupek=(int)fl+skupek;
		skupek_fl=(float)skupek/100;
		skupek_str=Float.toString(skupek_fl);
		textField_1.setText(skupek_str+"");		
	}
	
	public void napolni(){
		for (int x=0;x<21;x++){
			if (x>9){
				tabela[x][1]="000"+x;
				}
			else{ tabela[x][1]="0000"+x;
				}
		}
		tabela[1][2]="Kava kratka";
		tabela[2][2]="Kava dolga";
		tabela[3][2]="Kapucino";
		tabela[4][2]="Machiatto";
		tabela[5][2]="Caj";
		
		tabela[6][2]="Coca-Cola 0.3l";
		tabela[7][2]="Fanta 0.3l";
		tabela[8][2]="Sprite 0.3l";
		tabela[9][2]="Tonic 0.3l";
		tabela[10][2]="Juice 0.2";
		
		tabela[11][2]="Lasko 0.5l";
		tabela[12][2]="Union 0.5l";
		tabela[13][2]="CC 0.03";
		tabela[14][2]="Jack 0.03";
		tabela[15][2]="Stock 0.03";
		
		tabela[16][2]="Cig. Camel";
		tabela[17][2]="Cig. Davidoff";
		tabela[18][2]="Panin Salama";
		tabela[19][2]="Panin Slanina";
		tabela[20][2]="Zvecilni Orbit";
		
		tabela[1][3]="0.80";
		tabela[2][3]="0.80";
		tabela[3][3]="1";
		tabela[4][3]="1";
		tabela[5][3]="1";
		
		tabela[6][3]="2.20";
		tabela[7][3]="2.20";
		tabela[8][3]="2.20";
		tabela[9][3]="2";
		tabela[10][3]="1";
		
		tabela[11][3]="2.10";
		tabela[12][3]="2.10";
		tabela[13][3]="2.20";
		tabela[14][3]="2.60";
		tabela[15][3]="1.80";
		
		tabela[16][3]="3.10";
		tabela[17][3]="3.30";
		tabela[18][3]="2.20";
		tabela[19][3]="2.50";
		tabela[20][3]="0.80";
	}
}



here in osnova.java i open a frame and add some items to a list so i can sum them later. As i said works fine.

Now for toplo.java

public class Toplo extends osnova{
	public JFrame frame;
	public Integer i=0;
	
	
	
	public Toplo() {
		initialize();
	}

	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JButton btnNewButton = new JButton("Kratka");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			osnova.textField.setText("00001");
			osnova.dodej();			
			}
		});
                btnNewButton.setBounds(124, 32, 121, 41);
		frame.getContentPane().add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Capucino");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			osnova.textField.setText("00003");
			osnova.dodej();			
			}
		});



here is the new frame i created...but when i want to run it eclipse says that i need some static variables but when i set them to static and try to add 1 item it adds all items...and if i make a new object of osnova it deletes my previous entries from osnova and adds only the price...so my question is how can i fix this so that when i click on "Kratka" it adds only that item and it does not delete the others. Tnx in advance! and bye:)

Is This A Good Question/Topic? 0
  • +

Replies To: frames communicating or what

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: frames communicating or what

Posted 30 June 2012 - 09:58 AM

Respect Java conventions, a class name should start by a capital letter
So osnova should ve Osnova and Toplo_g should be toplo_g.

first why Toplo extends osnova ? Does a Toplo should contain all the fields held by a osnova instance ? I seriously doubt.

You shouldn't have static variable at all, especially JFrame. You want all your Osnova et Toplo to share the same JFrame ???

Your first class should extend it, that will simplify your life a lot

 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.EventQueue;

public class osnova extends JFrame {

	public String tabela[][] = new String[21][4];
	private JTextField textField_1;
	public  JTextArea textArea = new JTextArea();
	public  JTextField textField = new JTextField();
	public  Integer skupek=0;
	public String notranji="";
	public  float fl=0;
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					osnova window = new osnova();
					window.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public osnova() {
		initialize();
		napolni();
	
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
	
		getContentPane().setBackground(new Color(102, 255, 204));
		setBounds(100, 100, 450, 669);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
		
		textArea.setFont(new Font("Trebuchet MS", Font.PLAIN, 13));
		textArea.setBounds(71, 165, 218, 362);
		add(textArea);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(31, 119, 15, 14);
		add(lblId);
		
		
		textField.setBounds(71, 116, 218, 20);
		add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("Dodaj");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dodej();
			}
		});
		btnNewButton.setBounds(322, 115, 89, 23);
		add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			//frame.setVisible(false);
			Toplo Toplo_g=new Toplo();
			Toplo_g.setVisible(true);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon("C:\\Users\\Tomo\\Desktop\\png\\48x48\\Coffee.png"));
		btnNewButton_1.setBounds(322, 165, 89, 66);
		add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("New button");
		btnNewButton_2.setBounds(322, 265, 89, 66);
		add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("New button");
		btnNewButton_3.setBounds(322, 364, 89, 66);
		add(btnNewButton_3);
		
		JButton btnNewButton_4 = new JButton("New button");
		btnNewButton_4.setBounds(322, 463, 89, 66);
		add(btnNewButton_4);
		
		JLabel lblSkupaj = new JLabel("SKUPAJ:");
		lblSkupaj.setBounds(71, 541, 89, 14);
		add(lblSkupaj);
		
		textField_1 = new JTextField();
		textField_1.setBounds(203, 540, 86, 20);
		add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnNewButton_5 = new JButton("New button");
		btnNewButton_5.setBounds(71, 580, 89, 23);
		add(btnNewButton_5);
		
		JButton btnNewButton_6 = new JButton("New button");
		btnNewButton_6.setBounds(200, 580, 89, 23);
		add(btnNewButton_6);
		
	}
	public void dodej(){
		String text = textField.getText();
		String notranji=textArea.getText();
		Boolean napaka=true;
		for (int i=0;i<21;i++){
			if (text.equals(tabela[i][1])){	
				for (int j=2;j<3;j++){						
					textArea.setText(notranji+tabela[i][j]+"\t"+"\t"+tabela[i][j+1]);						
					notranji=textArea.getText();
					napaka=false;
					Skupek(i);												
				}
			}																
		}
		if(napaka==true){
			JOptionPane.showMessageDialog(this, "POZOR! IDji gredo od 00001 do 00020!");				
		}
		else{
			
			textArea.setText(notranji+"\n");
		}
	}

	public void Skupek(int i){
		String skupek_str;
		Float skupek_fl;
		fl=Float.parseFloat(tabela[i][3])*100;
		skupek=(int)fl+skupek;
		skupek_fl=(float)skupek/100;
		skupek_str=Float.toString(skupek_fl);
		textField_1.setText(skupek_str+"");		
	}
	
	public void napolni(){
		for (int x=0;x<21;x++){
			if (x>9){
				tabela[x][1]="000"+x;
				}
			else{ tabela[x][1]="0000"+x;
				}
		}
		tabela[1][2]="Kava kratka";
		tabela[2][2]="Kava dolga";
		tabela[3][2]="Kapucino";
		tabela[4][2]="Machiatto";
		tabela[5][2]="Caj";
		
		tabela[6][2]="Coca-Cola 0.3l";
		tabela[7][2]="Fanta 0.3l";
		tabela[8][2]="Sprite 0.3l";
		tabela[9][2]="Tonic 0.3l";
		tabela[10][2]="Juice 0.2";
		
		tabela[11][2]="Lasko 0.5l";
		tabela[12][2]="Union 0.5l";
		tabela[13][2]="CC 0.03";
		tabela[14][2]="Jack 0.03";
		tabela[15][2]="Stock 0.03";
		
		tabela[16][2]="Cig. Camel";
		tabela[17][2]="Cig. Davidoff";
		tabela[18][2]="Panin Salama";
		tabela[19][2]="Panin Slanina";
		tabela[20][2]="Zvecilni Orbit";
		
		tabela[1][3]="0.80";
		tabela[2][3]="0.80";
		tabela[3][3]="1";
		tabela[4][3]="1";
		tabela[5][3]="1";
		
		tabela[6][3]="2.20";
		tabela[7][3]="2.20";
		tabela[8][3]="2.20";
		tabela[9][3]="2";
		tabela[10][3]="1";
		
		tabela[11][3]="2.10";
		tabela[12][3]="2.10";
		tabela[13][3]="2.20";
		tabela[14][3]="2.60";
		tabela[15][3]="1.80";
		
		tabela[16][3]="3.10";
		tabela[17][3]="3.30";
		tabela[18][3]="2.20";
		tabela[19][3]="2.50";
		tabela[20][3]="0.80";
	}
}



Get ride of the JFrame in your Toplo too.
Now the scenario change if Toplo extends or should extends Osnova or not.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1