8 Replies - 349 Views - Last Post: 04 October 2011 - 03:54 PM Rate Topic: -----

#1 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

making a program import from different class/java file :P

Posted 04 October 2011 - 03:15 PM

yea its kinda hard to explain but i made this program and it works great then i wanted to make it a GUI and i made a new file which had all the gui coding in it and now i need to import a variable from the program to the gui file :S

to make the program import the gui i used this statement
JFrame a = new GUI();
a.show();



but i have no idea how to make the gui file import a variable from the thread

plzz help thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: making a program import from different class/java file :P

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: making a program import from different class/java file :P

Posted 04 October 2011 - 03:20 PM

One way to pass variables to another object is to include them as parameters in the constructor when instantiating the object:
GUI a = new GUI( variable );

I'm not sure that will work in your case. If you show us more code, we'll be able to give a better answer.
Was This Post Helpful? 0
  • +
  • -

#3 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: making a program import from different class/java file :P

Posted 04 October 2011 - 03:23 PM

View PostGregBrannon, on 04 October 2011 - 03:20 PM, said:

One way to pass variables to another object is to include them as parameters in the constructor when instantiating the object:
GUI a = new GUI( variable );

I'm not sure that will work in your case. If you show us more code, we'll be able to give a better answer.


hmm i did not really get that but im basicly making my "main" program get its GUI from another file called gui.java using the statement above and now the gui file needs a variable from the main program its not much more code to show :S
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: making a program import from different class/java file :P

Posted 04 October 2011 - 03:26 PM

Edit: You only have two lines of code?

Show us both the GUI class and the class containing your main() method. Then explain what variable the GUI instance has to have from main(). I'm thinking my first answer will do what you need, but I can't say for sure.

This post has been edited by GregBrannon: 04 October 2011 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: making a program import from different class/java file :P

Posted 04 October 2011 - 03:31 PM

View PostGregBrannon, on 04 October 2011 - 03:26 PM, said:

Edit: You only have two lines of code?

Show us both the GUI class and the class containing your main() method. Then explain what variable the GUI instance has to have from main(). I'm thinking my first answer will do what you need, but I can't say for sure.


ok but remember im a noob and have just started teaching gui coding so the only thing i want is that the button is going to display a scanner's input into a jlabel :S its pretty much a test :P

main program:

package lerox;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;

public class Lerox extends JFrame {
    
    public static void main(String[] args) throws IOException {
        boolean exit = false;
        JFrame a = new GUI();
        a.show();
        while (!exit){
           loop();
        }
   }
    public static void loop() throws IOException {
        
        System.out.println("type a command or type \"help\" for command list");
        boolean exit = false;
        String n = System.getProperty("Line.seperator");
        
        Scanner w2 = new Scanner(System.in);
        String input = w2.nextLine();
   
        if (input.equals("add movie")){
               while (exit == false){
        
               //writing movies to the list
               System.out.println(" ");
               System.out.println("what movie do you want to add to the list of watched movies ?");
               Scanner w1 = new Scanner(System.in);
               String m1 = w1.nextLine();
               BufferedWriter write = new BufferedWriter(new FileWriter("C:/Users/vegard/workspace/lerox/src/movie lib.txt", (true)));
               write.write(m1);
               write.newLine();
               write.flush();
               write.close();
               if (m1.equals ("exit")){
                   exit = true;
               }
               else {
                   System.out.print("movie " + m1);
                   System.out.println(" added to watchlist.");
                   System.out.println(" ");
               }
            }
        }
           if (input.equals("search")){
               BufferedReader read = new BufferedReader(new FileReader("C:/Users/vegard/workspace/lerox/src/movie lib.txt"));
               System.out.println("movie to search for ?");
               Scanner s = new Scanner(System.in);
               String s1 = s.nextLine();
               String sr = read.readLine();
               if (s1.equals(sr)){
                   System.out.println("u have already watched " + s1);
               }
               else{
                   System.out.println("you have not watched this movie!");
               }
           }
           
           if (input.equals("help")){
               System.out.println(" ");
               System.out.println("1.  search");
               System.out.println("2.  add movie");
               System.out.println("3.  help");
               System.out.println(" ");
           }
    }
}





and here's the gui:
package lerox;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class GUI extends JFrame {
    private JTextField textfield;
    private JButton button;
    private JLabel label;
        
    public static void main(String[] args) {
    }
    
    public GUI() {
        setSize(500, 250);
        setResizable(false);
        setTitle("movie library application.");
        setLocation(500, 220);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        
        textfield = new JTextField("this is default text in the textfield");
        add(textfield);
        
        label = new JLabel("this is a JLabel!");
        add(label);
        
        button = new JButton("SEND");
        add(button);
        
        event e = new event();
        button.addActionListener(e);
    }
    
    public class event implements ActionListener {
        
        public void actionPerformed(ActionEvent e) {
            label.setText(w2);
        }
        
    }
    
}




i want the label to display "w2" from the main program :/
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: making a program import from different class/java file :P

Posted 04 October 2011 - 03:43 PM

Add a setLabel() method to the GUI Class that addresses the label you want to change. Then you can do a.setLabel("new text"):

This post has been edited by n8wxs: 04 October 2011 - 03:48 PM

Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: making a program import from different class/java file :P

Posted 04 October 2011 - 03:48 PM

Wait a minute, say "display a scanner's input into a jlabel"? Why not taking input using JTextArea now since we have go into gui?
In gui, the main method should initialize gui and let all things done into gui, no need to mix command line with gui. This is why we separate our engine program and the main class, so when need to move from one mode to another we dont change anything in our program class
Was This Post Helpful? 1
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: making a program import from different class/java file :P

Posted 04 October 2011 - 03:49 PM

Don't be afraid that people might help you with parts of your code you didn't want help with. Most here are helpful and not critical.

You say you want to display w2, but w2 is a Scanner object. You can't do exactly as you're thinking - w2 can't be used as a component's label.

However, you CAN use a String that has been received by w2. Using your variable "input" as an example, you could add a method to your GUI class, public void setLabel( String label ), that you could use to change the label. After getting "input" from the user, call a.setLabel( input ) to change the label in a to "input".

Don't use show() anymore. Use setVisible().

This post has been edited by GregBrannon: 04 October 2011 - 03:50 PM

Was This Post Helpful? 0
  • +
  • -

#9 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: making a program import from different class/java file :P

Posted 04 October 2011 - 03:54 PM

thank u guys so much u see i came from another forum "HF" if anyone knows about it and over there people would always say taht ur code sucks etc :P but in fact ppl on this forum is alot nicer and yea i see what u mean by using the JTextArea :P i did not think of using it as a input before now, and thanks to all i think that i will be able to do it myself for now :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1