Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have two questions about java gui mainly using stack I figured out the second

ID: 3805082 • Letter: I

Question

I have two questions about java gui mainly using stack

I figured out the second problem so I only need the first problem

1. Help me write my code about pop button handler

I cannot understand this instruction. whenever I write the code it does not work

this is the instruction and this is my code:

private class popButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
  if(myStack.isEmpty())
{System.out.println("Empty Stack!");}

else{ myStack.pop();

myStack.pop();

}
}}

I think i have to use myTextTA.set(//something); because even if i push the pop button nothing happens to the text area(?)

2. The second question is I am not done with my code yet but it should be now looking like

like this. However, mine looks like this:

I cannot figure out why...

what did I do wrong?

The below is my code so far :

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Iterator;
import java.util.Stack;

public class Lab9 extends JFrame {
private Stack mystack= new Stack();

//variables
private JLabel enterString, enterLineNumber, displayText;
private JTextField addString, lineNumber;
private JTextArea myTextTA;
private JButton pushButton, popButton, exitButton;
private pushButtonHandler p;
private popButtonHandler c;
private exitButtonHandler e;
private String currentfont= "Arial";
//not sure for this number 8
private int bold=Font.PLAIN;
private int italic= Font.PLAIN;
private int size=12;
private Container hold,radio,combo;
private JCheckBox boldCheck, italicCheck;
private JRadioButton redRadio,greenRadio,blueRadio, blackRadio;
private JComboBox fontCombo, sizeCombo;
private ButtonGroup mygroup;
//main method
public static void main(String[]args){
Lab9 lab= new Lab9();
}
  
//constructor
Lab9(){
//set title
setTitle("Stack");
Container pane= getContentPane();
pane.setLayout(null);
setSize(500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);

//JPanels
hold= new JPanel();
hold.setLocation(20, 150);
hold.setSize(100,150);
pane.add(hold);

radio= new JPanel();
radio.setLocation(100,150);
radio.setSize(100,150);
pane.add(radio);

combo= new JPanel();
combo.setLocation(20,300);
combo.setSize(200,30);
pane.add(combo);

//Check boxes
boldCheck= new JCheckBox("Bold");
pane.add(boldCheck);
italicCheck= new JCheckBox("Italic");
pane.add(italicCheck);

//ration buttons
redRadio= new JRadioButton("Red");
greenRadio= new JRadioButton("Green");
blueRadio= new JRadioButton("Blue");
blackRadio= new JRadioButton("Blue");
pane.add(redRadio);
pane.add(greenRadio);
pane.add(blueRadio);
pane.add(blackRadio);

//Combo box
String[] options= {"Arial","Times New Roman", "Century Gothic", "Courier", "Serif"};
fontCombo= new JComboBox(options);
pane.add(fontCombo);
Integer[] intOptions={12,14,16,18,20,22,24};
sizeCombo= new JComboBox(intOptions);
pane.add(sizeCombo);


//Button Group
mygroup= new ButtonGroup();
mygroup.add(redRadio);
mygroup.add(greenRadio);
mygroup.add(blueRadio);
mygroup.add(blackRadio);

//Item Listener
//if i compile this i have to see two check boxes four radio buttons and
//two combo boxes.... why i cannot see anything




//JLabel
enterString= new JLabel("Enter a String");
enterString.setLocation(20,20);
enterString.setSize(200,30);
pane.add(enterString);

displayText= new JLabel("Text Appears Here");
displayText.setFont(new Font(currentfont,bold,size));
displayText.setLocation(270,300);
displayText.setSize(200,30);
pane.add(displayText);

//Text Fields
addString= new JTextField(10);
addString.setLocation(20,50);
addString.setSize(200,30);
pane.add(addString);


//Text Area
  
myTextTA= new JTextArea(10,20);
myTextTA.setLocation(270, 50);
myTextTA.setSize(200,200);
pane.add(myTextTA);

//insertButton, clearButton, getButton, exitButton;
pushButton= new JButton("Push");
p= new pushButtonHandler();
pushButton.addActionListener(p);
pushButton.setLocation(20,100);
pushButton.setSize(80,30);
pane.add(pushButton);

popButton= new JButton("Pop");
c= new popButtonHandler();
popButton.addActionListener(c);
popButton.setLocation(140, 100);
popButton.setSize(80,30);
pane.add(popButton);

exitButton= new JButton("Exit");
e= new exitButtonHandler();
exitButton.addActionListener(e);
exitButton.setLocation(230,400);
exitButton.setSize(80,30);
pane.add(exitButton);


setVisible(true);

}
private class pushButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
String newline=" ";
mystack.push(addString.getText());
myTextTA.insert(addString.getText()+newline, 0);
}

}
private class popButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(mystack.empty()==true){System.out.println("Empty");}
else{
String popOut= mystack.pop(); }
//myTextTA.setText(mystack.get()); }
}}
  
  

private class exitButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.exit(0);

}
  
}

}

For our Pop button, instead of clearing the contents of the list, you will use the pop method to get the first String from the stack. This String will be displayed in our other label from Lab 9, so you will need to set the text of that label to the popped String. We also need to update the contents of the text area. The simplest approach is to traverse through the stack, and concatenate each String into one string (you have done this in previous labs). Since Stack is a subclass of Vector, we can use the same approach as a Vector (ie using a foreach loop). Set the text of the text area to the final concatenated String. Finally, to prevent any exceptions, make sure the stack isn't empty before popping At this point, you should be able to run your GUI program, add items to the stack, and pop items from the stack. The bottom label should display the most recently popped String.

Explanation / Answer

1) as per your program we get the error in line as you are refering the method pop() which you have not written at all.

String popOut= mystack.pop(); }

so if you want to use this function then you are suppose to create a class for stack operation and create a function called pop

public claSsize StackOpOp {

    protected Obj[] st; // Stack

    protected int Ssize;   // Stack Size

    protected int Scount;   // Stack Counter

   

    public StackOp(int initiCap)

    {

        if(initiCap < 1)

        throw new IllegalArgumentException ("initiCap must be >= 1");

       

        st = new Obj[initiCap];

        Ssize = initiCap;

        Scount = 0;

    }

   

    public StackOp()

    {

        st = null;

        Ssize = Scount = 0;

    }

   

    public void create(int initiCap)

    {

        if(initiCap < 1)

        throw new IllegalArgumentException ("initiCap must be >= 1");

       

        st = new Obj[initiCap];

        Ssize = initiCap;

        Scount = 0;

    }

   

    public boolean isfull()

    {

        return( Scount==Ssize );

    }

   

    public boolean isempty()

    {

        return( Scount==0 );

    }

   

    public Obj[] update(){

       

        return st;

    }

   

    public boolean push(Obj element)

    {

        if( Scount==Ssize )

        return false;

       

        st[Scount] = element;

        Scount++;

       

        return true;

    }

   

    public Obj pop()

    {

        if( Scount==0 )

        return false;

       

        Scount--;

        return st[Scount];

    }

   

    public String toString()

    {

        int i;

        StringBuffer s = new StringBuffer("[");

       

        for( i = 0; i < Scount; i++)

        s.append(st[i].toString() + ", ");

       

        for( ; i < Ssize; i++)

        s.append("__, ");

       

        if(Ssize > 0)

        s.delete(s.length() -2, s.length());

       

        s.append("]");

        return new String(s);

    }

    }

and in your jframe make changes for popbutton handler AS

first add a object for class StackOp AS SO

StackOp SO;

int size=0;

private class popButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(mystack.empty()==true){System.out.println("Empty");}
else{
SO.pop();
      
        displayText.setText("popped : "+SO.st[SO.Scounter].toString());
        refresh(); }
}}

2) adding these lines in your main program will also make all your buttons visible

SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                new lab9().setVisible(true);

            }