JAR file messes GUI? - java

I've got some homework to make a simple login window.
After I finished with my coding I tried exporting the file into a runnable jar file, and after running the jar file I saw that it had messed up the GUI.
The JPasswordField fills up my entire JFrame for some reason. I have no clue what might be the problem because it runs fine from the IDE. any help?
here is the code (works fine in eclipse IDE):`
public class Login extends JFrame {
private static final long serialVersionUID = 1L;
private static JTextField user = new JTextField();
private static JPasswordField pass = new JPasswordField();
private static JButton Loginbtn = new JButton("Login");
protected String[] args;
private static JFrame frame = new JFrame("Log In");
public Login(){
Loginbtn.addActionListener(new ActionListener(){
#SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e){
if(user.getText().equals("Admin") && pass.getText().equals("Nimda")){
//System.out.println("Hello ADMIN!");
JOptionPane.showMessageDialog(null, "Logged In!");
MyLog.main(args);
frame.setVisible(false);
}
else{
//System.out.println("Login error!");
JOptionPane.showMessageDialog(null, "Login Error!");
}
}
});
}
public static void main(String[] args) {
#SuppressWarnings("unused")
Login login = new Login();
JLabel username = new JLabel("Username");
JLabel password = new JLabel("Password");
frame.setSize(260, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
username.setBounds(20, 60, 100, 20);
password.setBounds(20, 85, 100, 20);
pass.setBounds(85, 85, 150, 20);
user.setBounds(85, 60, 150, 20);
Loginbtn.setBounds(88, 110, 50, 20);
frame.add(username);
frame.add(password);
frame.add(Loginbtn);
frame.add(user);
frame.add(pass);
}
}
thanks in advance guys!
cheers!

Calling setVisible on the JFrame before adding your components to it causes the problem most likely. You should add all the components that you want to the JFrame before calling setVisible, as setVisible validates those components (lays them out).
What setVisible does from the API, you'll see it talks about validating the components
What validate does from API

Related

alternatives to using ".this" for addActionListener? static context issues

created login class for GUI, added all the GUI construction to a method so I can call it from my main class on startup. Calling method throws error "'GUILoginPage.this' cannot be referenced from a static context".
What are the work arounds for this? I understand the reasoning behind not being able to use .this but I haven't seen any solutions
public static void main() {
JFrame frame = new JFrame();
JButton loginButton = new JButton("Login");
JTextField userIDField = new JTextField();
JPasswordField userPasswordField = new JPasswordField();
JLabel userIDLabel = new JLabel("Username:");
JLabel userPasswordLabel = new JLabel("Password:");
userIDLabel.setBounds(50, 100, 75, 25);
userPasswordLabel.setBounds(50, 150, 75, 25);
userIDField.setBounds(125, 100, 200, 25);
userPasswordField.setBounds(125, 150, 200, 25);
loginButton.setBounds(125, 200, 100, 25);
loginButton.addActionListener(this);
frame.add(userIDLabel);
frame.add(userPasswordLabel);
frame.add(userIDField);
frame.add(userPasswordField);
frame.add(loginButton);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLayout(null);
frame.setVisible(true);
}
this doesn't have any meaning in a static context. your main is Static so it means that there is no instance of a class to work with.
Here you will need to define and use your own action listener that will handle the button click, such as :
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
performLogin();
}
} );

getting two instances of JDialog

I am a creating a certain project in java.
I added a button to sign up the user on one jframe and on button click first jframe dispose and second opens up where i have added a back button to go to first frame and login button. When login button is clicked a jdialog appears.
(I extends the jdialog class)
I have found out that if i click back button and then again click on sign up button, second jframe opens up which is normal but when i click login two instances of jdialog appears.
Here is the code for first JFrame button
frontsignbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new SignUpNew().buildDesign();
frontframe.dispose();
}
});
And for second
login.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Login();
}
});
And JDialog code
public class Login extends JDialog {
private JLabel usernametext = new JLabel("User Name");
private JLabel userpasswordtext = new JLabel("Password");
private JLabel message = new JLabel();
private JTextField userfield = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton submit = new JButton("Submit");
{
setTitle("Login");
setLayout(null);
setSize(400, 300);
setLocation(400, 300);
setVisible(true);
setResizable(false);
usernametext.setBounds(20, 20, 80, 10);
userpasswordtext.setBounds(20, 80, 80, 10);
userfield.setBounds(150, 20, 100, 20);
password.setBounds(150, 80, 100, 20);
submit.setBounds(75, 140, 100, 30);
message.setBounds(140, 240, 230, 30);
add(usernametext);
add(userpasswordtext);
add(userfield);
add(password);
add(submit);
add(message);
}
Any idea why this happens. I want to fix this.

How do I make my JPasswordField use a specific password?

Hello Guys I need a password that I want to be writen in the JPasswordField
(JPasswordFieldAnon) and attach submit action to the two JButtons
(AnonButton and AnonButton1).
Here is the code:
package javafx;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class Anonymous {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Anonymous window = new Anonymous();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Anonymous() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setTitle("Anonymous Terminal. Enter Password");
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(952, 785);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel JLabelAnon = new JLabel("We are Anonymous. We are Legion. We do not forgive. We do not forget. Expect Us.");
JLabelAnon.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
JLabelAnon.setBackground(Color.BLACK);
JLabelAnon.setFont(new Font("hooge 05_53", Font.PLAIN, 13));
JLabelAnon.setForeground(Color.GREEN);
JLabelAnon.setBounds(116, 566, 695, 14);
frame.getContentPane().add(JLabelAnon);
JPasswordField JPasswordFieldAnon = new JPasswordField("Enter Password: ", 1);
JPasswordFieldAnon.setBackground(new Color(0, 128, 0));
JPasswordFieldAnon.setToolTipText("Enter Password\r\n");
JPasswordFieldAnon.setText("");
JPasswordFieldAnon.setName("Anonymous Password");
JPasswordFieldAnon.setBounds(367, 535, 206, 22);
frame.getContentPane().add(JPasswordFieldAnon);
JButton AnonButton = new JButton("Expect Us!");
AnonButton.setBackground(new Color(34, 139, 34));
AnonButton.setName("Log In");
AnonButton.setBounds(579, 535, 115, 22);
frame.getContentPane().add(AnonButton);
JButton AnonButton1 = new JButton("Expect Us!");
AnonButton1.setName("Log In");
AnonButton1.setBackground(new Color(34, 139, 34));
AnonButton1.setBounds(246, 535, 115, 22);
frame.getContentPane().add(AnonButton1);
JTextPane textPane1 = new JTextPane();
textPane1.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
textPane1.setEditable(false);
textPane1.setBackground(Color.BLACK);
textPane1.setBounds(108, 563, 703, 20);
frame.getContentPane().add(textPane1);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon("F:\\FBI-Terminal_1.png"));
lblNewLabel.setBounds(0, 0, 948, 759);
frame.getContentPane().add(lblNewLabel);
}
public void actionPerformed(ActionEvent e) {
}
}
My problem is that I tried around 5 days now just trying a lot methods...
Some notes about your code.
Your password field variable does not have scope enough to retrieve the typed password later because it's a local variable within initialize() method. I'd define it as a class variable.
You have to attach an ActionListener to the buttons to actually "submit" the password.
You have an actionPerformed() method but the implementation of ActionListener interface is missing in your class header. This is why we should use #Override annotation when we implement or override existing methods.
See these tutorials:
How to Use Buttons
How to Use Password Fields
Variables (about different variable scopes)
Off-topic
Swing is designed to work with Layout Managers. and the use of methods such as setLocation(...), setBounds(...) or setXxxSize(...) is highly discouraged.

Java: Why isn't this JFrame button displaying?

Basically, all i wan't is for this button to display. It was working in another program I had earlier but it doesn't seem to be working in this one and i have no idea why. If anyone could help that would be great.
public void fixtureList()
{
JButton editButton;
setLayout(null);
editButton = new JButton("Edit");
editButton.setBounds(200, 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");
}
});
}
You forget to call loginPanel(). Try:
Main window = new Main();
window.setTitle("PE Fixtures v1.0");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.loginPanel();
window.setSize(250, 430);
window.getContentPane().setBackground(new Color(53, 56, 64));
window.setVisible(true);
Although, because you are subclassing JFrame, I would suggest doing most of that work in the constructor.
You need to call loginPanel() method within your main method, it is not being used at the moment.

Program shows an empty window [duplicate]

This question already has answers here:
JLabel doesn't show up
(4 answers)
Closed 9 years ago.
I have started a program in JAVA and SOMETIMES, when I run it or debug it, it shows an empty white window. I have no idea why, but I redebug it and it shows the window correctly. Btw, it has nothing to do with the mysql connect void at the end.
Here is the code:
package com.hinx.client;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.sql.*;
public class Main {
public static void main(String [] args)
{
createWindow();
}
static void createWindow()
{
//Create panel
JPanel content = new JPanel();
content.setLayout(null);
//Build the frame
JFrame frame = new JFrame("Hinx - A marketplace for apps - Client ALPHA_0.0.1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 233);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(content);
frame.setVisible(true);
//Create username label
JLabel username = new JLabel("Username:");
username.setFont(new Font("Arial", Font.BOLD, 15));
username.setForeground(Color.white);
username.setBounds(34, 8, 100, 50);
//Create password label
JLabel password = new JLabel("Password:");
password.setFont(new Font("Arial", Font.BOLD, 15));
password.setForeground(Color.white);
password.setBounds(36, 85, 100, 50);
//Create username field
JTextField usernamet = new JTextField(20);
usernamet.setBounds(12, 50, 125, 30);
usernamet.setBorder(javax.swing.BorderFactory.createEmptyBorder());
//Create password field
JTextField passwordt = new JTextField(20);
passwordt.setBounds(12, 125, 125, 30);
passwordt.setBorder(javax.swing.BorderFactory.createEmptyBorder());
//Add the login button
JButton login = new JButton("Login");
login.setBounds(0, 175, 150, 30);
login.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
//Create login panel
JPanel loginpanel = new JPanel();
loginpanel.setLayout(null);
loginpanel.setBounds(0, 0, 150, 400);
loginpanel.setBackground(Color.gray);
//Add the items to the loginpanel
loginpanel.add(username);
loginpanel.add(password);
loginpanel.add(usernamet);
loginpanel.add(passwordt);
loginpanel.add(login);
//Add the items to the content panel
content.add(loginpanel);
}
protected void connect()
{
String driver = "com.mysql.jdbc.Driver";
String dbadress = "";
String dbname = "";
String username = "";
String password = "";
try
{
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(dbadress+dbname, username,password);
Statement st = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
}
frame.setVisible(true);
make it the last statement, after you add all the components to the JFrame.
Also, it is generally a best practice to do any swing-related code in the GUI-thread (EDT):
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createWindow();
}
});
}
Swing GUIs should be started on the Event Dispatch Thread. See Initial Threads for more details.
Invoke frame.setVisible(true); at the end of your method ( after adding all the components to panel)

Categories