• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling and Returning values from GUI

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each button has the line which I missed out on


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

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I managed to get a working solution
I will post it up tomorrow morning so it can be evaluated upon.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic