Can anyone help me use a JLabel to display a stack??? I've been searching java.sun.com and I can't seem to find the right way to do it. I get errors every time. I think everything is right in my program besides this. Thanks for looking. Here's what I have:
(Am I posting the code right?)

public class GraphicStackPanel extends JPanel {

    private JLabel inputLabel, outputLabel, resultLabel;
    private JTextField pushstring;
    private JButton push, pop;

    int count = 0;

    public GraphicStackPanel(){

        inputLabel = new JLabel ("Enter word here: ");

        pushstring = new JTextField (10);
        push = new JButton ("Push!");
        push.addActionListener (new ButtonListener());

        outputLabel = new JLabel ("Your Stack: ");
        resultLabel = new JLabel ("--");

        pop = new JButton ("Pop!");
        pop.addActionListener (new ButtonListener());

        add(inputLabel);
        add(pushstring);
        add(push);
        add(pop);
        add(outputLabel);
        add(resultLabel);

        setPreferredSize (new Dimension (400, 300));
        setBackground (Color.white);
    }
        private class ButtonListener implements ActionListener{

            Stack stack = new Stack();

            public void actionPerformed (ActionEvent event){

            if (event.getSource() == push){
                stack.push (pushstring);

                resultLabel.setText(stack);
            }
            else{
                    stack.pop ();
                    resultLabel.setText(stack);
            }

            }
        }
}

Dani AI

Generated

Short summary and what likely caused the strange output: you were pushing the JTextField component itself and then asking the label to show the stack, so the label displayed the component's toString/UI info (the "ColorUIResource..." text). Also, because a new ButtonListener (and therefore a new Stack) was created for each button, a pop could operate on an empty stack. spotted the string/label issue and confirmed the symptoms.

Keep one stack instance in the panel and reuse one listener (or have the listener refer to the panel-level stack). Build the stack with generics so the model is clear:

private final Stack<String> stack = new Stack<String>();
private final ActionListener sharedListener = new ButtonListener();

push.addActionListener(sharedListener);
pop.addActionListener(sharedListener);

To show the whole stack in a JLabel (multi-line), format it as HTML and display top-first so the visual order matches LIFO:

private void refreshLabel() {
    StringBuilder html = new StringBuilder("<html>");
    for (int i = stack.size() - 1; i >= 0; i--) {
        html.append(stack.get(i)).append("<br/>");
    }
    html.append("</html>");
    resultLabel.setText(html.toString());
}

Notes and alternatives: always check for emptiness before popping to avoid exceptions. For a more natural interactive view use a JList with a DefaultListModel (push: insert at index 0; pop: remove index 0) — it handles multi-line display and selection without HTML hacks. Consider using Deque<String> (ArrayDeque) instead of the legacy Stack class for production code. Finally, clear and refocus the text field after a push and build the GUI on the EDT.

Recommended Answers

All 4 Replies

No you haven't posted the code right. The last code tag should have a forward slash like this: (/CODE)

For your problem, In your ActionListener you are passing a Stack object to the JLabel setText() method when it was expecting a String.

Instead of using this:

resultLabel.setText(stack);

YOu should have this:

resultLabel.setText(stack.toString());

and don't forget to remove the extra space in between "pop" and parenthesis:

else{
stack.pop ();
resultLabel.setText(stack);
}

thanks for your reply jocamps; i actually thought the same thing and had the code like that at first; the program compiled and ran until i entered text into the text field and got the following print when i tried to push: ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.Color...etc
and 'EmptyStackCollection' when i tried to pop.
any more suggestions???

set your stack as a global variable. In your code, what happens is, everytime you click a button you created a new Stack object it would work with push() since you have the initial value to be push but when click the pop button, a new Stack object is created which is empty (even if you click push() first).

also you could include an if statement to check if the stack is empty before using pop() method.

just noticed you were using a JTextField , so your stack.push(pushstring) should be :

stack.push(pushstring.getText());
commented: exceptional +0

I highly appreciate your help, jocamps; your solutions worked!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.