I am currently trying to take the typed text in the textField 'itemName' and have it print out on the text area 'printArea.' When I have both printArea and itemName on the same JPanel (p1) it works just fine. When printArea is set to a separate JPanel (itemName on p1 and printArea on p2), nothing prints out. Two JPanels are used to make the GUI appear as it was assigned. The area i Believe to be the issue is where I added JPanel 'p1' to JPanel 'p2'.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MovieGUI extends JFrame{
JButton submit = new JButton("Submit");
JTextField itemName = new JTextField();
JTextField itemPrice = new JTextField();
JTextField itemQuantity = new JTextField();
JTextArea printArea = new JTextArea(400,400);
public MovieGUI(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(6, 2));
p1.add(new JLabel("Item Name"));
p1.add(itemName); //p1.add(new JTextField(8));
p1.add(new JLabel("Item Price"));
p1.add(itemPrice); // p1.add(new JTextField(8));
p1.add(new JLabel("Quantity"));
p1.add(itemQuantity); //p1.add(new JTextField(8));
p1.add(new JLabel("submit"));
p1.add(submit);
//Something is not working...
JPanel p2 = new JPanel(new BorderLayout());
p2.setLayout(new BorderLayout());
p2.add(p1, BorderLayout.NORTH);
p2.add(printArea, BorderLayout.SOUTH);
//add(p1);
add(p2);
event e = new event();
submit.addActionListener(e);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
String text = itemName.getText();
printArea.setText(text); //printArea.setText("BUTTON");
}
}
public static void main(String[] args){
MovieGUI frame = new MovieGUI();
frame.setTitle("Submission");
frame.setSize(800, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Your JPanel
p2
does not contain nothing on CENTER
alignment, so printArea doesn't view on SOUTH
alignment. To see the inputItem
text on printArea
change from
p2.add(printArea, BorderLayout.SOUTH);
to
p2.add(printArea, BorderLayout.CENTER);