Error while controling visibility with setVisible method - java

While controlling the visibility of some components through a button, I am getting stackoverflow error.
I am setting setVisible(true) for some components based on the click of a button with actionperformed() method.
I am getting stackoverflow error on line 25 and 55.
import java.awt.event.*;
public class Expanse extends Frame {
Label l1, l2;
Button b1, b2, b3;
Frame f1;
TextField tf1, tf2;
Expanse() {
l1 = new Label("welcome user");
Font font1 = new Font("SansSerif", Font.BOLD, 25);
Font font2 = new Font("SansSerif", Font.BOLD, 18);
l1.setFont(font1);
addWindowListener(new myWindowAdapter());
l1.setBounds(100, 25, 300, 50);
add(l1);
l2 = new Label("please click any button from below");
l2.setBounds(75, 100, 500, 50);
l2.setFont(font2);
add(l2);
b1 = new Button("add data");
b1.setBounds(70, 175, 100, 50);
add(b1);
b1.addActionListener(new addListener());
b2 = new Button("retrive data");
b2.setBounds(275, 175, 100, 50);
add(b2);
setLayout(null);
setSize(500, 500);
setVisible(true);
tf1 = new TextField("enter amount");
tf2 = new TextField("enter reason");
tf1.setBounds(70, 260, 150, 30);
add(tf1);
tf2.setBounds(70, 320, 150, 30);
add(tf2);
b3 = new Button("write");
b3.setBounds(100, 380, 80, 35);
add(b3);
tf1.setVisible(false);
tf2.setVisible(false);
b3.setVisible(false);
}
public static void main(String a[]) {
Expanse e1 = new Expanse();
}
}
class myWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
class addListener extends Expanse implements ActionListener {
public void actionPerformed(ActionEvent ae) {
tf1.setVisible(true);
tf2.setVisible(true);
b3.setVisible(true);
}
}

Within Expanse constructor you have:
b1.addActionListener(new addListener());
which constructs an addListener object. 1
addListener extends Expanse so you are creating another Expanse object withing the Expanse constructor.
As pointed out by trashgod this is an endless process which causes the stackoverflow.
The cure is simple: there is no reason for addListener to extend Expanse.
class addListener implements ActionListener {...}
I assume the extends Expanse was added to gain access to tf1 and other class variables, but it is not the way to do it.
Alternatively simply have addListener class, an inner class in Expanse.
1Side note: see Java Naming Conventions

Related

JButtons are not being placed in the correct spot or are disappearing

I am a beginner programmer and I was trying to make a simple calculator, but for some reason, the buttons don't seem to work properly and they either disappear, fill up the whole panel, or are set in the wrong positions. The weird thing is that the buttons appear when I hover and click my mouse over them. I might be missing something very obvious but please help (Also the code isn't very clean). Thank you!
Main
public abstract class Main {
public static JFrame frame = new JFrame("Calculator");
public static JPanel panel = new JPanel();
public static CaculatorButton buttons = new CalculatorButton();
public static void main(String[] args) {
Window.window(frame, panel);
buttons.b0(panel);
}
}
CalculatorButton
public class CaculatorButton {
private static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private static JTextArea text;
private static JButton plus;
private static JButton minus;
private static JButton equals;
private static String temp;
public void b0(JPanel panel) {
b0 = new JButton("0");
b0.setFocusable(false);
b0.setBackground(Color.GRAY);
b0.setPreferredSize(new Dimension(80, 80));
panel.add(b0);
b0.setBounds(10, 395, 80, 80);
b1(panel); b2(panel); b3(panel); b4(panel); b5(panel); b6(panel); b7(panel); b8(panel); b9(panel); textArea(panel); plus(panel); minus(panel);
equals(panel); input();
}
private static void b1(JPanel panel) {
b1 = new JButton("1");
b1.setFocusable(false);
b1.setBackground(Color.GRAY);
b1.setPreferredSize(new Dimension(80, 80));
panel.add(b1);
b1.setBounds(10, 140, 80, 80);
}
private static void b2(JPanel panel) {
b2 = new JButton("2");
b2.setFocusable(false);
b2.setBackground(Color.GRAY);
b2.setPreferredSize(new Dimension(80, 80));
panel.add(b2);
b2.setBounds(95, 140, 80, 80);
}
private static void b3(JPanel panel) {
b3 = new JButton("3");
b3.setFocusable(false);
b3.setBackground(Color.GRAY);
b3.setPreferredSize(new Dimension(80, 80));
panel.add(b3);
b3.setBounds(180, 140, 80, 80);
}
private static void b4(JPanel panel) {
b4 = new JButton("4");
b4.setFocusable(false);
b4.setBackground(Color.GRAY);
b4.setPreferredSize(new Dimension(80, 80));
panel.add(b4);
b4.setBounds(10, 225, 80, 80);
}
private static void b5(JPanel panel) {
b5 = new JButton("5");
b5.setFocusable(false);
b5.setBackground(Color.GRAY);
b5.setPreferredSize(new Dimension(80, 80));
panel.add(b5);
b5.setBounds(95, 225, 80, 80);
}
private static void b6(JPanel panel) {
b6 = new JButton("6");
b6.setFocusable(false);
b6.setBackground(Color.GRAY);
b6.setPreferredSize(new Dimension(80, 80));
panel.add(b6);
b6.setBounds(180, 225, 80, 80);
}
private static void b7(JPanel panel) {
b7 = new JButton("7");
b7.setFocusable(false);
b7.setBackground(Color.GRAY);
b7.setPreferredSize(new Dimension(80, 80));
panel.add(b7);
b7.setBounds(10, 310, 80, 80);
}
private static void b8(JPanel panel) {
b8 = new JButton("8");
b8.setFocusable(false);
b8.setBackground(Color.GRAY);
b8.setPreferredSize(new Dimension(80, 80));
panel.add(b8);
b8.setBounds(95, 310, 80, 80);
}
private static void b9(JPanel panel) {
b9 = new JButton("9");
b9.setFocusable(false);
b9.setBackground(Color.GRAY);
b9.setPreferredSize(new Dimension(80, 80));
panel.add(b9);
b9.setBounds(180, 310, 80, 80);
}
private static void plus(JPanel panel) {
plus = new JButton("+");
plus.setFocusable(false);
plus.setBackground(new Color(0, 200, 150));
plus.setPreferredSize(new Dimension(80, 80));
panel.add(plus);
plus.setBounds(95, 395, 80, 80);
}
private static void minus(JPanel panel) {
minus = new JButton("-");
minus.setFocusable(false);
minus.setBackground(new Color(0, 200, 150));
b0.setPreferredSize(new Dimension(80, 80));
panel.add(minus);
minus.setBounds(180, 395, 80, 80);
}
private static void equals(JPanel panel) {
equals = new JButton("=");
equals.setFocusable(false);
equals.setBackground(new Color(200, 125, 0));
b0.setPreferredSize(new Dimension(80, 335));
panel.add(equals);
equals.setBounds(265, 140, 80, 335);
}
private static void input() {
JButton[] buttons = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9};
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i <= 9; i++) {
if(e.getSource()== buttons[i]) {
String str = Integer.toString(i);
if (temp == null) {
temp = str;
} else {
temp = temp + str;
text.setText(temp);
}
}
}
}
};
b0.addActionListener(al);
b1.addActionListener(al);
b2.addActionListener(al);
b3.addActionListener(al);
b4.addActionListener(al);
b5.addActionListener(al);
b6.addActionListener(al);
b7.addActionListener(al);
b8.addActionListener(al);
b9.addActionListener(al);
}
private static void textArea(JPanel panel) {
text = new JTextArea("");
panel.add(text);
text.setVisible(true);
text.setFocusable(true);
text.setForeground(new Color(51,255,255));
text.setFont(text.getFont().deriveFont(25f));
text.setBackground(Color.DARK_GRAY);
text.setBounds(10, 10, 335, 120);
}
}
Window
public class Window extends JFrame {
public static void window(JFrame frame, JPanel panel) {
frame.setSize(370, 522);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
panel.setBackground(Color.DARK_GRAY);
}
}
Don't use static variables. Don't use static methods. This is not the proper design.
Your class can then:
create the instance variables that are needed.
create and add all the buttons to the panel.
There is no need to create a separate method for each button. Use a loop to create and then add each button to the panel. The panel should use a layout manager so you don't need to worry about the size/location of the button. The GridLayout would be the easies to use. It will add the buttons in a row/column grid.
See: How to add a shortcut key for a jbutton in java? for a working example that uses this approach. It will show a better design for your class that incorporates all the above suggestions.
The weird thing is that the buttons appear when I hover and click my mouse over them.
Components should be added to the frame BEFORE the frame is made visible.
Your JPanel is constructed using the default constructor, which means it has a FlowLayout as the LayoutManager. That means that setting the bounds of each button manually will have strange effects. Try using an appropriate LayoutManager of your liking. Or create a custom LayoutManager (by implementing that interface) which will not change the location of the components on revalidation.
There is a corresponding tutorial on LayoutManagers:
https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html
Note: JPanel is-a Container so that is why the tutorial applies also to JPanels. You are already using a LayoutManager (named BorderLayout) for the JFrame named frame... You should also do that for the JPanel. It has a FlowLayout by default, but by setting manually the bounds of each JButton one can understand that you don't really need a FlowLayout but probably something else. You can give us a picture in your question on how you would like the layout to look like, so that we can give you directions on which LayoutManager to try.
Edit 1:
Because you are trying to create a calculator, I would suggest to try out GridLayout with 3 columns (and 0 as the rows argument, which will make the grid of always creating a new row when 3 columns are already reached).

JFrame: navigate to another frame using JButton

I'm trying to navigate with buttons in Java Swing.
I want to go to another existing frame when i click on a button.
I will attach first the Menu Panel:
public class MenuPanel extends JFrame implements ActionListener {
JLabel l1,l2;
JButton btn1,btn2,btn3,btn4;
MenuPanel()
{
setVisible(true);
setSize(600, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Menu Panel For Final Project");
l1 = new JLabel("Menu Panel Bitches: ");
l2 = new JLabel("This Program Made By Ghetto K");
l1.setForeground(Color.RED);
l1.setFont(new Font("Arial", Font.BOLD, 28));
l2.setForeground(Color.RED);
l2.setFont(new Font("Arial", Font.ITALIC,22));
btn1 = new JButton("Add Child");
btn2 = new JButton("Add Worker");
btn3 = new JButton("Add Class");
btn4 = new JButton("Add Task");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
l1.setBounds(150, 30, 400, 30);
l2.setBounds(150, 80, 400, 30);
btn1.setBounds(0, 230, 120, 30);
btn2.setBounds(160, 230, 120, 30);
btn3.setBounds(465, 230, 120, 30);
btn4.setBounds(320, 230, 120, 30);
add(l1);
add(l2);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
}
public static void main(String[] args) {
new MenuPanel();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1)
{
}
else if(e.getSource()==btn2)
{
}
else if(e.getSource()==btn3)
{
}
}
I want to navigate to this panel:
public class AddWorkerPanel extends JFrame implements ActionListener {
static Connection con = DatabaseConnection.getConnection();
JLabel l1, l2, l3;
JTextField tf1, tf2;
JButton btn1, btn2;
AddWorkerPanel()
{
setVisible(true);
setSize(600, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Add Worker form in Java");
l1 = new JLabel("Add Worker Form - Type Details Below:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Worker-Name:");
l3 = new JLabel("Worker-Class-Number:");
tf1 = new JTextField();
tf2 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn1.addActionListener(this);
btn2.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
tf1.setBounds(300, 70, 200, 30);
tf2.setBounds(300, 110, 200, 30);
btn1.setBounds(80, 230, 200, 30);
btn2.setBounds(300, 230, 200, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(btn1);
add(btn2);
}
public static void main(String[] args) {
new AddWorkerPanel();
}
}
A JFrame is the actual frame that sits on the screen. So what you want to do is derive your MenuPanel and AddWorkerPanel classes from JPanel, so that they can both sit inside of one frame. Then, in your main frame class, you can have a CardLayout holding both of your panels, and have a button that cycles through the cards on the card layout, using the next() method in the card layout instance that you have stored.
If you just want to close one JFrame and then open the other you should be avble by just simply doing this in the calling method of your button:
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new AddWorkerPanel();
super.dispose();
}
}

Reload the JFrame or JLabel

Im developing an little "clicker" but, if i press the button, and i should get 1+ Score, it dont work! Is there any way to reload or anything else?
Heres my code: (ClickEvent)
public class event implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("PRESS");
game.timesClicked.add(1);
points.setText(game.seePoints);
}
}
And there is my JFrame:
public class game extends JFrame
{
public static JButton buttonStart;
JButton buttonCredits;
JButton buttonBack;
JButton buttonLeave;
public static JFrame panel = new game();
public static ArrayList<Integer> timesClicked = new ArrayList<Integer>();
public static JLabel label1;
public static JLabel points;
public static String seePoints = "Deine Knöpfe: " + timesClicked.size();
public game()
{
setLayout(null);
label1 = new JLabel("ButtonClicker");
points = new JLabel(seePoints);
points.setFont(new Font("Tahoma", Font.BOLD, 15));
points.setBounds(0, 0, 200, 200);
label1.setFont(new Font("Tahoma", Font.BOLD, 50));
label1.setBounds(315, 50, 500, 200);
event e1 = new event();
JButton b = new JButton("KNOPF");
b.setBackground(new Color(96, 140, 247));
b.setForeground(Color.WHITE);
b.setFocusPainted(false);
b.setFont(new Font("Tahoma", Font.BOLD, 15));
b.setBounds( 402, 380, 180, 50 );
b.addActionListener(e1);
add(b);
add(label1);
add(points);
}
}
(Sorry For My Bad English)
public static String seePoints = "Deine Knöpfe: " + timesClicked.size();
This is only being called once at the start of your program. When you add to timesClicked, it does not recalculate seePoints.
You will need to set this variable to the correct value every time you click.

The type TextFieldDemo must implement the inherited abstract method ActionListener?

I almost have this code working. I can launch the GUI, however, when I press any of the buttons, I get the following error message:
The type TextFieldDemo must implement the inherited abstract method ActionListener
I have looked around online and can't see any issues with the code. Can someone help me please?
Thanks :)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TextFieldDemo implements ActionListener{
private JLabel lblName, lblAddress, lblPhone;
private JTextField txtName, txtAddress, txtPhone;
private JButton btnUpper, btnLower, btnExit;
private JPanel panel;
private JFrame frame;
public static void main(String[] args){
new TextFieldDemo();
}
public TextFieldDemo(){
createForm();
addFields();
addButtons();
frame.add(panel);
frame.setVisible(true);
}
public void createForm(){
frame = new JFrame();
frame.setTitle("Student Form");
frame.setSize(400,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(null);
}
public void addFields(){
txtName = new JTextField("Fred Bloggs");
txtName.setBounds(110, 50, 150, 20);
panel.add(txtName);
lblAddress = new JLabel ("Address");
lblAddress.setBounds(10, 70, 100, 20);
panel.add (lblAddress);
txtAddress = new JTextField ("Ashley Road");
txtAddress.setBounds(110, 70, 150, 20);
panel.add (txtAddress);
lblPhone = new JLabel ("Phone Number");
lblPhone.setBounds(10, 90, 100, 20);
panel.add (lblPhone);
txtPhone= new JTextField("01202 191 3333");
txtPhone.setBounds(110, 90, 150, 20);
panel.add (txtPhone);
lblName = new JLabel("Student Name");
lblName.setBounds(10, 50, 100, 20);
panel.add(lblName);
}
public void addButtons(){
btnUpper = new JButton ("UpperCase");
btnUpper.setBounds(50, 200, 100, 20);
btnUpper.addActionListener(this);
panel.add (btnUpper);
btnLower = new JButton ("LowerCase");
btnLower.setBounds(150, 200, 100, 20);
btnLower.addActionListener(this);
panel.add (btnLower);
btnExit = new JButton ("Exit");
btnExit.setBounds(250, 200, 100, 20);
btnExit.addActionListener(this);
panel.add (btnExit);
}
class UpperCaseHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
txtName.setText(txtName.getText().toUpperCase());
txtAddress.setText(txtAddress.getText().toUpperCase());
}
class LowerCaseHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
txtName.setText(txtName.getText().toLowerCase());
txtAddress.setText(txtAddress.getText().toLowerCase());
}
class ExitHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(frame,
"Are you sure you want to exit?",
"Exit?",
JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
System.exit(0);
}
}
}
}
}
}
You say TextFieldDemo implements ActionListener, but it doesn't have an actionPerformed() method, so it's not actually implementing the interface.
Either implement the method or don't claim it implements the interface.
I didn't think it would compile like you have it, but there you go!
The error should make you check the documentation of ActionListener to find out what method(s) is missing. Sometimes eclipse will prompt you to add stubs for the missing methods.
The documentation shows the interface ActionListener has one method to be implemented, actionPerformed();
http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html
Strange, the compiler should throw an error on this.

JFrame - ActionListener

I'm working on building a program that uses JFrame. What I want for my end result, is to implement an ActionListener which will remove labels when the user clicks a button. For example: when the user clicks the JButton, one of 5 labels is removed from the frame. When they click the button again, one of the remaining 4 labels is removed...and so on a so forth, until 0 labels remain. Technically, I have the program working as required however, I'm trying to see if there is a way to implement the ActionListener event via a loop as opposed to listing an if statement for each individual label. Thank you so much!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//calls for public class to inherit features of JFrame within Java
public class NoPurchaseReason extends JFrame implements ActionListener {
private int removeText = 0;
JButton btn = new JButton("Select");
JLabel lbl = new JLabel("Found better price");
JLabel lbl1 = new JLabel("Not as shown on website");
JLabel lbl2 = new JLabel("Wrong product");
JLabel lbl3 = new JLabel("Damaged upon delivery");
JLabel lbl4 = new JLabel("None of the above");
public static void main(String[] args) {
JFrame f = new NoPurchaseReason("Please tell us why you wish to return your purchase.");
f.setBounds(300, 100, 500, 500);
f.setVisible(true);
f.setBackground(Color.blue);
}
public NoPurchaseReason(String title) {
super(title);
setLayout(null);
lbl.setBounds(40, 40, 600, 40);
btn.setBounds(320, 10, 80, 20);
lbl.setBounds(100, 40, 100, 20);
lbl1.setBounds(100, 70, 100, 20);
lbl2.setBounds(100, 100, 150, 20);
lbl3.setBounds(100, 130, 100, 20);
lbl4.setBounds(100, 160, 100, 20);
add(btn);
add(lbl);
add(lbl);
add(lbl1);
add(lbl2);
add(lbl3);
add(lbl4);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
removeText++;
if (removeText == 1) {
lbl.setVisible(false);
lbl1.setBounds(100, 40, 100, 20);
lbl2.setBounds(100, 70, 100, 20);
lbl3.setBounds(100, 100, 150, 20);
lbl4.setBounds(100, 130, 100, 20);
}
if (removeText == 2) {
lbl1.setVisible(false);
lbl2.setBounds(100, 40, 100, 20);
lbl3.setBounds(100, 70, 150, 20);
lbl4.setBounds(100, 100, 100, 20);
}
if (removeText == 3) {
lbl2.setVisible(false);
lbl3.setBounds(100, 40, 150, 20);
lbl4.setBounds(100, 70, 100, 20);
}
if (removeText == 4) {
lbl3.setVisible(false);
lbl4.setBounds(100, 40, 100, 20);
}
if (removeText == 5) {
lbl4.setVisible(false);
}
}
}
Learning how to properly use layout managers will save you a lot of trouble in the long run.
You'll also find that people will tell you to adhere to the single responsibility principle, and avoid making classes that violate this principle (e.g., extending JFrame and implementing ActionListener).
You'll also hear folks tell you to prefer using actions over action listeners (if you need to share functionality across multiple components, that is).
A simple way would be to dedicate an entire panel to holding your labels, and simply remove the first label in the panel until there are no more labels. Here's an example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class LabelDemo
{
public static void main(String[] args) {
String[] labels = {
"Found better price",
"Not as shown on website",
"Wrong product",
"Damaged upon delivery",
"None of the above"
};
final JFrame frame = new JFrame();
final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for (String s: labels) {
panel.add(new JLabel(s));
}
frame.add(panel);
JButton button = new JButton("Select");
button.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent e) {
if (panel.getComponentCount() > 0)
panel.remove(0);
frame.repaint();
}
});
frame.add(button, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
}
Also, you may just have a certain goal in mind that I'm not aware of, but it honestly seems like a list would be better in this case. Here's an example of that as well:
String[] labels = {
"Found better price",
"Not as shown on website",
"Wrong product",
"Damaged upon delivery",
"None of the above"
};
JList<String> list = new JList<>(labels);
int option = JOptionPane.showConfirmDialog(null, list,
"Please tell us why you wish to return your purchase.",
JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
String selectedValue = list.getSelectedValue();
System.out.println(selectedValue); // Do something with it.
}

Categories

Resources