aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Calling and Returning values from GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Calling and Returning values from GUI" Watch "Calling and Returning values from GUI" New topic
Author

Calling and Returning values from GUI

Keith Spriggs
Greenhorn

Joined: Jul 19, 2012
Posts: 18
Hi Guys,
I surrender, I’m raising the white flag and getting down on my knees and begging for help. The problem that has driven me to this is as follows.

I need to write a method that calls a sub method which returns a char value.
The sub menu needs to be GUI interface.
But it cannot be a dialog box.
The main method needs to then use this information for calling the required methods.

Can anyone help me with this one? Simple code example would help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18155
    
    8

Here's a simple pair of text fragments which might work for you:





You'd have to put them in the right place to make them work.

That's just a guess, though. It's hard for me to tell what you're really after, since you didn't provide many details and the ones you did provide are rather non-standard. (For example I'm guessing that a "sub" method is just an ordinary method, only it happens to be called from code in some other method...) You might find that if you provided a more detailed description of your problem then you might get a better answer, I have a feeling that this answer isn't going to be sufficient.
Keith Spriggs
Greenhorn

Joined: Jul 19, 2012
Posts: 18
I'll explain a bit more. The main method calls the sub method which asks the user to make a choice between either BMW or Lexus.
This is displayed as a GUI Menu.
The GUI would then return either a or b to the main which then will go into a conditional statement for further action.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18155
    
    8

When you say the method "asks" the user to do something, exactly what does that mean?

I ask that because usually GUI code doesn't ask the user questions. Or if it does, it pops up a dialog box to ask that question. Usually GUI code just looks at the GUI components to see what the user has already put there. But you've rejected the dialog box idea. So now we need to know what you have in mind instead.
Keith Spriggs
Greenhorn

Joined: Jul 19, 2012
Posts: 18
The GUI method is called.
This brings up a menu of buttons.
The User selects the button and a char is returned to the method that calls it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18155
    
    8

Keith Spriggs wrote:This brings up a menu of buttons.


Okay. Why don't you show us the code that does that bringing-up, then, and we can start from there?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
I hope you don’t feel I am shamelessly butting in, Paul. I would hate to do that …


 … but shall do it anyway.

You do not want a GUI to choose your cars, or handle any choice, or anything. All the GUI should do is display things. If you want to get your app to work, it needs to work from the command line. Write an app which chooses cars from the command line, so you can write BMW or Lexus or whatever and have the choice handled.

Then you add a GUI on top, with buttons which instruct the app to handle whichever choice you have made.
Keith Spriggs
Greenhorn

Joined: Jul 19, 2012
Posts: 18
The code for the sub class is as follows


package John;

import java.awt.event.*;

import javax.swing.*;

public class Tiny extends JFrame implements ActionListener
{
private JLabel label1;
private JFrame frame1;
private JButton button1;
private JButton button2;
private JButton button3;
private char select;

public Tiny()
{
createFrame();
createLabel();
createButton1();
createButton2();
createButton3();
//actionPerformed(e1);
}

private void createFrame()
{
frame1 = new JFrame("Select Cars");
frame1.setSize(300, 400);
frame1.setLocation(350, 250);
frame1.setPreferredSize(null);
frame1.setLayout(new java.awt.GridLayout(4, 1));
frame1.setVisible(true);
frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private void createLabel()
{
label1 = new JLabel("Select a car", SwingConstants.CENTER);
label1.setSize(300, 100);
frame1.add(label1);
}

private void createButton1()
{
button1 = new JButton("Bmw");
button1.setSize(300, 100);
frame1.add(button1);
}

private void createButton2()
{
button2 = new JButton("Jaguar");
button2.setSize(300, 100);
frame1.add(button2);
}

private void createButton3()
{
button3 = new JButton("Lexus");
button3.setSize(300, 100);
frame1.add(button3);
}

@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
select = 'a';
else
if(e.getSource() == button2)
select = 'b';
else
if(e.getSource() == button3)
select = 'c';
else
select = 'd';
}

public char getSelect()
{
return select;
}
}



The code for the calling class is as follows

package John;

public class Creamy {


public static void main(String[] args)
{
Tiny bear = new Tiny();

char choice = bear.getSelect();

switch(choice)
{
case 'a' : System.out.println("German Car");
break;
case 'b' : System.out.println("British Car");
break;
case 'c' : System.out.println("Japanese Car");
break;
default : System.out.println("No car Selected");
}
}

}

Keith Spriggs
Greenhorn

Joined: Jul 19, 2012
Posts: 18
This is the same code that is written for the command line.


package John;

public class Creamy {


public static void main(String[] args)
{
Gormy blue = new Gormy();
char choice = blue.getSelect();

switch(choice)
{
case 'a' : System.out.println("German Car");
break;
case 'b' : System.out.println("British Car");
break;
case 'c' : System.out.println("Japanese Car");
break;
default : System.out.println("No car Selected");
}
}
}


And the subMenu looks like

package John;

import java.util.Scanner;

public class Gormy
{
private Scanner sc;

public Gormy()
{

}

private void printDetail()
{
System.out.println("Please choose a car");
System.out.println("choose a : BMW");
System.out.println("choose b : Jaguar");
System.out.println("choose c : Lexus");
}

private char getCharacter()
{
sc = new Scanner(System.in);

return sc.next().charAt(0);
}

public char getSelect()
{
printDetail();
return getCharacter();
}
}


this works perfectly and it what I want the GUI to mimic.
the reason why I don't want to use dailog boxes which work fine is that I plan to implement more features at a later stage.
Keith Spriggs
Greenhorn

Joined: Jul 19, 2012
Posts: 18
Each button has the line which I missed out on


button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Please use the code button, not coloured text, because otherwise your code is difficult to read.
I suggest you find whichever book you have which recommends addActionListener(this) and burn it. If you have buttons with the names of the cars in already, you can use those names. Don’t mess around with getSource() and ==, which is non‑object‑oriented style, and potentially error‑prone.
You know you can write this sort of thing?But what are you going to do when you have a "VW" button and a "Mercedes" button?

I think the object‑oriented thing to do is to create a CarMake class which can encapsulate the make and location. You can have a CarMake[] and you can display the name of each make, possibly with an associated number. You can create a button corresponding to each type of car. You can add listeners, and you can see from my ColourListener class how you might do it.
Keith Spriggs
Greenhorn

Joined: Jul 19, 2012
Posts: 18
I managed to get a working solution
I will post it up tomorrow morning so it can be evaluated upon.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18155
    
    8

Sounds like a good idea. Code review is a good thing and it should be done more in the real world as well as here.
Keith Spriggs
Greenhorn

Joined: Jul 19, 2012
Posts: 18
Hi Guys
This is the code for the main class
Thanks for all the help and advice.



This is the code for the called class
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling and Returning values from GUI
 
Similar Threads
Accessibility
Constructor initialization
Method trace
Differance in overriding variable and method
need help