I am trying to create a 2x2 grid out of 4 buttons for a membership program I am developing. The issue I'm having is that regardless of what I do, it just shows up as a 1x4 grid. Code is as follows.
private void buildStartupPanel()
{
startup = new JPanel();
startup.setLayout(new GridLayout(2,2));
addMember = new JButton ("Add a new member");
removeMember = new JButton ("remove Member");
reviewMember = new JButton ("Review a Member");
reviewAll = new JButton ("Review All Members");
startup.add(addMember);
startup.add(removeMember);
startup.add(reviewMember);
startup.add(reviewAll);
addMember.addActionListener(this);
removeMember.addActionListener(this);
reviewMember.addActionListener(this);
reviewAll.addActionListener(this);
}
When I output the result, it shows the following
Add a new Member
Remove Member
Review A Member
Review all Members
Instead of
Add a new Member Remove A Member
Review A Member Review all Members
Also if anyone could help me put a space between each of the buttons that would be great!
Use the 3rd & 4th int to the constructor for spacing. Otherwise, seems to work just fine here:
import java.awt.GridLayout;
import javax.swing.*;
public class StartupPanel {
private JComponent getStartupPanel()
{
JPanel startup = new JPanel();
startup.setLayout(new GridLayout(2,2,50,5));
JButton addMember = new JButton("Add a new member");
JButton removeMember = new JButton("remove Member");
JButton reviewMember = new JButton("Review a Member");
JButton reviewAll = new JButton("Review All Members");
startup.add(addMember);
startup.add(removeMember);
startup.add(reviewMember);
startup.add(reviewAll);
return startup;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
StartupPanel sp = new StartupPanel();
JOptionPane.showMessageDialog(null, sp.getStartupPanel());
}
});
}
}
thanks for the responses!! Come to find out it was my 2nd panel that I was adding to the code was misspelled (woops) and throwing everything off. Guess that's the importance of posting a full SSCCE. At least I learned how to do the spacing! thanks all!
Related
Ok, i am just a beginner programmer, so i am having a lot of difficulty in figuring this out. Basically i am trying to create a one digit calculator(meaning that calculations only occur with single digits of numbers). I have created the buttons, assigned them action listener and their classes, and all those stuff. And then i try to display those numbers to a label. Now the problem i have is, that, i have a button, which when clicked, will use a class. From that class, what i want to do is, remove all the buttons form the panel, and add new buttons. But when i try to remove the buttons, something weird happens. If i click that button, the buttons instead of getting removed/disappering, they stay there, but i cant interact with them. Any help to fix that? I want to completely remove them from the panel. Then i want to add new buttons in their place.
Here is the code of the main class
package onecalculator;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class code {
static JLabel see = new JLabel("Int a");
static JLabel no = new JLabel("Int b");
static JLabel lol = new JLabel("Answer");
static JPanel area = new JPanel();
static JButton secn = new JButton("next");
static JButton one = new JButton("1");
static JButton two = new JButton("2");
static JButton three = new JButton("3");
static JButton four = new JButton("4");
static JButton five = new JButton("5");
static JButton six = new JButton("6");
static JButton seven = new JButton("7");
static JButton eight = new JButton("8");
static JButton nine = new JButton("9");
static JButton bone = new JButton("1");
static JButton btwo = new JButton("2");
static JButton bthree = new JButton("3");
static JButton bfour = new JButton("4");
static JButton bfive = new JButton("5");
static JButton bsix = new JButton("6");
static JButton bseven = new JButton("7");
static JButton beight = new JButton("8");
static JButton bnine = new JButton("9");
static JButton div = new JButton("div");
static JButton mul = new JButton("mul");
static JButton add = new JButton("add");
public int a;
public int b;
public static void main(String[] args) {
JFrame screen = new JFrame("One Digit Calculator");
screen.setSize(400,600);
screen.setResizable(false);
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.add(area);
area.add(see);
area.add(no);
area.add(lol);
area.add(secn);
area.add(one);
area.add(two);
area.add(three);
area.add(add);
area.add(four);
area.add(five);
area.add(six);
area.add(mul);
area.add(seven);
area.add(eight);
area.add(nine);
area.add(div);
secn.addActionListener(new secn());
two.addActionListener(new Twoc());
three.addActionListener(new Threec());
four.addActionListener(new Fourc());
five.addActionListener(new Fivec());
six.addActionListener(new Sixc());
seven.addActionListener(new Sevenc());
eight.addActionListener(new Eightc());
nine.addActionListener(new Ninec());
one.addActionListener(new Onec());
area.setLayout(new GridLayout(4,4));
screen.setVisible(true);
}
}
Then here is the code of the class that removes the buttons in the panel
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class secn implements ActionListener {
#Override
public void actionPerformed(ActionEvent arg0) {
code.area.remove(code.one);
code.area.remove(code.two);
code.area.remove(code.three);
code.area.remove(code.four);
code.area.remove(code.five);
code.area.remove(code.six);
code.area.remove(code.seven);
code.area.remove(code.eight);
code.area.remove(code.nine);
code.area.add(code.bone);
code.area.add(code.btwo);
code.area.add(code.bthree);
code.area.add(code.bfour);
code.area.add(code.bfive);
code.area.add(code.bsix);
code.area.add(code.bseven);
code.area.add(code.beight);
code.area.add(code.bnine);
}
}
Please help.
What you would want to do is to call repaint() and revalidate() on the container (i.e. 'JPanel area') that holds you buttons. If you want to know exactly what repaint and revalidate do, have a look at this answer.
Below your code where you add your new buttons inside of the actionPerformed method, add the following to update the container:
code.area.repaint();
code.area.revalidate();
Keep in mind that this will cause your new elements to be added to the end of the elements that weren't deleted and in the order that you add them. You can use GridBagConstraints to select where which button is placed.
But I would say that removing the old buttons just to create new ones only for the purpose of entering the second value seems like a bad idea. Additionally, having separate ActionListeners for each button also seems a little wasteful.
I would propose having a global variable (boolean for example) to indicate whether you're using the first or second value.
static boolean isFirst = true;
When the 'next button' is pressed, you could then change this variable to 'false' and not remove any of the buttons. In your ActionListener you would just look at this variable to know whether to assign the pressed number to value a or value b.
For your ActionListener for the number buttons, I would propose to reuse one for all of them like this:
class MyListener implements ActionListener{
int value;
//when creating new instances of MyListener, you give each listener
//an int equivalent to the buttons value
MyListener(int value){
this.value = value;
}
#Override
public void actionPerformed(ActionEvent arg0){
if(isFirst){ //first value
a = value; //add value to your first number in any way you like
} else { //second value
b = value; //add value to your second number in any way you like
}
}
}
You would assign your ActionListener as follows:
two.addActionListener(new MyListener(2));
three.addActionListener(new MyListener(3));
I hope this is helpful and understandable. There are probably better ways to do it, but this would be my suggestion. I'm open to feedback on this.
The easiest solution would be to create a new JPanel instead of deleting the old buttons.
JPanel newArea= new JPanel();
//Add new buttons
code.area = newArea;
However i think you should consider redesigning your code. First of all you should not use static variables for your code class. Instead make them private and make a objekt of your code class.
public class code{
private JLabel see = new JLabel("Int a");
private JLabel no = new JLabel("Int b");
//...
public static void main(String[] args){
code mainClass = new code();
}
This allows you to use multiple instances of your code class instead of just one. Secondly you should not make a new class for every Actionlistener. Ecspecially since i think they are all doing the same. Also i dont think you even need to remake all the buttons. If you just want to save 2 values you can check if the first one has been set already:
class ButtonListener implements ActionListener{
int buttonValue;
code callingClass;
//Save the buttonn number and the calling class
MyListener(int buttonValue, code callingClass){
this.buttonValue = buttonValue;
this.callingClass = callingClass;
}
//If a is null set a, otherwise set b
public void actionPerformed(ActionEvent arg0){
if(callingClass.a == null){
callingClass.a = buttonValue;
} else {
callingClass.b = buttonValue;
}
}
}
In your code class you should then initialize a and b with null in your constructor, alongside all the stuff you preivously initialized in your main and use the ButtonListener class as your new actionlistener.
public code(){
a = null;
b = null;
//Initialize all the other stuff
secn.addActionListener(new ButtonListener(2, this));
}
Whenever you add or remove elements to a visible Swing component, you should use revalidate() (to recalculate its layout now that children have changed) and repaint() (to repaint the component itself), as suggested by Custos' answer.
However, there is the question of why you need to do this. You have two visually identical sets of digit-buttons, that only differ in how they handle clicks. There is no need to replace the buttons - just handle those differences by keeping a little bit of extra state (hasA and hasB varibales in my code below).
Since the introduction of lambdas (inline, anonymous functions), writing handler code for Java UIs has become much more readable and less verbose: note how my code below has 1-line handlers to bind the interface with actual logic, and how easy it would be to, say, add a new operator to the code below.
Note also that you do not need expose all graphical elements of a class as fields - here, digits and operators are only used to call digitPressed and operatorPressed, and lead to a smaller set of fields in the Calc class.
Also, by making Calc a subclass of JPanel, and avoiding any and all static fields, I can easily create multiple calculators operating side-by-side, independent of each other.
package one;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class Calc extends JPanel {
private JLabel labelA = new JLabel();
private JLabel labelB = new JLabel();
private JLabel labelAnswer = new JLabel();
private int a;
private int b;
private boolean hasA = false;
private boolean hasB = false;
public Calc() {
setLayout(new GridLayout(0, 4));
add(labelA);
add(labelB);
add(labelAnswer);
reset(); // rewrites labels, resets state
JButton reset = new JButton("reset");
add(reset);
reset.addActionListener((e) -> reset());
for (int i=1; i<10; i++) {
JButton b = new JButton("" + i);
add(b);
final int index = i; // for use in lambda
b.addActionListener((e) -> digitPressed(index));
}
for (String op : new String[] {"add", "mul", "div"}) {
JButton b = new JButton(op);
add(b);
final String index = op; // for use in lambda
b.addActionListener((e) -> operatorPressed(index));
}
}
private void reset() {
labelA.setText("value A: ?");
labelB.setText("value B: ?");
labelAnswer.setText("no operator");
hasA = hasB = false;
}
private void digitPressed(int i) {
if ( ! hasA) {
hasA = true;
a = i;
labelA.setText("value A:" + a);
} else if ( ! hasB) {
hasB = true;
b = i;
labelB.setText("value B:" + b);
}
}
private void operatorPressed(String operator) {
String answer = "???";
if (operator.equals("mul")) {
answer = "= " + (a * b);
} else if (operator.equals("div")) {
answer = "= " + (a / b);
} else if (operator.equals("add")) {
answer = "= " + (a + b);
}
labelAnswer.setText(answer);
}
public static void main(String[] args) {
JFrame screen = new JFrame("One Digit Calculator");
screen.setSize(400,600);
screen.setResizable(false);
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.add(new Calc());
screen.setVisible(true);
}
}
I managed to put this GUI together by adapting code from a book. I'm not sure how to implement listeners into this code since it is above my level, but I need them to make testing my program easier. I've looked through the posts on this site that are similar, however no one used the same code structure as me and I'm not sure how to implement the solutions on those posts.
So far I've tried using JCheckbox.addActionListener(this); which is what the oracle website said. Where ever I place this statement I get a variety of errors and I'm not sure why.
package inputform;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
public class Traitform extends JFrame implements ActionListener{
JPanel row1 = new JPanel();
JCheckBox eye1 = new JCheckBox("Brown");
JCheckBox eye2 = new JCheckBox("Blue");
JCheckBox eye3 = new JCheckBox("Green");
JCheckBox eye4 = new JCheckBox("Hazel");
JPanel row2 = new JPanel();
JCheckBox hair1 = new JCheckBox("Brown");
JCheckBox hair2 = new JCheckBox("Blonde");
JCheckBox hair3 = new JCheckBox("Ginger");
JCheckBox hair4 = new JCheckBox("Black");
JPanel row3 = new JPanel();
JCheckBox eye5 = new JCheckBox("Brown");
JCheckBox eye6 = new JCheckBox("Blue");
JCheckBox eye7 = new JCheckBox("Green");
JCheckBox eye8 = new JCheckBox("Hazel");
JPanel row4 = new JPanel();
JCheckBox hair5 = new JCheckBox("Brown");
JCheckBox hair6 = new JCheckBox("Blonde");
JCheckBox hair7 = new JCheckBox("Ginger");
JCheckBox hair8 = new JCheckBox("Black");
public Traitform () {
super("Parent Trait Form");
setSize(1000, 1000);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(0,1);
setLayout(layout);
row1.add(eye1);
row1.add(eye2);
row1.add(eye3);
row1.add(eye4);
row3.add(eye5);
row3.add(eye6);
row3.add(eye7);
row3.add(eye8);
row2.add(hair1);
row2.add(hair2);
row2.add(hair3);
row2.add(hair4);
row4.add(hair5);
row4.add(hair6);
row4.add(hair7);
row4.add(hair8);
add(row1);
add(row2);
add(row3);
add(row4);
setVisible(true);
}
private static void setLookAndFeel() {
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc){
}
}
public static void main(String[] arguments){
Traitform.setLookAndFeel();
Traitform frame = new Traitform();
}
#Override
public void actionPerformed(ActionListener e){
System.out.println("action");
}
}
I need this program to take the user input from the check boxes so that it can be used in another part of my program in a separate package. When I run the current code I get the error uncompilable program.
This is clearly wrong:
JCheckbox.addActionListener(this);
addActionListener is not a static method. Also you probably want to do different things when different checkboxes are checked.
So you should do something like this:
hair5.addActionListener(() -> {System.out.println("hair5 clicked")});
You can also add single action listener for all checkboxes. For example put all your checkboxes in a list then do something like:
ActionListener myActionListener = () -> {System.out.println("my action listener")}
for (checkbox: checkboxList) {
checkbox.addActionListener(myActionListener);
}
The argument of addActionListener() method is an ActionListner (see doc).
So too use eye1.addActionListener(this); (for example) this need to be an ActionListner. ActionListener is an interface, which means this this needs to implement ActionListener.
this , in your case is traitform class (which should be Traitform see Java naming conventions) :
After changing the class definition to
public class Traitform extends JFrame implements ActionListener{
You need to add the implementation. In this case it is one method:
#Override
public void actionPerformed(ActionEvent e) {
// what ever you write here is invoked when action listener is used.
System.out.println("Action listener fired !");
}
Following the example above (eye1.addActionListener(this);), the actionPerformed method is invoked when eye1 fires an ActionEvent.
Im still pretty new to java and I wrote a magic 8 ball program awhile ago. Well I just learned about JFrames and JPanels, so i wanted to update it.
Im having trouble with a couple of things, and i dont know if this is asking too much because of never used this website before. i dont understand actionlisteners too well and i need help putting one on the b button to make it paint a new JPanel that paints my random chooser statements. That's the other thing, how do i make it paint one of my array elements?
window
package pack;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class window extends ball {
public static void main (String []args) {
JFrame f = new JFrame("hello");
JPanel p = new JPanel();
JLabel l = new JLabel("What do you ask the magic conch shell?");
JTextField t = new JTextField(25);
JButton b = new JButton();
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { }
});
//panel
b.setText("Ask");
p.add(l);
p.add(b);
p.add(t);
//frame
f.add(p);
f.setSize(300, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}}
ball
package pack;
import java.util.Scanner;
import java.util.Random;
public class ball {
public static void main(String[]args) {
String question = ("What do you ask the Magic Conch shell?");
Scanner userInput =new Scanner(System.in);
String answer = userInput.nextLine();
String[] array;
array = new String[8];
array[0] = ("yes");
array[1] = ("no");
array[2] = ("maybe");
array[3] = ("never");
array[4] = ("ask again later");
array[5] = ("positive");
array[6] = ("unlikely");
array[7] = ("yes");
Random dice = new Random();
int n = dice.nextInt(8);
System.out.println(array[n]);
}
}
Take the code from your ball class, use it to create a method which returns the result of the roll, this provides you a point of re-use
public class Ball {
private String[] results = new String[] {
"yes", "no", "maybe", "never", "ask again later", "postive", "unlikely", "yes"
};
private Random random = new Random();
public String roll() {
int n = random.nextInt(results.length);
return results[n];
}
}
Now, in your buttons ActionListener, you need to "roll" the ball
Ball ball = new Ball();
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String result = ball.roll();
}
});
Now, you next problem is, you don't actually have anywhere to display the result.
You really don't want to introduce a new component at this stage if you can help it, instead, you want to have one ready to use, so, let's add another JLabel to act as the "answer"
Ball ball = new Ball();
JLabel answer = new JLabel("...");
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String result = ball.roll();
answer.setText(result);
}
});
//panel
b.setText("Ask");
p.add(l);
p.add(b);
p.add(t);
p.add(answer);
Adding components at runtime isn't difficult, but it does introduce a number of other issues you need to aware of. In this, we've limited some of the issues by introducing a component which we can simply update as needed
I've been working on this project for an assignment and I've been stuck on this problem. I new and don't understand much of the programming jargon so if someone could help explain why my program isn't working that would be great.
The programs purpose is to display a randomly generated matrix of 1's and 0's in a 10x10 layout and have some buttons on the top that have functions. I'm just stock on how to get everything to display.
Thanks in advance.
UPDATE:: Told providing all my code would help
public class Module5 extends JFrame {
private static JTextArea area = new JTextArea();
private static JFrame frame = new JFrame();
private static JPanel general = new JPanel();
private static JPanel buttons = new JPanel();
private static JPanel numbers = new JPanel();
private static JButton button0 = new JButton("Reset to 0");
private static JButton button1 = new JButton("Resset to 1");
private static JButton buttonReset = new JButton("Reset");
private static JButton quit = new JButton("Quit");
public static class Numbers extends JPanel {
public Numbers() {
area.setText(Integer.toString((int) Math.round(Math.random())));
this.add(area);
}
public void Module5(){
numbers.setLayout(new GridLayout(10, 10));
for (int i = 0; i < 100; i++) {
this.add(new Numbers());
}
}
}
public static void main (String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
general.setLayout(new BoxLayout(general, BoxLayout.Y_AXIS));
general.add(buttons);
general.add(numbers);
buttons.add(button0);
buttons.add(button1);
buttons.add(buttonReset);
buttons.add(quit);
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
}
Since this does look like homework I'll give you some pointers but am not going to give you the code.
Move your constructor for Module5 out of the numbers class and into its own class. Also remove the void return type from this to make it a correct constructor.
Move the code in your main into the constructor for Module5. This is the main frame so when you build a new one it should be initialised here, not in main. And remove the setVisible call for now (this is addressed in number 6)
After doing 1 and 2, get rid of your frame variable, your Module5 is a JFrame so anything to do with frame can just be changed to the keyword this (meaning this Module5 object)
Also move the area variable to be within the Numbers class - otherwise every Number is essentially going to share the same text area and This is not what you want.
Don't have your variables as static they should not need to be.
Once this is all done make sure it is running on the Event Dispatch Thread by making your main method like this (the one piece of code I will give you)
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
Module5 mod5 = new Module5();
mod5.setVisible(true);
}
});
}
Very new to Java, but I am slowly picking my way through things. So please be kind. I understand most things I've tried so far, and built a version of the following that uses console output, but now I'm trying to make a GUI. I tried the netbeans GUI maker, but it created so much new code that when I tried to pick through it, I got lost. I'm much better at learning by piecing new things together myself, not having an IDE generate a ton of code and then attempt to find where I want to work.
I am trying to build an window that has a list with three choices on the left side, a button in the middle that confirms your choice, and an answer output on the right. Once the button is pressed, the input from the list is read and is converted into a corresponding answer. As of right now, all I get is "We recommend... null" after selecting an option in the list. The button appears to do nothing at the moment.
I have used tutorials, hacked up others' code from online, and referenced a few books, but I'm stuck.
Here is what I have:
package diffguidegui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class DiffGuideGUI extends JPanel implements ListSelectionListener {
private JList resultsTabList;
private DefaultListModel listModel;
private static final String recommendString = "Recommend a Option";
private JButton recommendButton;
private String recommendOutput;
final JLabel output = new JLabel("We recommend..." + recommendOutput);
//build list
public DiffGuideGUI () {
super(new BorderLayout());
listModel = new DefaultListModel();
listModel.addElement("A");
listModel.addElement("B");
//create the list and put it in the scroll pane
resultsTabList = new JList(listModel);
resultsTabList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
resultsTabList.setSelectedIndex(0);
//listener for user input
resultsTabList.addListSelectionListener(this);
resultsTabList.setVisibleRowCount(2);
JScrollPane listScrollPane = new JScrollPane(resultsTabList);
//build the button at the bottom to fire overall behavior
recommendButton = new JButton(recommendString);
recommendButton.setActionCommand(recommendString);
recommendButton.addActionListener(new RecommendListener());
//create a panel that uses Boxlayout for the button
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.add(recommendButton);
//create a panel that uses Boxlayout for the label
JPanel outputPane = new JPanel();
outputPane.setLayout(new BoxLayout(outputPane, BoxLayout.LINE_AXIS));
outputPane.add(output);
add(listScrollPane, BorderLayout.WEST);
add(buttonPane, BorderLayout.CENTER);
add(outputPane, BorderLayout.EAST);
}
//build listener class
class RecommendListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//build in logic for choice made here
String resultsTabChoice;
resultsTabChoice = (String)resultsTabList.getSelectedValue();
if( resultsTabChoice.equals("A")) {
recommendOutput = "One";}
else {recommendOutput = "Two";}
}
}
public void valueChanged(ListSelectionEvent e) {
if(e.getValueIsAdjusting() == false) {
if(resultsTabList.getSelectedIndex() == -1) {
recommendButton.setEnabled(false);
} else {
recommendButton.setEnabled(true);
}
}
}
//Create GUI and show it
private static void createAndShowGUI() {
JFrame frame = new JFrame("Recommend Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create and set up content pane
JComponent newContentPane = new DiffGuideGUI();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
//display the window
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The button appears to do nothing at the moment.
It does something. It calculates the value for your recommendOutput varable. But you never output this value.
try the following:
//build listener class
class RecommendListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//build in logic for choice made here
String resultsTabChoice;
resultsTabChoice = (String)resultsTabList.getSelectedValue();
if( resultsTabChoice.equals("A")) {
recommendOutput = "One";}
else {recommendOutput = "Two";}
System.out.println(recommendOutput); // <-###################
}
}
This should print the value to stdout
To put the value into your label try this instead:
output.setText(recommendOutput);
where do you set the text for the JLabel? It says "We recommend NULL" because recommenedOutput is null when the object is created. I dont see
output.setText("We recommend "+value) anywhere. You probably need output.invalidate() also. Try putting setText(String text)/invalidate() in the RecommendListener.actionPerformed() method.
output.setText("We recommend A");
output.invalidate();