Click here to Skip to main content
13,801,048 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
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:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
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;

/**
 *
 * @author Chiranthaka
 */
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);
    
    //Adding controls to the form.
    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);
    
    //Setting the size of the controls and placing them.
    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);
       
    //Addind an action when clicked the 'OK' button.
    jButAdd.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent e) {
          JOptionPane.showMessageDialog(jFirstForm, "Eggs are not supposed to be green.");
        }
    });
    
    //Addind an action when clicked the 'Cancel' button.
    jButCancel.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            clearText();
        }
    });
    
    //Setting the default close action.
    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
Posted 3-Jan-18 8:38am
Updated 3-Jan-18 8:40am
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Use this
jFirstForm.getContentPane().setBackground(Color.BLUE);
  Permalink  
Comments
Chiranthaka Sampath 5-Jan-18 6:12am
   
I have put this statement in JavaSwingFirstExample(){} and it works fine. Thanks for the quick response.
wseng 8-Jan-18 3:19am
   
you're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.181215.1 | Last Updated 3 Jan 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100