Hi guys,
I have a small question. I have developed a JFrame Form from scratch using Java SWING package.
The appearance is ok. I have not used a layout manager so that I have manually given the locations & the dimensions.
It also connect to a MySQL database and there aren't any issues regarding that also.
Now I want to change the default background color of the JFrame Form. I am unable to do that. Also I found that initComponents() has not declared by my self.
So that when I tried to declare getContentPane() but I couldn't do that also.
Please refer the below snippet and help me to solve the issue.
I am using NetBeans with JavaSE.
The below snippet is working fine in my development environment.
What I have tried:
package javaswing;
import java.awt.Color;
import java.awt.Container;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaSwingFirstExample {
JFrame jFirstForm = new JFrame();
JLabel jLabFirstName = new JLabel("First Name");
JLabel jLabLastName = new JLabel("Last Name");
JLabel jLabAddress = new JLabel("Address");
JLabel jLabAge = new JLabel("Age");
JTextField jTxtFirstName = new JTextField();
JTextField jTxtLastName = new JTextField();
JTextField jTxtAddress = new JTextField();
JTextField jTxtAge = new JTextField();
JButton jButAdd = new JButton("Add");
JButton jButUpdate = new JButton("Update");
JButton jButDelete = new JButton("Delete");
JButton jButEdit = new JButton("Edit");
JButton jButCancel = new JButton("Cancel");
JavaSwingFirstExample(){
jFirstForm.setSize(400,400);
jFirstForm.setLayout(null);
jFirstForm.setVisible(true);
jFirstForm.add(jLabFirstName);
jFirstForm.add(jLabLastName);
jFirstForm.add(jLabAddress);
jFirstForm.add(jLabAge);
jFirstForm.add(jTxtFirstName);
jFirstForm.add(jTxtLastName);
jFirstForm.add(jTxtAddress);
jFirstForm.add(jTxtAge);
jFirstForm.add(jButAdd);
jFirstForm.add(jButUpdate);
jFirstForm.add(jButDelete);
jFirstForm.add(jButEdit);
jFirstForm.add(jButCancel);
jLabFirstName.setBounds(20, 10, 150, 35);
jLabLastName.setBounds(20, 55, 150, 35);
jLabAddress.setBounds(20, 100, 150, 35);
jLabAge.setBounds(20, 145, 150, 35);
jTxtFirstName.setBounds(170, 10, 200, 35);
jTxtLastName.setBounds(170, 55, 200, 35);
jTxtAddress.setBounds(170, 100, 200, 35);
jTxtAge.setBounds(170, 145, 200, 35);
jButAdd.setBounds(30, 200, 150, 35);
jButUpdate.setBounds(200, 200, 150, 35);
jButDelete.setBounds(30, 255, 150, 35);
jButEdit.setBounds(200, 255, 150, 35);
jButCancel.setBounds(120, 310, 150, 35);
jButAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(jFirstForm, "Eggs are not supposed to be green.");
}
});
jButCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearText();
}
});
jFirstForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JavaSwingFirstExample();
String dbURL = "jdbc:mysql://localhost:3306/basicinfo";
String dbUser = "root";
String dbPassword = "1234@com";
try {
Connection dbConn = DriverManager.getConnection(dbURL, dbUser, dbPassword);
JOptionPane.showMessageDialog(null, "Sucessful. My SQL Database is now conneted!");
} catch (SQLException ex) {
Logger.getLogger(JavaSwingFirstExample.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Database is not connecting!");
}
}
private void clearText(){
jTxtFirstName.setText("");
jTxtLastName.setText("");
jTxtAddress.setText("");
jTxtAge.setText("");
}
}
Please refer the code and suggest me a solution.
Thanks,
ChiranSJ