Why wont the submit button work on the GUI - java

I have created an ActionListener and the button but the button doesn't work.
The action listener is supposed to be integrated with the submit button, please help?
Code:
import javax.swing.*;
import javax.swing.event.DocumentListener;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Executer {
private JLabel lblCommand;
private JTextField txtEnter;
private JButton btNext, btPrevious, btSubmit;
private JPanel panel;
public static void main(String[] args) {
new Executer();
}
public Executer() {
JFrame frame = new JFrame("Script Executer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,300);
frame.setVisible(true);
myPanel();
Text();
Fields();
Buttons();
frame.add(panel);
frame.setVisible(true);
}
public void myPanel() {
panel = new JPanel();
panel.setLayout(null);
}
public void Text() {
lblCommand = new JLabel("Enter Here");
lblCommand.setBounds(145, 100, 150, 20);
Font styleOne = new Font("Arial", Font.BOLD, 13);
lblCommand.setFont(styleOne);
panel.add(lblCommand);
}
public void Fields() {
txtEnter = new JTextField();
txtEnter.setBounds(230, 100, 120, 20);
panel.add(txtEnter);
}
public void Buttons() {
btNext = new JButton ("Next");
btNext.setBounds(300,215,100,20);
panel.add(btNext);
btPrevious = new JButton ("Previous");
btPrevious.setBounds(190,215,100,20);
panel.add(btPrevious);
btSubmit = new JButton("Submit");
btSubmit.setBounds(80,215,100,20);
panel.add(btSubmit);
btSubmit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String userEntered = txtEnter.getText();
if(userEntered.equalsIgnoreCase("yes"))
{
//run your script
}
}
});
}
}

Your code appears fine.
Enter a print statement and you can see it is working.
btSubmit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// here the click happend so you can check your Textfield
String userEntered = txtEnter.getText();
System.out.println("User enterd: " + userEntered);
if(userEntered.equalsIgnoreCase("yes"))
{
System.out.println("Entered Yes");
}
}
});

Related

How to click on JLabel, edit with JTextField and exit changing Jlabel?

I am trying to create a GUI and in that GUI I have different JLabels with a value. I want to be able to click on a JLabel to edit it in my JTextfield (only have 1) and after I press enter it should leave Editing the JLabel. At the moment if I try to edit a JLabel it will change but when I click on the next one the old one will also still change.
This is my code:
public class GUI {
JFrame frame;
int n1=1;
int n2=1;
int n3=1;
GUI(){
frame=new JFrame();//creating instance of JFrame
JLabel l1=new JLabel(Integer.toString(n1));
JLabel l2=new JLabel(Integer.toString(n2));
JLabel l3=new JLabel(Integer.toString(n3));
JTextField t=new JTextField();
l1.setBounds(40,50,100, 40);
l2.setBounds(40,100,100, 40);
l3.setBounds(40,150,100, 40);
t.setBounds(20,200,100, 40);
frame.add(l1);
frame.add(l2);
frame.add(l3);
frame.add(t);
l1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
t.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
l1.setText(t.getText());
n1=parseInt(t.getText());
}
});
}
});
l2.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
t.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
l2.setText(t.getText());
n2=parseInt(t.getText());
}
});
}
});
frame.setSize(400,500);//400 width and 500 height
frame.setLayout(null);//using no layout managers
frame.setVisible(true);//making the frame visible
}
public static void main(String[] args) {
new GUI();
}
}
Thanks in advance.
Don't add action listeners for each click. Clicking on a label should record the state of your UI -- that that label is now being edited, and set up the value in the JTextField. Then enter should transfer the value to the JLabel which was recorded as selected.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GUI {
JLabel currentEditLabel = null;
JFrame frame;
int n1 = 1;
int n2 = 1;
int n3 = 1;
GUI() {
frame = new JFrame();//creating instance of JFrame
JLabel l1 = new JLabel(Integer.toString(n1));
JLabel l2 = new JLabel(Integer.toString(n2));
JLabel l3 = new JLabel(Integer.toString(n3));
JTextField t = new JTextField();
l1.setBounds(40, 50, 100, 40);
l2.setBounds(40, 100, 100, 40);
l3.setBounds(40, 150, 100, 40);
t.setBounds(20, 200, 100, 40);
frame.add(l1);
frame.add(l2);
frame.add(l3);
frame.add(t);
t.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (currentEditLabel != null) {
currentEditLabel.setText(t.getText());
currentEditLabel = null;
}
}
});
addMouseListener(l1, t);
addMouseListener(l2, t);
addMouseListener(l3, t);
frame.setSize(400, 500);//400 width and 500 height
frame.setLayout(null);//using no layout managers
frame.setVisible(true);//making the frame visible
}
private void addMouseListener(JLabel label, JTextField t) {
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
currentEditLabel = (JLabel) e.getComponent();
t.setText(currentEditLabel.getText());
}
});
}
public static void main(String[] args) {
new GUI();
}
}
Figuring out how to set n1, n2, ... is left as an exercise, as is how to indicate in the UI that no label is selected (hint: should you allow input in the JTextField when no label has been selected?)
As #kleopatra says, using no layout manager is not good practice, as if your panel is resized (perhaps your program will be run on a mobile device, for instance) your components may become hidden, See this discussion.
Here's your code using GridLayout, a simple layout manager.
Points to note:
I've removed the absolute positioning and sizing of the components and the frame.
The frame is now resizable, so you can see what the layout manager does as the size changes.
The JFrame is packed before displaying it.
To get a layout which does exactly what you want you can look at GridBagLayout, and also think about nesting containers with simple layout managers.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GUI {
JLabel currentEditLabel = null;
JFrame frame;
int n1 = 1;
int n2 = 1;
int n3 = 1;
GUI() {
frame = new JFrame();
JLabel l1 = new JLabel(Integer.toString(n1));
JLabel l2 = new JLabel(Integer.toString(n2));
JLabel l3 = new JLabel(Integer.toString(n3));
JTextField t = new JTextField();
GridLayout layout = new GridLayout(4, 1, 10, 10);
frame.setLayout(layout);
frame.add(l1);
frame.add(l2);
frame.add(l3);
frame.add(t);
frame.setResizable(true);
t.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (currentEditLabel != null) {
currentEditLabel.setText(t.getText());
currentEditLabel = null;
}
}
});
addMouseListener(l1, t);
addMouseListener(l2, t);
addMouseListener(l3, t);
frame.pack();
frame.setVisible(true);//making the frame visible
}
private void addMouseListener(JLabel label, JTextField t) {
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
currentEditLabel = (JLabel) e.getComponent();
t.setText(currentEditLabel.getText());
}
});
}
public static void main(String[] args) {
new GUI();
}
}

JFrame suddenly freaks out

i'm making a tiny program where you have 3 timers and when you press start it gives a progress bar until time is over, but JFrame decided to do this after I pressed start:
Here is my code:
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
static int getTime(JFrame frame){
String ts = JOptionPane.showInputDialog(frame, "Set time: ", "time", 1);
return Integer.parseInt(ts);
}
public static class globalVars{
public static int time1 = 5;
public static int time2 = 5;
public static int time3 = 5;
}
static void startTimer(JFrame frame) {
int time = globalVars.time1+globalVars.time2+globalVars.time3;
int timeF = Math.toIntExact(System.currentTimeMillis() / 1000l);
JProgressBar bar = new JProgressBar(0, time);
frame.add(bar);
frame.pack();
int i=0;
while (System.currentTimeMillis()!=timeF+time* 60L){
bar.setValue((timeF+time*60-Math.toIntExact(System.currentTimeMillis() / 1000l))/60);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Timer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
//-----------------------------------------------------------//
Icon icon1 = new ImageIcon("placeholder.png");
String button1Text = "Button1, ";
JButton button1 = new JButton(String.format("%s%dmin", button1Text, globalVars.time1));
button1.setIcon(icon1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
globalVars.time1=getTime(frame);
button1.setText(String.format("%s%dmin", button1Text, globalVars.time1));
}
});
frame.add(button1);
//-----------------------------------------------------------//
Icon icon2 = new ImageIcon("placeholder.png");
String button2Text = "Button2, ";
JButton button2 = new JButton(String.format("%s%dmin", button2Text, globalVars.time2));
button2.setIcon(icon2);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
globalVars.time2=getTime(frame);
button2.setText(String.format("%s%dmin", button2Text, globalVars.time2));
}
});
frame.add(button2);
//-----------------------------------------------------------//
Icon icon3 = new ImageIcon("placeholder.png");
String button3Text = "Button3, ";
JButton button3 = new JButton(String.format("%s%dmin", button3Text, globalVars.time3));
button3.setIcon(icon3);
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
globalVars.time3=getTime(frame);
button3.setText(String.format("%s%dmin", button3Text, globalVars.time3));
}
});
frame.add(button3);
//-----------------------------------------------------------//
JButton buttonStart = new JButton("Start");
buttonStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonStart.removeActionListener(this);
startTimer(frame);
}
});
frame.add(buttonStart);
//-----------------------------------------------------------//
frame.pack();
frame.setSize(400, 200);
frame.setVisible(true);
}
}
I know it is a very inefficient way of doing things and I will improve it but now I'm more worried about the black screen. Tnx!

This code shows focus gained before the button is clicked

I want the GUI to show "focus gained" when the user clicks on the button and
"focus lost" when clicking anywhere else. But it always shows "focus gained".
Main Code:
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*;
public class focuslistener extends JFrame {
private Container c;
private JButton jb1;
private Font f;
private JTextArea ta1;
focuslistener() {
inticompo();
}
public void inticompo() {
c = this.getContentPane();
c.setBackground(Color.yellow);
c.setLayout(null);
f = new Font("Arial", Font.ITALIC + Font.BOLD, 18);
jb1 = new JButton("Clicked");
jb1.setBounds(50, 10, 250, 80);
jb1.setForeground(Color.red);
jb1.setBackground(Color.blue);
jb1.setFont(f);
c.add(jb1);
ta1 = new JTextArea();
ta1.setFont(f);
ta1.setBounds(10, 110, 400, 300);
ta1.setForeground(Color.black);
ta1.setBackground(Color.red);
c.add(ta1);
jb1.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent fe) {
ta1.setText("focus Gained");
}
#Override
public void focusLost(FocusEvent fe) {
ta1.setText("focus Lost");
}
});
}
public static void main(String[] args) {
focuslistener a = new focuslistener();
a.setVisible(true);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setBounds(50, 100, 500, 500);
a.setTitle("hello");
}
}

how can i use a Jbutton in a class to trigger a panelSlider effect which is in a button actionPerformed in another class

how can i use a Jbutton in a class to trigger a panelSlider effect which is in a button actionPerformed in another class
Find the code here
public class programDisplay extends javax.swing.JPanel { /** * Creates new form programDisplay */
public programDisplay() {
initComponents();
}
here is an example for sliding effect of panel. you can take it as a reference and implement your requirement.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class SlidingPanel {
JPanel panel;
public void makeUI() {
panel = new JPanel();
panel.setBackground(Color.RED);
panel.setBounds(0, 0, 400, 400);
JButton button = new JButton("Click");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
((JButton) e.getSource()).setEnabled(false);
new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setLocation(panel.getX() - 1, 0);
if (panel.getX() + panel.getWidth() == 0) {
((Timer) e.getSource()).stop();
System.out.println("Timer stopped");
}
}
}).start();
}
});
panel.add(button);
JFrame frame = new JFrame("Sliding Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SlidingPanel().makeUI();
}
});
}
}
UPDATE :
in HomePane.java
rightPane rPane = new rightPane();
JPanel lPane = new leftPane(rPane);
in leftPane.java
private rightPane rPane;
public leftPane(final rightPane rPane)
{
pane = new JPanel();
this.rPane = rPane;
staffLogin = new JButton("STAFF LOGIN");
adminLogin = new JButton("ADMIN LOGIN");
guestLogin = new JButton("GUEST LOGIN");
staffLogin.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e)
{
rPane.trigerEvent();
}
});
this.setLayout(null);
this.add(staffLogin);
this.add(adminLogin);
this.add(guestLogin);
staffLogin.setFont(new Font("Serif", Font.BOLD, 20));
adminLogin.setFont(new Font("Serif", Font.BOLD, 20));
guestLogin.setFont(new Font("Serif", Font.BOLD, 20));
staffLogin.setBounds(20, 10, 200, 50);
adminLogin.setBounds(20, 75, 200, 50);
guestLogin.setBounds(20, 140, 200, 50);
this.setBorder(BorderFactory.createLineBorder(Color.BLUE));
pane.setBorder(BorderFactory.createLineBorder(Color.red));
}

I thought I had the program ready to run but when I run it nothing happens

So I thought I had this code being able to work but it is not working.
I do not know what to do and I have tried looking at everything everyone has suggested but I just do not know what to do so any help would be greatly appreciated!
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class memory extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(156, 93, 82));
g.fill3DRect(21,3,7,12, true);
g.setColor(new Color(156,23,134));
g.fillOval(1,15,15,15);
g.fillOval(16,15,15,15);
g.fillOval(31,15,15,15);
g.fillOval(7,31,15,15);
g.fillOval(22,31,15,15);
g.fillOval(16,47,15,15);
setVisible(false);}
public memory()
{
GridLayout h =new GridLayout(3,3);
final JFrame frame = new JFrame();
final JPanel pan = new JPanel(h);
frame.add(pan);
pan.setBackground(new Color(130,224,190));
pan.setFont(new Font("Serif", Font.BOLD, 28));
JButton button1= new JButton();
pan.add(button1);
final JLabel label1= new JLabel("hi");
label1.setVisible(false);
pan.add(label1);
JButton button2= new JButton();
pan.add(button2);
final JLabel label2= new JLabel("hi");
label2.setVisible(false);
pan.add(label2);
JButton button3= new JButton();
pan.add(button3);
final JLabel label3 = new JLabel("hi");
label3.setVisible(false);
pan.add(label3);
JButton button4 = new JButton();
pan.add(button4);
final JLabel label4 = new JLabel("hi");
label4.setVisible(false);
pan.add(label4);
JButton button5= new JButton();
pan.add(button5);
final JLabel label5= new JLabel("hi");
label5.setVisible(false);
pan.add(label5);
JButton button6= new JButton();
pan.add(button6);
final JLabel label6= new JLabel("hi");
label6.setVisible(false);
pan.add(label6);
JButton button7= new JButton();
pan.add(button7);
final JLabel label7= new JLabel("hi");
label7.setVisible(false);
pan.add(label7);
JButton button8= new JButton();
pan.add(button8);
final JLabel label8= new JLabel("hi");
label8.setVisible(false);
pan.add(label8);
JButton button9= new JButton();
pan.add(button9);
final JButton button10= new JButton("Exit");
pan.add(button10);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Memory Game");
frame.setSize(500,500);
frame.setVisible(true);
setLayout(new BorderLayout());
add(pan,BorderLayout.CENTER);
add(button10, BorderLayout.SOUTH);
setSize(600,600);
setVisible(true);
final JLabel label9= new JLabel("hi");
label9.setVisible(false);
pan.add(label9);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label1.setVisible(true);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label2.setVisible(true);
}
});
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label3.setVisible(true);
}
});
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label4.setVisible(true);
}
});
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label5.setVisible(true);
}
});
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label6.setVisible(true);
}
});
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label7.setVisible(true);
}
});
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label8.setVisible(true);
frame.getContentPane().add(new memory());
setVisible(true);
}});
;
button9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label9.setVisible(true);}}
);
button10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (button10.getSize() != null) {
System.exit(0);}}
});};
public static void main(String args[])
{
new memory();
};
}
As I said in your last question, you need to add your panel to an instance of a JFrame...
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new memory());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
Take some time and have a read through Creating a GUI With JFC/Swing
You'll also want to remove
final JFrame frame = new JFrame();
final JPanel pan = new JPanel(h);
frame.add(pan);
From the constructor and simple add your components directly to the (memory) panel
You'll also need to remove setVisible(false); from your paintComponent method ... which explains why you're having so many problems...
You must have a JFrame and show it for a swing application. I dont see anything like that.
Your main should be like this:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new memory());
frame.setSize(500, 400);
frame.setVisible(true);
}
});
}

Categories