Java: Button isn't showing up - java

So, i'm using Java and JFrame and i would like the end user to be able to click the button "Login" and it should display another saying "Edit". For some odd reason when the end-user clicks "Login" it doesn't show the "Edit" box but it displays the "System.out.println"
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
public void fixtureList()
{
JButton editButton;
editButton = new JButton("Edit");
editButton.setBounds(100, 200, 100, 100);
add(editButton);
}
public void loginPanel()
{
setLayout(null);
JButton loginButton;
loginButton = new JButton("Login");
loginButton.setBounds(10, 10, 100, 100);
add(loginButton);
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
fixtureList();
System.out.println("Loading the fixtures screen");
}
});
}
public static void main(String[] args)
{
Main window = new Main();
window.setTitle("PE Fixtures v1.0");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(250, 430);
window.loginPanel();
window.getContentPane().setBackground(new Color(53, 56, 64));
window.setVisible(true);
}
}

Call repaint in the ActionListener
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
fixtureList();
System.out.println("Loading the fixtures screen");
repaint();
}

Related

Getting user input and then printing new text using AWT

I am trying to create a simple program where when I press a button, new text will appear but I have no idea how to do it (I imagine it is very simple).
The code I have right now is:
import java.awt.*;
public class ConsumptionGUI extends Frame
{
public ConsumptionGUI()
{
Frame fr = new Frame();
Button b1 = new Button ("Terminate Program");
Button b2 = new Button ("Start");
b1.setBounds(50,50,50,50);
b2.setBounds(50,50,50,50);
b1.addActionListener(e-> System.exit(0));
Label txt = new Label ("This is my first GUI");
//add to frame (after all buttons and text was added)
fr.add(b2);
fr.add(txt);
fr.add(b1);
fr.setSize(500,300);
fr.setTitle("Vehicles Information System");
fr.setLayout(new FlowLayout());
fr.setVisible(true);
} //end constructor
public static void main(String args[]){
ConsumptionGUI frame1= new ConsumptionGUI();
} //end main
Basically after this point I managed to create a frame with 2 buttons and some text in the middle.
I am really struggling to continue from here.
I need the program to first start by the press of a button then print some new text (something like "please enter your car's speed") and then save this information (to be used in a simple formula).
Afterwards the program needs to display the formula used and print what is the value calculated.
Can anyone please help?
Thanks
To get user input, you can implement a Dialog like in below code. You can use another similar dialog to show the formula and result as well.
import java.awt.*;
import java.awt.event.*;
public class ConsumptionGUI extends Frame
{
public ConsumptionGUI()
{
Frame fr = new Frame();
Button b1 = new Button("Terminate Program");
Button b2 = new Button("Start");
//b1.setBounds(50, 50, 50, 50); // Unnecessary
//b2.setBounds(50, 50, 50, 50); // Unnecessary
b1.addActionListener(e -> System.exit(0));
b2.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
InputDialog dialog = new InputDialog(fr);
dialog.setVisible(true);
System.out.println("User inputted speed = " + dialog.getSpeed());
}
});
Label txt = new Label("This is my first GUI");
//add to frame (after all buttons and text was added)
fr.add(b2);
fr.add(txt);
fr.add(b1);
fr.setSize(500, 300);
fr.setTitle("Vehicles Information System");
fr.setLayout(new FlowLayout());
fr.setVisible(true);
} //end constructor
public static void main(String args[])
{
ConsumptionGUI frame1 = new ConsumptionGUI();
} //end main
}
class InputDialog extends Dialog
{
private int speed;
InputDialog(Frame owner)
{
super(owner, "Input", true);
addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing(WindowEvent e)
{
dispose();
}
});
TextField textField = new TextField(20);
Button okButton = new Button("OK");
okButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
String speedString = textField.getText();
speed = !speedString.isEmpty() ? Integer.parseInt(speedString) : 0;
dispose();
}
});
setLayout(new GridLayout(3, 1));
add(new Label("Please enter your car's speed"));
add(textField);
add(okButton);
pack();
}
int getSpeed()
{
return speed;
}
}

I can't change the color of a JButton when pressed

I have all the imports needed and there are no errors but it won't work.
final JButton button_32 = new JButton("2");
button_32.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button_32.setBackground(Color.red);
}
});
button_32.setBounds(0, 57, 33, 29);
contentPane.add(button_32);
You can create your own Button, which extends ButtonModel or just do it, as suggested here.
public class Main {
static JFrame frame;
public static void main(String[] args)
{
// schedule this for the event dispatch thread (edt)
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
displayJFrame();
}
});
}
static void displayJFrame()
{
frame = new JFrame("Our JButton listener example");
// create our jbutton
final JButton showDialogButton = new JButton("Click Me");
// add the listener to the jbutton to handle the "pressed" event
showDialogButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// when the button is pressed
showDialogButton.setBackground(Color.RED);
}
});
// put the button on the frame
frame.getContentPane().setLayout(new FlowLayout());
frame.add(showDialogButton);
// set up the jframe, then display it
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(300, 200));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
I think it can be related to "implementation of the abstract class".
Try this:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ExamButton extends JFrame {
JButton button_32 = new JButton("ssf");
JFrame frame = new JFrame();
public ExamButton() {
final JButton button_32 = new JButton("2");
button_32.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
button_32.setBackground(Color.red);
}
});
button_32.setBounds(0, 57, 33, 29);
add(button_32, BorderLayout.CENTER);
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
new ExamButton();
}
}

How do i add an action to a JFrame JButton?

I have a JFrame gui im making and I need help.
Im trying to find out how to add a action to my button.
as in using it in a "if" statement or make i print something out when you push it.
thanks!
code:
package agui;
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
JButton button = new JButton("click me");
JPanel panel = new JPanel();
panel.add(button);
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
You want the "addActionListener" method, something like:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked the button");
}
});

Modal Window on top of Modal Window in java swing?

I have the following problem:
I have a main application window (JFrame) that fills the whole screen.
When a button is clicked, a smaller window appears, to let the user input some Data. While the User does this, the main window should neither jump in front of it, nor allow interaction.
Solution to that: open a modal JDialog. Lets call that Dialog 1.
But when a button within this Dialog is clicked, a NEW window (yes/no-dialog) is supposed to pop up.. and, again, needs to act modal on the already modal JDialog Dialog 1. Trying to do that, the second Dialog keeps disappearing behind the first one.
I tried making Dialog 1 a JFrame but then, of course, I loose the "modal" bit. Forcing Dialog 1 to stay in from still keeps the main window's Button clickable.
What am I missing? How can I put a modal swing window OVER another modal swing window?
Edit:
minimal example of one not-really working version:
Main class for opening:
public class MainWin extends JFrame {
public MainWin(){
this.setSize(800,800);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Dia1(MainWin.this);
}
});
this.setVisible(true);
}
}
Main window:
public class MainWin extends JFrame {
public MainWin(){
this.setSize(800,800);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Dia1(MainWin.this);
}
});
this.setVisible(true);
}
}
First Dialog:
public class Dia1 extends JDialog {
public Dia1(final JFrame parent){
super(parent, true);
this.setSize(400, 400);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
this.setVisible(true);
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Dia2(parent);
}
});
}
}
Second Dialog:
public class Dia2 extends JDialog {
public Dia2(JFrame parent){
super(parent, true);
this.setSize(200, 200);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
this.setVisible(true);
}
}
PS: I just realised: Dialog 2 is not hidden, as I suspected.. it is simply not there. Most likely because the parent window is blocked from the Modal Dialog?
Here is an MCVE of modality working as advertised.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ModalOverModal {
private JComponent ui = null;
ModalOverModal() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new GridLayout());
ui.setBorder(new EmptyBorder(40,40,40,40));
final JButton b1 = new JButton("Open Modal Dialog");
b1.setMargin(new Insets(40, 200, 40, 200));
ui.add(b1);
final JButton b2 = new JButton("Open 2nd Modal Dialog");
ActionListener al1 = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(b1, b2);
}
};
b1.addActionListener(al1);
ActionListener al2 = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(b2, "Close Me!");
}
};
b2.addActionListener(al2);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
ModalOverModal o = new ModalOverModal();
JFrame f = new JFrame("Modal over Modal");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

Java: Clear JFrame and load new buttons

So, i have my main screen showing and i would like it so that when the user clicks "Login" is loads another screen. I have my second screen in another function called "fixtureLists". When i call this function it just overlays the buttons ontop of the login screen. How would i get it so that the screen is cleared and then the fixtureLists screen loads?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Main extends JFrame
{
public Main()
{
loginButton = new JButton("Login");
loginButton.setBounds( 125, 300, 100, 35);
add(loginButton);
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
fixtureList();
System.out.println("Loading the fixtures screen");
}
});
}
public void fixtureList()
{
JButton editButton;
JButton createButton;
JCheckBox chkBox;
setLayout(null);
editButton = new JButton("Edit");
editButton.setBounds( 10, 10, 100, 35);
add(editButton);
createButton = new JButton("Create");
createButton.setBounds( 140, 10, 100, 35);
add(createButton);
editButton = new JButton("Edit");
createButton.setBounds( 10, 30, 100, 35);
}
public static void main(String args[])
{
Main window = new Main();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(250, 430);
window.getContentPane().setBackground(new Color(53, 56, 64));
window.setVisible(true);
window.setTitle("PE Timetable v1.0");
}
}
I would not define the login dialog in the main frame.
Your login dialog should pop up in a dialog over the frame.
Having said that, if you want the login on the same frame, then use Cardlayout. On one Cardlayout have your login stuff, then when you login, switch the cardlayout to another panel.

Categories

Resources