I've been making an app with java swing and JavaFX and didn't run into a problem until now. Whenever I set the guis visibility to true, the GUI becomes very small, when it should be 1150 px wide, 900 px tall. Does anyone get any ideas?
Main Gui code:
import javax.swing.*;
import java.awt.*;
public class mainGui extends JFrame{
public static JFrame mainScreen = new JFrame("Tamo");
public mainGui() {
JPanel topPanel = new JPanel();
mainScreen.setSize(1150, 900);
mainScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainScreen.setResizable(false);
mainScreen.setLayout(new BorderLayout());
topPanel.setBackground(new Color(226, 230, 204));
topPanel.setPreferredSize(new Dimension(1150, 100));
mainScreen.add(topPanel, BorderLayout.NORTH);
ImageIcon icon = new ImageIcon("C:\\Users\\uname\\Desktop\\TamoDienynas\\pngs\\tamo.png");
mainScreen.setIconImage(icon.getImage());
mainScreen.setVisible(false);
}
public static void main(String[] args) {
new mainGui();
}
}
Class which make's the gui visible:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static me.rockorbonk.tamodienynas.GUI.mainGui.mainScreen;
public class loginGui extends JFrame implements ActionListener {
private static JFrame frame;
private static JLabel userLabel;
private static JLabel passwordLabel;
private static JLabel success;
private static JButton login;
private static JTextField userText;
private static JPasswordField passwordText;
loginGui() {
frame = new JFrame();
JPanel panel = new JPanel();
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel);
panel.setLayout(null);
userLabel = new JLabel("User");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
userText = new JTextField();
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
passwordText = new JPasswordField();
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);
login = new JButton("Login");
login.setBounds(10, 80, 80, 25);
login.addActionListener(this);
panel.add(login);
success = new JLabel("");
success.setBounds(10, 110, 300, 25);
panel.add(success);
ImageIcon icon = new ImageIcon("C:\\Users\\uname\\Desktop\\TamoDienynas\\pngs\\tamo.png");
frame.setIconImage(icon.getImage());
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent ae) {
String uname = userText.getText();
String pwd = passwordText.getText();
if(uname.equals("rokasbruz") && pwd.equals("Rokas123")) {
success.setText("Sekmingai prisijungėte!");
ActionListener l = evt -> {
userLabel.setVisible(false);
passwordLabel.setVisible(false);
userText.setVisible(false);
passwordText.setVisible(false);
login.setVisible(false);
success.setVisible(false);
frame.setVisible(false);
mainScreen.setVisible(true);
mainScreen.pack();
};
Timer timer = new Timer(2000, l);
timer.setRepeats(false);
timer.start();
}
else{
success.setText("Slapyvardis arba slaptažodis nesutampa!");
}
}
public static void main(String[] args) {
new loginGui();
}
}
Any help would be appreciated!
P.S. I'm using IntelliJ ultimate as the IDE
-Rock
mainScreen is just a JFrame that does not contain anything. Hence when you make it visible, all you see is the title bar. In order to initialize mainScreen you need to call mainGui constructor. Nonetheless, as Andrew Thompson wrote in his comment, your style of coding a Swing application is very unusual. Consider using a dialog as your login window.
Here is your code that displays mainScreen as I believe you intended. I only changed the ActionListener, in class loginGui, that displays mainGui. I added one line which is indicated with the comment ADDED THIS LINE
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.Timer;
import static me.rockorbonk.tamodienynas.GUI.mainGui.mainScreen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class loginGui extends JFrame implements ActionListener {
private static JFrame frame;
private static JLabel userLabel;
private static JLabel passwordLabel;
private static JLabel success;
private static JButton login;
private static JTextField userText;
private static JPasswordField passwordText;
loginGui() {
frame = new JFrame();
JPanel panel = new JPanel();
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel);
panel.setLayout(null);
userLabel = new JLabel("User");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
userText = new JTextField();
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
passwordText = new JPasswordField();
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);
login = new JButton("Login");
login.setBounds(10, 80, 80, 25);
login.addActionListener(this);
panel.add(login);
success = new JLabel("");
success.setBounds(10, 110, 300, 25);
panel.add(success);
ImageIcon icon = new ImageIcon("C:\\Users\\uname\\Desktop\\TamoDienynas\\pngs\\tamo.png");
frame.setIconImage(icon.getImage());
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent ae) {
String uname = userText.getText();
String pwd = passwordText.getText();
if (uname.equals("rokasbruz") && pwd.equals("Rokas123")) {
success.setText("Sekmingai prisijungėte!");
ActionListener l = evt -> {
userLabel.setVisible(false);
passwordLabel.setVisible(false);
userText.setVisible(false);
passwordText.setVisible(false);
login.setVisible(false);
success.setVisible(false);
frame.setVisible(false);
new mainGui(); // ADDED THIS LINE
mainScreen.setVisible(true);
mainScreen.pack();
};
Timer timer = new Timer(2000, l);
timer.setRepeats(false);
timer.start();
}
else {
success.setText("Slapyvardis arba slaptažodis nesutampa!");
}
}
public static void main(String[] args) {
new loginGui();
}
}
Here is how it appears (after logging in).
Related
I'm trying to make a banking system GUI and I've done well so far but I've run into quite the roadblock. I want to be able to log in, but only if you've previously created an account, but I'm not sure if it's even possible to run my create account class and save the information received for future use since the program will close. I assume it's not possible as I'd need a place to store the username and password for future use, such as a server or something, but I thought I'd ask to see if maybe there was something I could do with an array or something. (P.S. I know I shouldn't use .setLayout(null) but this is just a fun first solo coding project I'm trying to do and I don't understand what people say about it so I'll learn about it later)
Here is my code so far:
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class CreateAccount{
private JPanel panel;
private JFrame frame;
private JLabel username;
private JLabel title;
private JLabel passwordLabel;
private JLabel confirmPasswordLabel;
private JLabel alreadyLoggedIn;
private JTextField usernameText;
private JPasswordField passwordText;
private JPasswordField confirmPasswordText;
private JButton confirmButton;
private JButton alreadyLoggedInButton;
private JLabel failureLabel;
public CreateAccount()
{
panel = new JPanel();
frame = new JFrame();
frame.setSize(500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(panel);
panel.setLayout(null);
Clicklistener click = new Clicklistener();
title = new JLabel("Create New Account");
title.setFont(new Font("Serif", Font.BOLD, 18));
title.setBounds(150,10,200, 25);
panel.add(title);
username = new JLabel("New Username:");
username.setBounds(40,50,100,25);;
username.setFont(new Font("Serif", Font.BOLD, 14));
panel.add(username);
passwordLabel = new JLabel("Create Password:");
passwordLabel.setBounds(30,90,150,25);
passwordLabel.setFont(new Font("Serif", Font.BOLD, 14));
panel.add(passwordLabel);
confirmPasswordLabel = new JLabel("Confirm Password:");
confirmPasswordLabel.setBounds(18,130,150,25);
confirmPasswordLabel.setFont(new Font("Serif", Font.BOLD, 14));
panel.add(confirmPasswordLabel);
alreadyLoggedIn = new JLabel("Already logged in?");
alreadyLoggedIn.setBounds(28,225,200,25);
panel.add(alreadyLoggedIn);
usernameText = new JTextField();
usernameText.setBounds(160,53,225,25);
panel.add(usernameText);
passwordText = new JPasswordField();
passwordText.setBounds(160,93,225,25);
panel.add(passwordText);
confirmPasswordText = new JPasswordField();
confirmPasswordText.setBounds(160,133,225,25);
panel.add(confirmPasswordText);
confirmButton = new JButton("Confirm Login");
confirmButton.setBounds(205,190,125,25);
confirmButton.addActionListener(click);
panel.add(confirmButton);
alreadyLoggedInButton = new JButton("Login Here");
alreadyLoggedInButton.setBounds(140,225, 125,25);
alreadyLoggedInButton.addActionListener(click);
panel.add(alreadyLoggedInButton);
failureLabel = new JLabel("");
failureLabel.setBounds(175,160,400,25);
panel.add(failureLabel);
frame.setVisible(true);
}
public String getUsername() {
return usernameText.getText();
}
public String getPassword() {
return passwordText.getText();
}
private class Clicklistener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String password = passwordText.getText();
String confirmedPassword = confirmPasswordText.getText();
if ((password.equals(confirmedPassword)) && (e.getSource() == confirmButton))
{
frame.dispose();
//call Home Page
}
else if(!password.equals(confirmedPassword)) {
failureLabel.setText("Passwords don't match. Try Again!");
}
if (e.getSource() == alreadyLoggedInButton)
{
frame.dispose();
new Login();
}
}
}
}
My Login class code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
private JFrame frame;
private JPanel panel;
private JLabel userLabel;
private JTextField userText;
private JLabel passwordLabel;
private JPasswordField passwordText;
private JButton button;
private JLabel success;
public Login(){
panel = new JPanel();
frame = new JFrame();
frame.setSize(350,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null);
userLabel = new JLabel("Username:");
userLabel.setBounds(10, 20, 80, 25); // x , y , width , height
panel.add(userLabel);
userText = new JTextField();
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
passwordText = new JPasswordField();
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
button = new JButton("Login");
button.setBounds(135,80,80,25);
button.addActionListener(new Clicklistener());
panel.add(button);
success = new JLabel("");
success.setBounds(10,110,300,25);
panel.add(success);
success.setText("");
frame.setVisible(true);
}
private class Clicklistener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if ((e.getSource() == button))
{
frame.dispose();
}
}
}
}
I'm trying to create a login form and I have some problems at getting the showPassword checkBox work. When showPassword is selected I want the content of the JPasswordField, passwordField, to be visible and when showPassword is not selected I want it to be "hide". I don't understant why my code doesn't work. I write the code this way because I want to implement it in the future as a Model View Controller. I'd prefer not to change any attribute from private in public if possible. Any ideas why this doesn't work? Thanks!
package project3;
import javax.swing.JFrame;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.*;
#SuppressWarnings("serial")
public class LogInWindow extends JFrame {
private Container container = getContentPane();
private JLabel titleLabel = new JLabel("WarehouseApp");
private JLabel userLabel = new JLabel("USERNAME");
private JLabel passwordLabel = new JLabel("PASSWORD");
private JTextField userTextField = new JTextField();
private JPasswordField passwordField = new JPasswordField();
private JButton loginButton = new JButton("LOGIN");
private JCheckBox showPassword = new JCheckBox("Show Password");
private JLabel logInAsLabel = new JLabel("LOGIN AS");
private JComboBox<String> logInAsComboBox = new JComboBox<String>();
public LogInWindow() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(10, 10, 370, 370);
this.setName("Login Form");
this.setResizable(false);
this.setLocationRelativeTo(null);
container.setLayout(null);
titleLabel.setBounds(80, -10, 200, 100);
userLabel.setBounds(50, 80, 100, 30);
userTextField.setBounds(150, 80, 150, 30);
passwordLabel.setBounds(50, 130, 100, 30);
passwordField.setBounds(150, 130, 150, 30);
logInAsLabel.setBounds(50, 180, 100, 30);
logInAsComboBox.setBounds(150, 180, 150, 30);
showPassword.setBounds(150, 220, 150, 30);
loginButton.setBounds(150, 260, 100, 30);
Font font = new Font("Times New Roman", Font.BOLD, 30);
titleLabel.setFont(font);
logInAsComboBox.addItem("ADMIN");
logInAsComboBox.addItem("CLIENT");
logInAsComboBox.setSelectedIndex(-1);
container.add(titleLabel);
container.add(userLabel);
container.add(passwordLabel);
container.add(userTextField);
container.add(passwordField);
container.add(logInAsLabel);
container.add(logInAsComboBox);
container.add(showPassword);
container.add(loginButton);
}
public void showPasswordWhenClicked(ActionListener listenForPassword) {
showPassword.addActionListener(listenForPassword);
}
public boolean getPasswordStatus() {
if (showPassword.isSelected()==true)
return true;
return false;
}
public void setPasswordVisible() {
passwordField.setEchoChar((char) 0);
}
public void setPasswordInvisible() {
passwordField.setEchoChar('*');
}
}
package project3;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Controller {
private LogInWindow theView;
public Controller(LogInWindow theView) {
this.theView = theView;
this.theView.showPasswordWhenClicked(new showPasswordListener());
}
public class showPasswordListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if (theView.getPasswordStatus()==true) {
theView.setPasswordVisible();
} else {
theView.setPasswordInvisible();
}
}
}
public static void main(String[] args) {
LogInWindow logIn = new LogInWindow();
logIn.setVisible(true);
}
}
Your code does not create an instance of Controller, so that class’s constructor is never called. Therefore, showPasswordWhenClicked is never called.
Try adding this line to your main method:
new Controller(logIn);
I am currently designing a login screen, but I ran into a strange issue. I already designed my GUI with the help of swing, and it was time to make functional buttons. I wanted to test my login button and if it would take me to the frame I want, but it is unable to. I can set a JOptionPane.showMessageDialog for example, which works just fine, but I am unable to open another frame from the button. I tried with New JFrameName().setVisible(true), and also JFrameName test = new JFrameName(); test.setVisible(true);, but the methods show up in red. Here is my code.
package com.edu4java.swing.tutrial3;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginView {
public static void main(String[] args) {
JFrame frame = new JFrame("Bus Tour Booking System");
frame.setSize(300, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel titleLabel = new JLabel("Bus Tour Booking System");
titleLabel.setBounds(70,15,150,25);
panel.add(titleLabel);
JLabel userLabel = new JLabel("Username: ");
userLabel.setBounds(30, 50, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(120, 50, 130, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setBounds(30, 80, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(120, 80, 130, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(100, 125, 80, 25);
panel.add(loginButton);
ActionListener myButtonListener = new MyButtonListener();
loginButton.addActionListener(myButtonListener);
}
private static class MyButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
//can't access a new frame from here :(
}
}
}
I would be very grateful if someone could help me out, I read a lot on Stackoverflow and Reddit, but just can't find the solution. I am also new to Java, so that doesn't help a lot either :D. Thanks in advance!
P.S. As far as the actual functionality for the login screen, I am going to do that in a later stage.
This is your login class. I put the JFrame frame in the global scope, so you can manipulate it from the ButtonListener method. I also created a SomeFrame class, just to demonstrate the new JFrame that would be created when you click the button. When an action is performed(the button is clicked) a new object of SomeFrame is created. Since SomeFrame extends JFrame we can use the method setVisible() to a SomeFrame object. The SomeFrame frame appears and the LoginView is no longer visible.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginView {
public static JFrame frame = new JFrame("Bus Tour Booking System");
public static void main(String[] args) {
frame.setSize(300, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel titleLabel = new JLabel("Bus Tour Booking System");
titleLabel.setBounds(70,15,150,25);
panel.add(titleLabel);
JLabel userLabel = new JLabel("Username: ");
userLabel.setBounds(30, 50, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(120, 50, 130, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setBounds(30, 80, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(120, 80, 130, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(100, 125, 80, 25);
panel.add(loginButton);
ActionListener myButtonListener = new MyButtonListener();
loginButton.addActionListener(myButtonListener);
}
private static class MyButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
SomeFrame newFrame = new SomeFrame();
newFrame.setVisible(true);
frame.setVisible(false);
}
}
}
This is the SomeFrame class.
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SomeFrame extends JFrame {
public SomeFrame(){
super("something");
this.setSize(300, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
this.add(panel);
this.setVisible(true);
}
}
I've got 2 errors:
C:\Users\anderson\Documents\FlopstoGFlopsConverter1.java:80: error: cannot find symbol
long flops = Long.parseLong(this.textField1.getText());
^
symbol: variable textField1
C:\Users\anderson\Documents\FlopstoGFlopsConverter1.java:85: error: cannot find symbol
this.textField2.setText(String.valueOf(gFlops));
^
symbol: variable textField2
2 errors
it's just so difficult to learn.
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.JLabel;
import javax.swing.JTextField;
public class FlopstoGFlopsConverter1 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
FlopstoGFlopsConverter1() {
setSize(500, 350);
setVisible(true);
JButton button1 = new JButton("Convert!");
button1.addActionListener(this);
JPanel panel = new JPanel();
panel.add(button1);
panel.setLayout(null);
this.add(panel);
button1.setBounds(190, 230, 100, 30);
JLabel label1 = new JLabel("Enter Flops");
panel.add(label1);
this.add(panel);
label1.setBounds(89, 52, 150, 50);
JTextField textArea1 = new JTextField(20);
JPanel p = new JPanel();
panel.add(textArea1);
this.add(panel);
textArea1.setBounds(160, 69, 160, 20);
JTextField textArea2 = new JTextField(20);
panel.add(textArea2);
this.add(panel);
textArea2.setBounds(159, 155, 160, 20);
JLabel label2 = new JLabel("Gigaflops ");
panel.add(label2);
this.add(panel);
label2.setBounds(91, 150, 200, 30);
}
public static void main(String[]args) {
new FlopstoGFlopsConverter1();
}
public void actionPerformed(ActionEvent e) {
System.out.println("");
try {
long flops = Long.parseLong(this.textField1.getText());
double gFlops = flops/1000000000;
this.textField2.setText(String.valueOf(gFlops));
} catch(Exception exception) {
}
}
}
You need to declare textArea1 and textArea2 as instance variables and use those in actionPerformed() method.
Currently, there are no textField1 and textField2 declared, instead textArea1 and textArea2, but even these are in the local scope of the constructor.
JTextField textArea1 = null; // Outside the constructor, inside the class
JTextField textArea2 = null; // Outside the constructor, inside the class
...
...
// Inside actionPerformed method
long flops = Long.parseLong(this.textArea1.getText()); // use textArea1
...
this.textArea2.setText(String.valueOf(gFlops));
As it says you do not have any declaration of textField1 and textField2. Declare them first. If you meant to use textArea1 and textArea2, declare them as instance variables after the class declaration so you can use them. Your class would look like this:
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.JLabel;
import javax.swing.JTextField;
public class FlopstoGFlopsConverter1 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField textArea1;
private JTextField textArea2;
FlopstoGFlopsConverter1() {
setSize(500, 350);
setVisible(true);
JButton button1 = new JButton("Convert!");
button1.addActionListener(this);
JPanel panel = new JPanel();
panel.add(button1);
panel.setLayout(null);
this.add(panel);
button1.setBounds(190, 230, 100, 30);
JLabel label1 = new JLabel("Enter Flops");
panel.add(label1);
this.add(panel);
label1.setBounds(89, 52, 150, 50);
textArea1 = new JTextField(20);
JPanel p = new JPanel();
panel.add(textArea1);
this.add(panel);
textArea1.setBounds(160, 69, 160, 20);
textArea2 = new JTextField(20);
panel.add(textArea2);
this.add(panel);
textArea2.setBounds(159, 155, 160, 20);
JLabel label2 = new JLabel("Gigaflops ");
panel.add(label2);
this.add(panel);
label2.setBounds(91, 150, 200, 30);
}
public static void main(String[]args) {
new FlopstoGFlopsConverter1();
}
public void actionPerformed(ActionEvent e) {
System.out.println("");
try {
long flops = Long.parseLong(this.textArea1.getText());
double gFlops = flops/1000000000;
this.textArea2.setText(String.valueOf(gFlops));
} catch(Exception exception) {
}
}
}
How would I go upon adding JLabel hovering? Like when you move your mouse over top a JLabel a and new image will overlap it. I know how to make it work with buttons, but the same technique will not work for JLabels. Will anyone guide me towards adding JLabel hovering? Please and thanks.
package src;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/*
* #Author - 0x29A
*
*
*/
public class Jframe {
public static void main(final String args[]) {
/*
* #Images
*/
final ImageIcon icon = new ImageIcon("Data/button.png");
final JLabel label = new JLabel(icon);
final ImageIcon icon1 = new ImageIcon("Data/button1.png");
final JLabel label1 = new JLabel(icon1);
final ImageIcon icon2 = new ImageIcon("Data/button2.png");
final JLabel label2 = new JLabel(icon2);
final ImageIcon icon3 = new ImageIcon("Data/button3.png");
final JLabel label3 = new JLabel(icon3);
final ImageIcon icon4 = new ImageIcon("Data/button4.png");
final JLabel label4 = new JLabel(icon4);
final ImageIcon icon5 = new ImageIcon("Data/button5.png");
final JLabel label5 = new JLabel(icon5);
final ImageIcon icon6 = new ImageIcon("Data/background.png");
final JLabel label6 = new JLabel(icon6);
/*
* #Image Location
*/
label.setBounds(282, 255, 96, 96);
label1.setBounds(384, 255, 96, 96);
label2.setBounds(282, 153, 96, 96);
label3.setBounds(384, 153, 198, 96);
label4.setBounds(181, 152, 96, 96);
label5.setBounds(181, 255, 96, 96);
label6.setBounds(0, 0, 765, 503);
/*
* #Frame
*/
final JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(765, 503));
frame.setLayout(null);
frame.add(label);
frame.add(label1);
frame.add(label2);
frame.add(label3);
frame.add(label4);
frame.add(label5);
frame.add(label6);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Made a quick example, it uses a MouseListener and MosueAdapter to monitor mouseExited() and mouseEntered() events on the JLabel, and when either of these methods are called (i.e when the mouse is over the label or not) the picture is changed:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import javax.swing.*;
public class LabelHoverTest extends JFrame {
Icon pic1;
Icon pic2;
JLabel label;
public LabelHoverTest(String title) {
super(title);
pic1 = UIManager.getIcon("OptionPane.informationIcon");
pic2 = UIManager.getIcon("OptionPane.questionIcon");
createAndShowUI();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LabelHoverTest("Label Hover Test").setVisible(true);
}
});
}
private void createAndShowUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
addComponentsToPane(getContentPane());
setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);
}
private void addComponentsToPane(Container contentPane) {
label = new JLabel(pic1);
contentPane.add(label, BorderLayout.CENTER);
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(java.awt.event.MouseEvent evt) {
label.setIcon(pic2);
}
#Override
public void mouseExited(java.awt.event.MouseEvent evt) {
label.setIcon(pic1);
}
});
}
}
you can use the MouseEntered mouse event for that and write this code
JLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("image location")));