Problem with gridbaglayout in JPanel

Hello I am trying to display contact information in three serperate Jpanels on tabbbed panes.I would like to get help in configuring maybe just the pane of void showPane1().I need something like
Searchlb | Combodropname1
firstnamelb | firstnametxt | lastnamelb | lastnametxt
Addresslb addresslb.Horizontal-----------------------*-
citylb | citytxt | statelb | statetxt
postcodelb | postcodetxt | countrylb | countrytxt
emaillb | emailtxt | homephonelb | homephonetxt
faxnumberlb | faxnumbertxt
Other panes have buttons
Here`s the code it operates on login of MSAccess table called persons:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;
import java.util.Date;
import java.text.NumberFormat;
public class Addressbook extends JFrame{
     // Define constant variables
     final static String driver      = "sun.jdbc.odbc.JdbcOdbcDriver";
     final static String url      = "jdbc:odbc:addBKTAFE";
     final static String login      = "LOG-IN";
     final static String tab_login      = "Log-in";
     final static String tab1      = "INQUIRE Personnel Details";
     final static String tab2      = "UPDATE/DELETE Personnel Details";
     final static String tab3      = "INSERT Personnel Details";
     final static String insert      = "SAVE RECORD";
     final static String update           = "UPDATE RECORD";
     final static String delete      = "DELETE RECORD";
     final static String inquire      = "INQUIRE RECORD";
     final static String clear      = " CLEAR ";
     final static String relogin      = "Log-in failed! Please relog-in!";
     final static String norecfound      = "No Record Found!";
     final static String recinserted     = "Record Inserted!";
     final static String recupdated      = "Record Updated!";
     final static String recdeleted      = "Record Deleted!";
     final static String numericerror = "Age should be numeric!";
     final static String information = "INFORMATION";
     final static String error      = "ERROR";
     final static String genexception = "GENERAL EXCEPTION";
     final static String sqlexception = "SQL EXCEPTION";
     final static String confdelete = "CONFIRM DELETE";
     final static String slash      = "/";
     final static String table1      = "persons";
     final static String table2      = "Addresses";
// Events events = new Events(this);
     // Define variables     for general use
     String sql = ""; // Used to store sql statements
     int pane_number = 0; // Used to indicate what screen needs to be processed
                                   // like resetting input fields and comboboxes
     boolean abort = false;// Used to indicate if error found to avoid further
                                   // processing/validations
     // Define container, panels and tabbedpane
Container cntr = getContentPane();
JTabbedPane tpane = new JTabbedPane();
     JPanel      cbpanel1 , cbpanel2 , cbpanel3,
               panel1 , panel2 , panel3;
     // Setup constraints and type of layout
     GridBagConstraints constraints = new GridBagConstraints();
     GridBagConstraints constraints1 = new GridBagConstraints();
     GridBagConstraints constraints2 = new GridBagConstraints();
     GridBagConstraints constraints3 = new GridBagConstraints();
     GridBagLayout layout = new GridBagLayout ();
     // Define fonts to be used
     Font labelFont = new Font("Arial",Font.PLAIN,12);
     Font buttonFont = new Font("Arial",Font.BOLD,13);
     // Define labels
     JLabel lbUser      = new JLabel("Enter User ID: " );
     JLabel lbPassword      = new JLabel("Enter Password: ");
     JLabel lbSelectName      = new JLabel("Search Name: " );
     JLabel lbFirstName      = new JLabel("First Name: " );
     JLabel lbLastName      = new JLabel("Last Name: " );
     JLabel lbAddress           = new JLabel("Address: " );
     JLabel lbCity           = new JLabel("City" );
     JLabel lbState           = new JLabel("State: " );
     JLabel lbPostcode      = new JLabel("Postcode" );
JLabel lbCountry           = new JLabel("Country" );
     JLabel lbEmailAddress      = new JLabel("Email Address: " );
JLabel lbHomeNumber      = new JLabel("Home Phone No.: " );
JLabel lbFaxNumber      = new JLabel("Fax No.: " );
     // Define combo boxes in third screen (insert pane)
     JComboBox cbName1          = new JComboBox();
     JComboBox cbPersonId1          = new JComboBox();
     // Define combo boxes in second (update/delete pane)
     JComboBox cbName2          = new JComboBox();
     JComboBox cbPersonId2          = new JComboBox();
     // Define buttons, text fields and password field
     JButton btLogin      = new JButton (login );
     JButton btInsert = new JButton (insert );
     JButton btUpdate      = new JButton (update );
     JButton btDelete      = new JButton (delete );
     JButton btInquire      = new JButton (inquire);
     JButton btClear      = new JButton (clear );
     JPasswordField jpPassword           = new JPasswordField(10 );
     JTextField tfUser           = new JTextField("",10 );
     // Inquiry fields on first screen (inquiry pane)
     JTextField tfFirstName1      = new JTextField("",30);
     JTextField tfLastName1           = new JTextField("",30);
     JTextField      tfAddress1          = new JTextField("",30);
     JTextField      tfCity1 = new JTextField("",15);
     JTextField      tfState1 = new JTextField("",15);
     JTextField      tfPostcode1      = new JTextField("",30);
     JTextField      tfCountry1          = new JTextField("",15 );
JTextField      tfEmailAddress1      = new JTextField("",30);
JTextField      tfHomeNumber1      = new JTextField("",15);
     JTextField      tfFaxNumber1           = new JTextField("",15 );
     // Input fields on second screen (update/delete pane)
     JTextField tfFirstName2      = new JTextField("",30);
     JTextField tfLastName2          = new JTextField("",30);
     JTextField      tfAddress2          = new JTextField("",30);
     JTextField      tfCity2 = new JTextField("",15);
     JTextField      tfState2 = new JTextField("",15);
     JTextField      tfPostcode2      = new JTextField("",30);
     JTextField      tfCountry2          = new JTextField("",15 );
JTextField      tfEmailAddress2      = new JTextField("",30);
JTextField      tfHomeNumber2      = new JTextField("",15);
     JTextField      tfFaxNumber2      = new JTextField("",15 );
     // Input fields on third screen (insert pane)
     JTextField tfFirstName3      = new JTextField("",30);
     JTextField tfLastName3          = new JTextField("",30);
     JTextField      tfAddress3          = new JTextField("",30);
     JTextField      tfCity3 = new JTextField("",15);
     JTextField      tfState3 = new JTextField("",15);
     JTextField      tfPostcode3      = new JTextField("",30);
     JTextField      tfCountry3          = new JTextField("",15 );
JTextField      tfEmailAddress3      = new JTextField("",30);
JTextField      tfHomeNumber3      = new JTextField("",15);
     JTextField      tfFaxNumber3           = new JTextField("",15 );
//------------------------------------------------------------------------------>>>
//-----------------------Start Addressbook()------------------------------------>>>
Addressbook(){
// define listener after adding items to CB to avoid triggering it
     //     cbName1.addItemListener(new ItemListener());
// public void itemStateChanged(ItemEvent e){
//--------------------------END Addressbook constructor------------------------------------->>>
//------------------------------------------------------------------------------>>>
//------------------------------------------------------------------------------>>>
//--------------------Start setupLoginPanel()----------------------------------->>>
     // Setup the login screen
     public void setupLoginPanel(){
          // set application title
     setTitle("Address Book Application");
          // center screen
          setLocation((Toolkit.getDefaultToolkit().getScreenSize().width
                              - getWidth())/2,
                    (Toolkit.getDefaultToolkit().getScreenSize().height
                         - getHeight())/2);
     panel1 = new JPanel();
          // set screen border
          panel1.setBorder(BorderFactory.createTitledBorder(
                              BorderFactory.createEtchedBorder(),""));
          // add tabbedpane to panel
          tpane.addTab(tab_login, panel1);
          // add panel to container
          cntr.add(tpane);
          // setup layout as GridBagLayout
          constraints.insets = new Insets(2,2,2,2);
          panel1.setLayout(layout);
          // setup User ID label in display area
          lbUser.setFont(labelFont);
          constraints.ipadx = 2;
          constraints.ipady = 2;
          constraints.gridx = 0;
          constraints.gridy = 0;
          constraints.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbUser, constraints);
          panel1.add(lbUser);
          // setup User ID input field in display area
          tfUser.setFont(labelFont);
          constraints.ipadx = 2;
          constraints.ipady = 2;
          constraints.gridx = 1;
          constraints.gridy = 0;
          constraints.fill = GridBagConstraints.HORIZONTAL;
          layout.setConstraints(tfUser, constraints);
          panel1.add(tfUser);
          // setup Password label in display area
          lbPassword.setFont(labelFont);
          constraints.ipadx = 2;
          constraints.ipady = 2;
          constraints.gridx = 0;
          constraints.gridy = 1;
          constraints.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbPassword, constraints);
          panel1.add(lbPassword);
          // setup Password input field in display area
          jpPassword.setEchoChar('*');
          constraints.ipadx = 2;
          constraints.ipady = 2;
          constraints.gridx = 1;
          constraints.gridy = 1;
          layout.setConstraints(jpPassword, constraints);
          panel1.add(jpPassword);
          // setup Login button in display area
          btLogin.setFont(buttonFont);
          constraints.anchor = GridBagConstraints.WEST;
          constraints.gridy = 3;
          constraints.gridx = 1;
          layout.setConstraints(btLogin, constraints);
          panel1.add(btLogin);
          // setup login button listener
          btLogin.addActionListener(new ButtonHandler());
          // allow ALT L to press login button
          btLogin.setMnemonic('l');
//--------------------End setupLoginPanel()------------------------------------->>>
//--------------------Start login()--------------------------------------------->>>
     // Validate user input from the login screen based on information from login
     // table (note: manually create/update your login from table LOGIN).
     public void login(){
          String user = tfUser.getText();
          user = user.trim();
          char[] pw = jpPassword.getPassword();
String password = new String(pw).trim();
          sql = "SELECT * FROM persons WHERE username='"+
               user+"' AND password='"+password+"'";
          try{
               // load MS Access driver
               Class.forName(driver);
          }catch(java.lang.ClassNotFoundException ex){
               JOptionPane.showMessageDialog(null,ex.getMessage(), error ,
                    JOptionPane.PLAIN_MESSAGE);
          try{
               // setup connection to DBMS
               Connection conn = DriverManager.getConnection(url);
               // create statement
               Statement stmt = conn.createStatement();
               // execute sql statement
               stmt.execute(sql);
               ResultSet rs = stmt.getResultSet();
               boolean recordfound = rs.next();
               if (recordfound){
                    tpane.removeTabAt(0);
                    showPane1();
                    showPane2();
                    showPane3();
               else{
                    // username/password invalid
                    JOptionPane.showMessageDialog(null,relogin, error,
                         JOptionPane.INFORMATION_MESSAGE);
                    //clear login and password fields
                    tfUser.setText ("");
                    jpPassword.setText("");
               conn.close();
          }catch(Exception ex){
               JOptionPane.showMessageDialog(null,ex.getMessage(), genexception,
                    JOptionPane.INFORMATION_MESSAGE);
//--------------------End login()----------------------------------------------->>>
//--------------------Start showPane1()----------------------------------------->>>
     // Setup screen 1(inquiry pane) including labels, input fields, comboboxes.
     // Table PERSONS is read to list inquiry parameters.
     void showPane1(){
          panel1 = new JPanel();
          cbpanel1 = new JPanel();
          // set screen border
          panel1.setBorder(BorderFactory.createTitledBorder(
                              BorderFactory.createEtchedBorder(),""));
          // add tabbedpane to panel
          tpane.addTab(tab1, panel1);
          // setup layout as GridBagLayout
          constraints1.insets = new Insets(2,2,2,2);
          panel1.setLayout (layout);
          cbpanel1.setLayout (layout);
          // setup Name combobox label
          lbSelectName.setFont(labelFont);
          constraints1.gridx = 0;
          constraints1.gridy = 0;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbSelectName, constraints1);
          panel1.add(lbSelectName);
          // setup Name combobox as search key
          cbName1.setFont(labelFont);
          constraints1.ipady = 10;
          constraints1.gridx = 1;
          constraints1.gridy = 0;
          constraints1.gridwidth = 3;
          constraints1.anchor = GridBagConstraints.WEST;
          constraints1.fill = GridBagConstraints.HORIZONTAL;
          layout.setConstraints(cbName1, constraints1);
          panel1.add(cbName1);
          // setup search combobox (Name and corresponding key)
          cbName1.addItem ("Choose one:");
          cbPersonId1.addItem("0");
          // setup First Name label in display area
          lbFirstName.setFont(labelFont);
          constraints1.gridx = 0;
          constraints1.gridy = 1;
          constraints1.anchor = GridBagConstraints.WEST;
          constraints1.fill = GridBagConstraints.NONE;
          constraints1.gridwidth = 1;
          layout.setConstraints(lbFirstName, constraints1);
          panel1.add(lbFirstName);
          // setup First Name input field in display area
          tfFirstName1.setFont(labelFont);
          tfFirstName1.setEditable(false);
          constraints1.gridx = 1;
          constraints1.gridy = 1;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfFirstName1, constraints1);
          panel1.add(tfFirstName1);
          // setup Last Name label in display area
          lbLastName.setFont(labelFont);
          constraints1.gridx = 2;
          constraints1.gridy = 1;
          constraints1.anchor = GridBagConstraints.WEST;
          constraints1.fill = GridBagConstraints.NONE;
          layout.setConstraints(lbLastName, constraints1);
          panel1.add(lbLastName);
          // setup Last Name input field in display area
          tfLastName1.setFont(labelFont);
          tfLastName1.setEditable(false);
          constraints1.gridx = 3;
          constraints1.gridy = 1;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfLastName1, constraints1);
          panel1.add(tfLastName1);
          // setup Address label in display area
          lbAddress.setFont(labelFont);
          constraints1.gridx = 0;
          constraints1.gridy = 2;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbAddress, constraints1);
          panel1.add(lbAddress);
          // setup Address input field in display area
          tfAddress1.setFont(labelFont);
          tfAddress1.setEditable(false);
          constraints1.gridx = 1;
          constraints1.gridy = 2;
          constraints1.gridwidth = 3;
          constraints1.anchor = GridBagConstraints.WEST;
          constraints1.fill = GridBagConstraints.HORIZONTAL;
          layout.setConstraints(tfAddress1, constraints1);
          panel1.add(tfAddress1);
          // setup City label in display area
          lbCity.setFont(labelFont);
          constraints1.gridx = 0;
          constraints1.gridy = 3;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbCity, constraints1);
          panel1.add(lbCity);
          // setup City input field in display area
          tfCity1.setFont(labelFont);
          tfCity1.setEditable(false);
          constraints1.gridx = 1;
          constraints1.gridy = 3;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfCity1, constraints1);
          panel1.add(tfCity1);
          // setup State label in display area
          lbState.setFont(labelFont);
          constraints1.gridx = 2;
          constraints1.gridy = 3;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbState, constraints1);
          panel1.add(lbState);
          // setup State input field in display area
          tfState1.setFont(labelFont);
          tfState1.setEnabled(false);
          constraints1.gridx = 3;
          constraints1.gridy = 3;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfState1, constraints1);
          panel1.add(tfState1);
          // setup Postcode label in display area
          lbPostcode.setFont(labelFont);
          constraints1.gridx = 0;
          constraints1.gridy = 4;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbPostcode, constraints1);
          panel1.add(lbPostcode);
          // setup Address input field in display area
          tfPostcode1.setFont(labelFont);
          tfPostcode1.setEditable(false);
          constraints1.gridx = 1;
          constraints1.gridy = 4;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfPostcode1, constraints1);
          panel1.add(tfPostcode1);
          // setup Country label in display area
          lbCountry.setFont(labelFont);
          constraints1.gridx = 2;
          constraints1.gridy = 4;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbCountry, constraints1);
          panel1.add(lbCountry);
          // setup Country input field in display area
          tfCountry1.setFont(labelFont);
          tfCountry1.setEditable(false);
          constraints1.gridx = 3;
          constraints1.gridy = 4;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfCountry1, constraints1);
          panel1.add(tfCountry1);
          // setup Email Address label in display area
          lbEmailAddress = new JLabel ("Email Address:");
          lbEmailAddress.setFont(labelFont);
          constraints1.gridx = 0;
          constraints1.gridy = 5;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbEmailAddress, constraints1);
          panel1.add(lbEmailAddress);
          // setup Email Address input field in display area
          tfEmailAddress1.setFont(labelFont);
          constraints1.gridx = 1;
          constraints1.gridy = 5;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfEmailAddress1, constraints1);
          panel1.add(tfEmailAddress1);
          // setup Home Phone Number label in display area
          lbHomeNumber.setFont(labelFont);
          constraints1.gridx = 2;
          constraints1.gridy = 5;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbHomeNumber, constraints1);
          panel1.add(lbHomeNumber);
          // setup Home Phone Number input field in display area
          tfHomeNumber1.setFont(labelFont);
          tfHomeNumber1.setEditable(false);
          constraints1.gridx = 3;
          constraints1.gridy = 5;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfHomeNumber1, constraints1);
          panel1.add(tfHomeNumber1);
          // setup Fax Number label in display area
          lbFaxNumber.setFont(labelFont);
          constraints1.gridx = 0;
          constraints1.gridy = 6;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbFaxNumber, constraints1);
          panel1.add(lbFaxNumber);
          // setup Fax Number input field in display area
          tfFaxNumber1.setFont(labelFont);
          tfFaxNumber1.setEditable(false);
          constraints1.gridx = 1;
          constraints1.gridy = 6;
          constraints1.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfFaxNumber1, constraints1);
          panel1.add(tfFaxNumber1);
// indicate inquiry pane
          pane_number = 1;
          // read table get the list of names in CB search key
     accessDBInit();
          // define listener after adding items to CB to avoid triggering it
          cbName1.addItemListener(new ComboBoxHandler());
//--------------------End showPane1()------------------------------------------->>>
//--------------------Start showPane2()----------------------------------------->>>
     // Setup screen 2(update and delete pane) including labels, input fields,
     // comboboxes, and buttons. Table PERSONS is read to list inquiry parameters.
     void showPane2(){
          panel2 = new JPanel();
          cbpanel2 = new JPanel();
          labelFont = new Font("Arial",Font.PLAIN,12);
          buttonFont = new Font("Arial",Font.BOLD,12);
          // set screen border
          panel2.setBorder(BorderFactory.createTitledBorder(
               BorderFactory.createEtchedBorder(),""));
          // add tabbedpane to panel
          tpane.addTab(tab2, panel2);
          // setup layout as GridBagLayout
          constraints2.insets = new Insets(2,2,2,2);
          panel2.setLayout (layout);
          cbpanel2.setLayout (layout);
          // setup Name combobox label
          lbSelectName.setFont(labelFont);
          constraints2.gridx = 0;
          constraints2.gridy = 0;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbSelectName, constraints2);
          panel1.add(lbSelectName);
          // setup Name combobox as search key
          cbName2.setFont(labelFont);
          constraints2.ipady = 10;
          constraints2.gridx = 1;
          constraints2.gridy = 0;
          constraints2.gridwidth = 3;
          constraints2.anchor = GridBagConstraints.WEST;
          constraints2.fill = GridBagConstraints.HORIZONTAL;
          layout.setConstraints(cbName1, constraints2);
          panel1.add (cbName1);
          // setup search combobox (Name and corresponding key)
          cbName1.addItem ("Choose one:");
          cbPersonId1.addItem("0");
          // setup First Name label in display area
          lbFirstName.setFont(labelFont);
          constraints2.gridx = 0;
          constraints2.gridy = 1;
          constraints2.anchor = GridBagConstraints.WEST;
          constraints2.fill = GridBagConstraints.NONE;
          constraints2.gridwidth = 1;
          layout.setConstraints(lbFirstName, constraints2);
          panel1.add(lbFirstName);
          // setup First Name input field in display area
          tfFirstName2.setFont(labelFont);
          tfFirstName2.setEditable(false);
          constraints2.gridx = 1;
          constraints2.gridy = 1;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfFirstName2, constraints2);
          panel1.add(tfFirstName2);
          // setup Last Name label in display area
          lbLastName.setFont(labelFont);
          constraints2.gridx = 2;
          constraints2.gridy = 1;
          constraints2.anchor = GridBagConstraints.WEST;
          constraints2.fill = GridBagConstraints.NONE;
          layout.setConstraints(lbLastName, constraints2);
          panel1.add(lbLastName);
          // setup Last Name input field in display area
          tfLastName2.setFont(labelFont);
          tfLastName2.setEditable(false);
          constraints2.gridx = 3;
          constraints2.gridy = 1;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfLastName2, constraints2);
          panel1.add(tfLastName2);
          // setup Address label in display area
          lbAddress.setFont(labelFont);
          constraints2.gridx = 0;
          constraints2.gridy = 2;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbAddress, constraints2);
          panel1.add(lbAddress);
          // setup Address input field in display area
          tfAddress2.setFont(labelFont);
          tfAddress2.setEditable(false);
          constraints2.gridx = 1;
          constraints2.gridy = 2;
          constraints2.gridwidth = 3;
          constraints2.anchor = GridBagConstraints.WEST;
          constraints2.fill = GridBagConstraints.HORIZONTAL;
          layout.setConstraints(tfAddress2, constraints2);
          panel1.add(tfAddress2);
          // setup City label in display area
          lbCity.setFont(labelFont);
          constraints2.gridx = 0;
          constraints2.gridy = 3;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbCity, constraints2);
          panel1.add(lbCity);
          // setup City input field in display area
          tfCity2.setFont(labelFont);
          tfCity2.setEditable(false);
          constraints2.gridx = 1;
          constraints2.gridy = 3;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfCity2, constraints2);
          panel1.add(tfCity2);
          // setup State label in display area
          lbState.setFont(labelFont);
          constraints2.gridx = 2;
          constraints2.gridy = 3;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbState, constraints2);
          panel1.add(lbState);
          // setup State input field in display area
          tfState2.setFont(labelFont);
          tfState2.setEnabled(false);
          constraints2.gridx = 3;
          constraints2.gridy = 3;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfState2, constraints2);
          panel1.add(tfState2);
          // indicate inquiry pane
          pane_number = 1;
          // setup Address label in display area
          lbPostcode.setFont(labelFont);
          constraints2.gridx = 0;
          constraints2.gridy = 4;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbPostcode, constraints2);
          panel1.add(lbPostcode);
          // setup Address input field in display area
          tfPostcode2.setFont(labelFont);
          tfPostcode2.setEditable(false);
          constraints2.gridx = 1;
          constraints2.gridy = 4;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfPostcode2, constraints2);
          panel1.add(tfPostcode2);
          // setup Country label in display area
          lbCountry.setFont(labelFont);
          constraints2.gridx = 2;
          constraints2.gridy = 4;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbCountry, constraints2);
          panel1.add(lbCountry);
          // setup Country input field in display area
          tfCountry2.setFont(labelFont);
          tfCountry2.setEditable(false);
          constraints2.gridx = 3;
          constraints2.gridy = 4;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfCountry2, constraints2);
          panel1.add(tfCountry2);
          // setup Email Address label in display area
          lbEmailAddress = new JLabel ("Email Address:");
          lbEmailAddress.setFont(labelFont);
          constraints2.gridx = 0;
          constraints2.gridy = 5;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbEmailAddress, constraints2);
          panel1.add(lbEmailAddress);
          // setup Email Address input field in display area
          tfEmailAddress2.setFont(labelFont);
          constraints2.gridx = 1;
          constraints2.gridy = 5;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfEmailAddress2, constraints2);
          panel1.add(tfEmailAddress2);
          // setup Home Phone Number label in display area
          lbHomeNumber.setFont(labelFont);
          constraints2.gridx = 2;
          constraints2.gridy = 5;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbHomeNumber, constraints2);
          panel1.add(lbHomeNumber);
          // setup Home Phone Number input field in display area
          tfHomeNumber2.setFont(labelFont);
          tfHomeNumber2.setEditable(false);
          constraints2.gridx = 3;
          constraints2.gridy = 5;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfHomeNumber2, constraints2);
          panel1.add(tfHomeNumber2);
          // setup Fax Number label in display area
          lbFaxNumber.setFont(labelFont);
          constraints2.gridx = 0;
          constraints2.gridy = 6;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbFaxNumber, constraints2);
          panel1.add(lbFaxNumber);
          // setup Fax Number input field in display area
          tfFaxNumber2.setFont(labelFont);
          tfFaxNumber2.setEditable(false);
          constraints2.gridx = 1;
          constraints2.gridy = 6;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfFaxNumber2, constraints2);
          panel1.add(tfFaxNumber2);
          // setup UPDATE button in display area
          btUpdate.setFont(buttonFont);
          constraints2.gridx = 3;
          constraints2.gridy = 7;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(btUpdate, constraints2);
          panel2.add(btUpdate);
          // setup DELETE button in display area
          btDelete.setFont(buttonFont);
          constraints2.gridx = 1;
          constraints2.gridy = 7;
          constraints2.anchor = GridBagConstraints.WEST;
          layout.setConstraints(btDelete, constraints2);
          panel2.add(btDelete);
          btUpdate.addActionListener(new ButtonHandler());
          btDelete.addActionListener(new ButtonHandler());
          // allow ALT U to press update button
          btUpdate.setMnemonic('u');
          // allow ALT D to press delete button
          btDelete.setMnemonic('d');
          // read table get the list of names in combo box search key
accessDBInit();
          // define listener after adding items to CB to avoid triggering it
          cbName2.addItemListener(new ComboBoxHandler());
//--------------------End showPane2()------------------------------------------->>>
//--------------------Start showPane3()----------------------------------------->>>
     // Setup screen 2(insert pane) including labels, input fields, comboboxes,
     // and buttons.
     void showPane3(){
          panel3      = new JPanel();
          // set screen border
          panel3.setBorder(BorderFactory.createTitledBorder(
               BorderFactory.createEtchedBorder(),""));
          // add tabbedpane to panel
          tpane.addTab(tab3, panel3);
          // setup layout as GridBagLayout
          constraints3.insets = new Insets(2,2,2,2);
          panel3.setLayout (layout);
          // setup First Name label in display area
          JLabel lbFirstName = new JLabel("First Name:");
          lbFirstName.setFont(labelFont);
          constraints3.gridx = 0;
          constraints3.gridy = 0;
          constraints3.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbFirstName, constraints3);
          panel3.add(lbFirstName);
          // setup First Name input field in display area
          tfFirstName3.setFont(labelFont);
          constraints3.ipady = 8; // adjust heigth of input field
          constraints3.gridx = 1;
          constraints3.gridy = 0;
          constraints3.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfFirstName3, constraints3);
          panel3.add(tfFirstName3);
          // setup Last Name label in display area
          lbLastName = new JLabel("Last Name: ");
          lbLastName.setFont(labelFont);
          constraints3.gridx = 2;
          constraints3.gridy = 0;
          constraints3.anchor = GridBagConstraints.WEST;
          layout.setConstraints(lbLastName, constraints3);
          panel3.add(lbLastName);
          // setup Last Name input field in display area
          tfLastName3.setFont(labelFont);
          constraints3.gridx = 3;
          constraints3.gridy = 0;
          constraints3.anchor = GridBagConstraints.WEST;
          layout.setConstraints(tfLastName3, constraints3);
          panel3.add(tfLastName3);
          // setup Middle Name label in display area
          lbAddress = new JLabel("Address: ");
          lbAddress.setFont(labelFont);
          constraints3.gridx = 0;
          constraints3.gridy = 1;
          constraints3.anch

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class andy extends JFrame {
  // Define constant variables
  final static String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  final static String url = "jdbc:odbc:addBKTAFE";
  final static String login = "LOG-IN";
  final static String tab_login = "Log-in";
  final static String tab1 = "INQUIRE Personnel Details";
  final static String tab2 = "UPDATE/DELETE Personnel Details";
  final static String tab3 = "INSERT Personnel Details";
  final static String tab4 = "PRINT";
  final static String insert = "SAVE RECORD";
  final static String update = "UPDATE RECORD";
  final static String delete = "DELETE RECORD";
  final static String inquire = "INQUIRE RECORD";
  final static String clear = " CLEAR ";
  final static String relogin = "Log-in failed! Please relog-in!";
  final static String norecfound = "No Record Found!";
  final static String recinserted = "Record Inserted!";
  final static String recupdated = "Record Updated!";
  final static String recdeleted = "Record Deleted!";
  final static String numericerror = "Age should be numeric!";
  final static String information = "INFORMATION";
  final static String error = "ERROR";
  final static String genexception = "GENERAL EXCEPTION";
  final static String sqlexception = "SQL EXCEPTION";
  final static String confdelete = "CONFIRM DELETE";
  final static String slash = "/";
  final static String table1 = "persons";
  final static String table2 = "Addresses";
  // Define container, panels and tabbedpane
  Container cntr = getContentPane();
  JTabbedPane tpane = new JTabbedPane();
  JPanel cbpanel1 , cbpanel2 , cbpanel3,
         panel1 , panel2 , panel3, panel4;
  // Define fonts to be used
  Font labelFont = new Font("Arial",Font.PLAIN,12);
  Font buttonFont = new Font("Arial",Font.BOLD,13);
  // Define labels
  JLabel lbUser = new JLabel("Enter User ID: " );
  JLabel lbPassword = new JLabel("Enter Password: ");
  JLabel lbSelectName = new JLabel("Search Name: " );
  JLabel lbFirstName = new JLabel("First Name: " );
  JLabel lbLastName = new JLabel("Last Name: " );
  JLabel lbAddress = new JLabel("Address: " );
  JLabel lbCity = new JLabel("City: " );
  JLabel lbState = new JLabel("State: " );
  JLabel lbPostcode = new JLabel("Postcode: " );
  JLabel lbCountry = new JLabel("Country: " );
  JLabel lbEmailAddress = new JLabel("Email Address: " );
  JLabel lbHomeNumber = new JLabel("Home Phone No.: " );
  JLabel lbFaxNumber = new JLabel("Fax No.: " );
  // Define combo boxes in third screen (insert pane)
  JComboBox cbName1     = new JComboBox();
  JComboBox cbName2     = new JComboBox();
  JComboBox cbName3     = new JComboBox();
  JComboBox cbPersonId1 = new JComboBox();
  JComboBox cbPersonId2 = new JComboBox();
  JComboBox cbPersonId3 = new JComboBox();
  // Inquiry fields on first screen (inquiry pane)
  JTextField tfFirstName1 = new JTextField("",30);
  JTextField tfLastName1 = new JTextField("",30);
  JTextField tfAddress1 = new JTextField("",30);
  JTextField tfCity1 = new JTextField("",15);
  JTextField tfState1 = new JTextField("",15);
  JTextField tfPostcode1 = new JTextField("",30);
  JTextField tfCountry1 = new JTextField("",15 );
  JTextField tfEmailAddress1 = new JTextField("",30);
  JTextField tfHomeNumber1 = new JTextField("",15);
  JTextField tfFaxNumber1 = new JTextField("",15 );
  // Input fields on second screen (update/delete pane)
  JTextField tfFirstName2 = new JTextField("",30);
  JTextField tfLastName2 = new JTextField("",30);
  JTextField tfAddress2 = new JTextField("",30);
  JTextField tfCity2 = new JTextField("",15);
  JTextField tfState2 = new JTextField("",15);
  JTextField tfPostcode2 = new JTextField("",30);
  JTextField tfCountry2 = new JTextField("",15);
  JTextField tfEmailAddress2 = new JTextField("",30);
  JTextField tfHomeNumber2 = new JTextField("",15);
  JTextField tfFaxNumber2 = new JTextField("",15);
  // Input fields on third screen (inset details pane)
  JTextField tfFirstName3 = new JTextField("",30);
  JTextField tfLastName3 = new JTextField("",30);
  JTextField tfAddress3 = new JTextField("",30);
  JTextField tfCity3 = new JTextField("",15);
  JTextField tfState3 = new JTextField("",15);
  JTextField tfPostcode3 = new JTextField("",30);
  JTextField tfCountry3 = new JTextField("",15);
  JTextField tfEmailAddress3 = new JTextField("",30);
  JTextField tfHomeNumber3 = new JTextField("",15);
  JTextField tfFaxNumber3 = new JTextField("",15);
  GridBagLayout layout = new GridBagLayout();
  GridBagConstraints constraints = new GridBagConstraints();
  public andy() {
    super("Address Book Application");
    showPane1();
    showPane2();
    showPane3();
    showPane4();
    cntr.add(tpane, "Center");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(900,385);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((screenSize.width  - getWidth())/2,
                (screenSize.height - getHeight())/2);
    setVisible(true);
  // Setup screen 1(inquiry pane) including labels, input fields, comboboxes.
  // Table PERSONS is read to list inquiry parameters.
  void showPane1() {
    panel1 = new JPanel();
    cbpanel1 = new JPanel();
    // set screen border
    panel1.setBorder(BorderFactory.createTitledBorder(
      BorderFactory.createEtchedBorder(),""));
    // add tabbedpane to panel
    tpane.addTab(tab1, panel1);
    // setup layout as GridBagLayout
    constraints.insets = new Insets(2,2,2,2);
    panel1.setLayout (layout);
    cbpanel1.setLayout (layout);
    // setup Name combobox label
    lbSelectName.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    layout.setConstraints(lbSelectName, constraints);
    panel1.add(lbSelectName);
    // setup Name combobox as search key
    cbName1.setFont(labelFont);
    constraints.ipady = 10;
    constraints.gridwidth = constraints.REMAINDER;
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(cbName1, constraints);
    panel1.add(cbName1);
    // setup search combobox (Name and corresponding key)
    cbName1.addItem ("Choose one:");
    cbPersonId1.addItem("0");
    // setup First Name label in display area
    lbFirstName.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbFirstName, constraints);
    panel1.add(lbFirstName);
    // setup First Name input field in display area
    tfFirstName1.setFont(labelFont);
    tfFirstName1.setEditable(false);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfFirstName1, constraints);
    panel1.add(tfFirstName1);
    // setup Last Name label in display area
    lbLastName.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbLastName, constraints);
    panel1.add(lbLastName);
    // setup Last Name input field in display area
    tfLastName1.setFont(labelFont);
    tfLastName1.setEditable(false);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfLastName1, constraints);
    panel1.add(tfLastName1);
    // setup Address label in display area
    lbAddress.setFont(labelFont);
    constraints.gridwidth = 1;
    constraints.anchor = GridBagConstraints.EAST;
    layout.setConstraints(lbAddress, constraints);
    panel1.add(lbAddress);
    // setup Address input field in display area
    tfAddress1.setFont(labelFont);
    tfAddress1.setEditable(false);
    constraints.gridwidth = constraints.REMAINDER;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.weightx = 1.0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    layout.setConstraints(tfAddress1, constraints);
    panel1.add(tfAddress1);
    // setup City label in display area
    lbCity.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    constraints.fill = GridBagConstraints.NONE;
        layout.setConstraints(lbCity, constraints);
    panel1.add(lbCity);
    // setup City input field in display area
    tfCity1.setFont(labelFont);
    tfCity1.setEditable(false);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfCity1, constraints);
    panel1.add(tfCity1);
    // setup State label in display area
    lbState.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbState, constraints);
    panel1.add(lbState);
    // setup State input field in display area
    tfState1.setFont(labelFont);
    tfState1.setEnabled(false);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfState1, constraints);
    panel1.add(tfState1);
    // setup Postcode label in display area
    lbPostcode.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbPostcode, constraints);
    panel1.add(lbPostcode);
    // setup Address input field in display area
    tfPostcode1.setFont(labelFont);
    tfPostcode1.setEditable(false);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfPostcode1, constraints);
    panel1.add(tfPostcode1);
    // setup Country label in display area
    lbCountry.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbCountry, constraints);
    panel1.add(lbCountry);
    // setup Country input field in display area
    tfCountry1.setFont(labelFont);
    tfCountry1.setEditable(false);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfCountry1, constraints);
    panel1.add(tfCountry1);
    // setup Email Address label in display area
    lbEmailAddress.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbEmailAddress, constraints);
    panel1.add(lbEmailAddress);
    // setup Email Address input field in display area
    tfEmailAddress1.setFont(labelFont);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfEmailAddress1, constraints);
    panel1.add(tfEmailAddress1);
    // setup Home Phone Number label in display area
    lbHomeNumber.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbHomeNumber, constraints);
    panel1.add(lbHomeNumber);
    // setup Home Phone Number input field in display area
    tfHomeNumber1.setFont(labelFont);
    tfHomeNumber1.setEditable(false);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfHomeNumber1, constraints);
    panel1.add(tfHomeNumber1);
    // setup Fax Number label in display area
    lbFaxNumber.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbFaxNumber, constraints);
    panel1.add(lbFaxNumber);
    // setup Fax Number input field in display area
    tfFaxNumber1.setFont(labelFont);
    tfFaxNumber1.setEditable(false);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfFaxNumber1, constraints);
    panel1.add(tfFaxNumber1);
    // indicate inquiry pane
    // pane_number = 1;
    // read table get the list of names in CB search key
    // accessDBInit();
    // define listener after adding items to CB to avoid triggering it
    // cbName1.addItemListener(new ComboBoxHandler());
  //--------------------End showPane1()------------------------------------------->>>
  //--------------------Start showPane2()----------------------------------------->>>
  void showPane2() {
    panel2 = new JPanel();
    cbpanel2 = new JPanel();
    // set screen border
    panel2.setBorder(BorderFactory.createTitledBorder(
      BorderFactory.createEtchedBorder(),""));
    // add tabbedpane to panel
    tpane.addTab(tab2, panel2);
    // setup layout as GridBagLayout
    constraints.ipady = 0;
    panel2.setLayout (layout);
    cbpanel2.setLayout (layout);
    // setup Name combobox label
    lbSelectName = new JLabel("Search Name: ");
    lbSelectName.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    layout.setConstraints(lbSelectName, constraints);
    panel2.add(lbSelectName);
    // setup Name combobox as search key
    cbName2.setFont(labelFont);
    constraints.ipady = 10;
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(cbName2, constraints);
    panel2.add(cbName2);
    // setup search combobox (Name and corresponding key)
    cbName2.addItem ("Choose one:");
    cbPersonId2.addItem("0");
    // setup UPDATE button in display area
    JButton btUpdate = new JButton("Update");
    btUpdate.setFont(buttonFont);
//    btUpdate.addActionListener(new ButtonHandler());
    btUpdate.setMnemonic(KeyEvent.VK_U);
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(btUpdate, constraints);
    panel2.add(btUpdate);
    // setup DELETE button in display area
    JButton btDelete = new JButton("Delete");
    btDelete.setFont(buttonFont);
//    btDelete.addActionListener(new ButtonHandler());
    btDelete.setMnemonic(KeyEvent.VK_D);
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(btDelete, constraints);
    panel2.add(btDelete);
    // setup First Name label in display area
    lbFirstName = new JLabel("First Name: ");
    lbFirstName.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbFirstName, constraints);
    panel2.add(lbFirstName);
    // setup First Name input field in display area
    tfFirstName2.setFont(labelFont);
    tfFirstName2.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfFirstName2, constraints);
    panel2.add(tfFirstName2);
    // setup Last Name label in display area
    lbLastName = new JLabel("Last Name: ");
    lbLastName.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbLastName, constraints);
    panel2.add(lbLastName);
    // setup Last Name input field in display area
    tfLastName2.setFont(labelFont);
    tfLastName2.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfLastName2, constraints);
    panel2.add(tfLastName2);
    // setup Address label in display area
    lbAddress = new JLabel("Address: ");
    lbAddress.setFont(labelFont);
    constraints.gridwidth = 1;
    constraints.anchor = GridBagConstraints.EAST;
    layout.setConstraints(lbAddress, constraints);
    panel2.add(lbAddress);
    // setup Address input field in display area
    tfAddress2.setFont(labelFont);
    tfAddress2.setEditable(true);
    constraints.gridwidth = constraints.REMAINDER;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.weightx = 1.0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    layout.setConstraints(tfAddress2, constraints);
    panel2.add(tfAddress2);
    // setup City label in display area
    lbCity = new JLabel("City: ");
    lbCity.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    constraints.fill = GridBagConstraints.NONE;
    layout.setConstraints(lbCity, constraints);
    panel2.add(lbCity);
    // setup City input field in display area
    tfCity2.setFont(labelFont);
    tfCity2.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfCity2, constraints);
    panel2.add(tfCity2);
    // setup State label in display area
    lbState = new JLabel("State: ");
    lbState.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbState, constraints);
    panel2.add(lbState);
    // setup State input field in display area
    tfState2.setFont(labelFont);
    tfState2.setEnabled(true);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfState2, constraints);
    panel2.add(tfState2);
    // setup Postcode label in display area
    lbPostcode = new JLabel("Postcode: ");
    lbPostcode.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbPostcode, constraints);
    panel2.add(lbPostcode);
    // setup Address input field in display area
    tfPostcode2.setFont(labelFont);
    tfPostcode2.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfPostcode2, constraints);
    panel2.add(tfPostcode2);
    // setup Country label in display area
    lbCountry = new JLabel("Country: ");
    lbCountry.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbCountry, constraints);
    panel2.add(lbCountry);
    // setup Country input field in display area
    tfCountry2.setFont(labelFont);
    tfCountry2.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfCountry2, constraints);
    panel2.add(tfCountry2);
    // setup Email Address label in display area
    lbEmailAddress = new JLabel ("Email Address: ");
    lbEmailAddress.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbEmailAddress, constraints);
    panel2.add(lbEmailAddress);
    // setup Email Address input field in display area
    tfEmailAddress2.setFont(labelFont);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfEmailAddress2, constraints);
    panel2.add(tfEmailAddress2);
    // setup Home Phone Number label in display area
    lbHomeNumber = new JLabel("Home Phone No.: ");
    lbHomeNumber.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbHomeNumber, constraints);
    panel2.add(lbHomeNumber);
    // setup Home Phone Number input field in display area
    tfHomeNumber2.setFont(labelFont);
    tfHomeNumber2.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfHomeNumber2, constraints);
    panel2.add(tfHomeNumber2);
    // setup Fax Number label in display area
    lbFaxNumber = new JLabel("Fax No.: ");
    lbFaxNumber.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbFaxNumber, constraints);
    panel2.add(lbFaxNumber);
    // setup Fax Number input field in display area
    tfFaxNumber2.setFont(labelFont);
    tfFaxNumber2.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfFaxNumber2, constraints);
    panel2.add(tfFaxNumber2);
  //-----------------------------End showpane2()---------------------------------->>
  //--------------------Start showPane3()----------------------------------------->>>
  void showPane3() {
    panel3 = new JPanel();
    // cbpanel3 = new JPanel();
    // set screen border
    panel3.setBorder(BorderFactory.createTitledBorder(
      BorderFactory.createEtchedBorder(),""));
    // add tabbedpane to panel
    tpane.addTab(tab3, panel3);
    // setup layout as GridBagLayout
    panel3.setLayout(layout);
    constraints.ipady = 0;
    panel3.setLayout (layout);
    // cbpanel3.setLayout (layout);
    // setup Name combobox label
    lbSelectName = new JLabel("Search Name: ");
    lbSelectName.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    layout.setConstraints(lbSelectName, constraints);
    panel3.add(lbSelectName);
    // setup Name combobox as search key
    cbName3 = new JComboBox();
    cbName3.setFont(labelFont);
    constraints.ipady = 10;
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(cbName3, constraints);
    panel3.add(cbName3);
    // setup search combobox (Name and corresponding key)
    cbName3.addItem ("Choose one:");
    cbPersonId3.addItem("0");
    // setup INSERT button in display area
    JButton btInsert = new JButton("Insert");
    btInsert.setFont(buttonFont);
//    btInsert.addActionListener(new ButtonListener());
    btInsert.setMnemonic(KeyEvent.VK_S);
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(btInsert, constraints);
    panel3.add(btInsert);
    // setup CLEAR button in display area
    JButton btClear = new JButton("Clear");
    btClear.setFont(buttonFont);
//    btClear.addActionListener(new ButtonListener());
    btClear.setMnemonic(KeyEvent.VK_C);
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(btClear, constraints);
    panel3.add(btClear);
    // setup First Name label in display area
    lbFirstName = new JLabel("First Name: ");
    lbFirstName.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbFirstName, constraints);
    panel3.add(lbFirstName);
    // setup First Name input field in display area
    tfFirstName3.setFont(labelFont);
    tfFirstName3.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfFirstName3, constraints);
    panel3.add(tfFirstName3);
    // setup Last Name label in display area
    lbLastName = new JLabel("Last Name: ");
    lbLastName.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbLastName, constraints);
    panel3.add(lbLastName);
    // setup Last Name input field in display area
    tfLastName3.setFont(labelFont);
    tfLastName3.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfLastName3, constraints);
    panel3.add(tfLastName3);
    // setup Address label in display area
    lbAddress = new JLabel("Address: ");
    lbAddress.setFont(labelFont);
    constraints.gridwidth = 1;
    constraints.anchor = GridBagConstraints.EAST;
    layout.setConstraints(lbAddress, constraints);
    panel3.add(lbAddress);
    // setup Address input field in display area
    tfAddress3.setFont(labelFont);
    tfAddress3.setEditable(true);
    constraints.gridwidth = constraints.REMAINDER;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.weightx = 1.0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    layout.setConstraints(tfAddress3, constraints);
    panel3.add(tfAddress3);
    // setup City label in display area
    lbCity = new JLabel("City: ");
    lbCity.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    constraints.fill = GridBagConstraints.NONE;
    layout.setConstraints(lbCity, constraints);
    panel3.add(lbCity);
    // setup City input field in display area
    tfCity3.setFont(labelFont);
    tfCity3.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfCity3, constraints);
    panel3.add(tfCity3);
    // setup State label in display area
    lbState = new JLabel("State: ");
    lbState.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbState, constraints);
    panel3.add(lbState);
    // setup State input field in display area
    tfState3.setFont(labelFont);
    tfState3.setEnabled(true);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfState3, constraints);
    panel3.add(tfState3);
    // setup Postcode label in display area
    lbPostcode = new JLabel("Postcode: ");
    lbPostcode.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbPostcode, constraints);
    panel3.add(lbPostcode);
    // setup Address input field in display area
    tfPostcode3.setFont(labelFont);
    tfPostcode3.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfPostcode3, constraints);
    panel3.add(tfPostcode3);
    // setup Country label in display area
    lbCountry = new JLabel("Country: ");
    lbCountry.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbCountry, constraints);
    panel3.add(lbCountry);
    // setup Country input field in display area
    tfCountry3.setFont(labelFont);
    tfCountry3.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfCountry3, constraints);
    panel3.add(tfCountry3);
    // setup Email Address label in display area
    lbEmailAddress = new JLabel ("Email Address: ");
    lbEmailAddress.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbEmailAddress, constraints);
    panel3.add(lbEmailAddress);
    // setup Email Address input field in display area
    tfEmailAddress3.setFont(labelFont);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfEmailAddress3, constraints);
    panel3.add(tfEmailAddress3);
    // setup Home Phone Number label in display area
    lbHomeNumber = new JLabel("Home Phone No.: ");
    lbHomeNumber.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = constraints.RELATIVE;
    layout.setConstraints(lbHomeNumber, constraints);
    panel3.add(lbHomeNumber);
    // setup Home Phone Number input field in display area
    tfHomeNumber3.setFont(labelFont);
    tfHomeNumber3.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridwidth = constraints.REMAINDER;
    layout.setConstraints(tfHomeNumber3, constraints);
    panel3.add(tfHomeNumber3);
    // setup Fax Number label in display area
    lbFaxNumber = new JLabel("Fax No.: ");
    lbFaxNumber.setFont(labelFont);
    constraints.anchor = GridBagConstraints.EAST;
    constraints.gridwidth = 1;
    layout.setConstraints(lbFaxNumber, constraints);
    panel3.add(lbFaxNumber);
    // setup Fax Number input field in display area
    tfFaxNumber3.setFont(labelFont);
    tfFaxNumber3.setEditable(true);
    constraints.anchor = GridBagConstraints.WEST;
    layout.setConstraints(tfFaxNumber3, constraints);
    panel3.add(tfFaxNumber3);
  //-----------------------------End showpane3()---------------------------------->>
  //-----------------------------Start showPane4()-------------------------------->>
  void showPane4() {
//    qtm = new QueryTableModel();
//    JTable table = new JTable(qtm);
//    String query = "SELECT FirstName, LastName, HomePhone, " +
//                   "FROM Addresses ORDER By LastName";
//    qtm.setQuery(query);
    JPanel demoPanel = new JPanel();
    demoPanel.setPreferredSize(new Dimension(400,500));
    demoPanel.setBackground(Color.pink);
    demoPanel.add(new JLabel("Demo Panel in place of table",
                              SwingConstants.CENTER));
    JScrollPane scrollPane = new JScrollPane(demoPanel);
    panel4 = new JPanel();
//    pane_number = 4;
    panel4.setLayout(layout);
    constraints.ipady = 0;
//    labelFont = new Font("Arial", Font.PLAIN, 12);   defined above
    buttonFont = new Font("Arial", Font.PLAIN, 13);
   // set screen border
   panel4.setBorder(
     BorderFactory.createTitledBorder(
       BorderFactory.createEtchedBorder(), ""));
   // add panel to tabbed pane
   tpane.addTab(tab4, panel4);
   JLabel lbPrint = new JLabel("Print");
   lbPrint.setFont(labelFont);
   constraints.anchor = constraints.EAST;
   layout.setConstraints(lbPrint, constraints);
   panel4.add(lbPrint);
   JButton btPrint = new JButton("Print");
   btPrint.setFont(buttonFont);
//   btPrint.addActionListener(new ButtonListener());
   btPrint.setMnemonic(KeyEvent.VK_P);
   constraints.anchor = constraints.WEST;
   constraints.gridwidth = constraints.REMAINDER;
   layout.setConstraints(btPrint, constraints);
   panel4.add(btPrint);
   constraints.weighty = 1.0;
   constraints.fill = constraints.BOTH;
   constraints.anchor = constraints.CENTER;
   layout.setConstraints(scrollPane, constraints);
   panel4.add(scrollPane);
  public static void main(String[] args) {
    new andy();
}

Similar Messages

  • Advance level drawing problem with Jframe and JPanel need optimize sol?

    Dear Experts,
    I m trying to create a GUI for puzzle game following some kind of "game GUI template", but i have problems in that,so i tried to implement that in various ways after looking on internet and discussions about drawing gui in swing, but i have problem with both of these, may be i m doing some silly mistake, which is still out of my consideration. please have a look at these two and recommend me one of them, which is running without problems (flickring and when you enlarge window the board draw copies (tiled) everywhere,
    Note: i don't want to inherit jpanel or Jframe
    here is my code : import java.awt.BorderLayout;
    public class GameMain extends JFrame {
         private static final long serialVersionUID = 1L;
         public int mX, mY;
         int localpoints = 0;
         protected static JTextField[][] squares;
         protected JLabel statusLabel = new JLabel("jugno");
         Label lbl_score = new Label("score");
         Label lbl_scorelocal = new Label("local score");
         protected static TTTService remoteTTTBoard;
         // Define constants for the game
         static final int CANVAS_WIDTH = 800; // width and height of the game screen
         static final int CANVAS_HEIGHT = 600;
         static final int UPDATE_RATE = 4; // number of game update per second
         static State state; // current state of the game
         private int mState;
         // Handle for the custom drawing panel
         private GameCanvas canvas;
         // Constructor to initialize the UI components and game objects
         public GameMain() {
              // Initialize the game objects
              gameInit();
              // UI components
              canvas = new GameCanvas();
              canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
              this.setContentPane(canvas);
              this.setDefaultCloseOperation(EXIT_ON_CLOSE);
              this.pack();
              this.setTitle("MY GAME");
              this.setVisible(true);
         public void gameInit() {     
         // Shutdown the game, clean up code that runs only once.
         public void gameShutdown() {
         // To start and re-start the game.
         public void gameStart() {
         private void gameLoop() {
         public void keyPressed(KeyEvent e) {
         public void keyTyped(KeyEvent e) {
         public void gameKeyReleased(KeyEvent e) {
              PuzzleBoard bd = getBoard();
              for (int row = 0; row < 4; ++row) {
                   for (int col = 0; col < 4; ++col) {
                        if (e.getSource() == squares[row][col]) {
                             if (bd.isOpen(col, row)) {
                                  lbl_score.setText("Highest Score = "
                                            + Integer.toString(bd.getPoints()));
                                  setStatus1(bd);
                                  pickSquare1(col, row, squares[row][col].getText()
                                            .charAt(0));
         protected void pickSquare1(int col, int row, char c) {
              try {
                   remoteTTTBoard.pick(col, row, c);
              } catch (RemoteException e) {
                   System.out.println("Exception: " + e.getMessage());
                   e.printStackTrace();
                   System.exit(1);
         // method "called" by remote object to update the state of the game
         public void updateBoard(PuzzleBoard new_board) throws RemoteException {
              String s1;
              for (int row = 0; row < 4; ++row) {
                   for (int col = 0; col < 4; ++col) {
                        squares[row][col].setText(new_board.ownerStr(col, row));
              lbl_score.setText("Highest Score = "
                        + Integer.toString(new_board.getPoints()));
              setStatus1(new_board);
         protected void setStatus1(PuzzleBoard bd) {
              boolean locals = bd.getHave_winner();
              System.out.println("local win" + locals);
              if (locals == true) {
                   localpoints++;
                   System.out.println("in condition " + locals);
                   lbl_scorelocal.setText("Your Score = " + localpoints);
              lbl_score
                        .setText("Highest Score = " + Integer.toString(bd.getPoints()));
         protected PuzzleBoard getBoard() {
              PuzzleBoard res = null;
              try {
                   res = remoteTTTBoard.getState();
              } catch (RemoteException e) {
                   System.out.println("Exception: " + e.getMessage());
                   e.printStackTrace();
                   System.exit(1);
              return res;
         /** Custom drawing panel (designed as an inner class). */
         class GameCanvas extends JPanel implements KeyListener {
              /** Custom drawing codes */
              @Override
              public void paintComponent(Graphics g) {
                   // setOpaque(false);
                   super.paintComponent(g);
                   // main box; everything placed in this
                   // JPanel box = new JPanel();
                   setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
                   // add(statusLabel, BorderLayout.NORTH);
                   // set up the x's and o's
                   JPanel xs_and_os = new JPanel();
                   xs_and_os.setLayout(new GridLayout(5, 5, 0, 0));
                   squares = new JTextField[5][5];
                   for (int row = 0; row < 5; ++row) {
                        for (int col = 0; col < 5; ++col) {
                             squares[row][col] = new JTextField(1);
                             squares[row][col].addKeyListener(this);
                             if ((row == 0 && col == 1) || (row == 2 && col == 3)
                             || (row == 1 && col == 4) || (row == 4 && col == 4)
                                       || (row == 4 && col == 0))
                                  JPanel p = new JPanel(new BorderLayout());
                                  JLabel label;
                                  if (row == 0 && col == 1) {
                                       label = new JLabel("1");
                                       label.setHorizontalAlignment(JLabel.LEFT);
                                       label.setVerticalAlignment(JLabel.TOP);
                                  else if (row == 4 && col == 0) {// for two numbers or
                                       // two
                                       // blank box in on row
                                       label = new JLabel("2");
                                       label.setHorizontalAlignment(JLabel.LEFT);
                                       label.setVerticalAlignment(JLabel.TOP);
                                  else if (row == 1 && col == 4) {
                                       label = new JLabel("3");
                                       label.setHorizontalAlignment(JLabel.LEFT);
                                       label.setVerticalAlignment(JLabel.TOP);
                                  else if (row == 4) {
                                       label = new JLabel("4");
                                       label.setHorizontalAlignment(JLabel.LEFT);
                                       label.setVerticalAlignment(JLabel.TOP);
                                  else {
                                       label = new JLabel("5");
                                       label.setHorizontalAlignment(JLabel.LEFT);
                                       label.setVerticalAlignment(JLabel.TOP);
                                  label.setOpaque(true);
                                  label.setBackground(squares[row][col].getBackground());
                                  label.setPreferredSize(new Dimension(label
                                            .getPreferredSize().width, squares[row][col]
                                            .getPreferredSize().height));
                                  p.setBorder(squares[row][col].getBorder());
                                  squares[row][col].setBorder(null);
                                  p.add(label, BorderLayout.WEST);
                                  p.add(squares[row][col], BorderLayout.CENTER);
                                  xs_and_os.add(p);
                             } else if ((row == 2 && col == 1) || (row == 1 && col == 2)
                                       || (row == 3 && col == 3) || (row == 0 && col == 3)) {
                                  xs_and_os.add(squares[row][col]);
                                  // board[ row ][ col ].setEditable(false);
                                  // board[ row ][ col ].setText("");
                                  squares[row][col].setBackground(Color.RED);
                                  squares[row][col].addKeyListener(this);
                             } else {
                                  squares[row][col] = new JTextField(1);
                                  // squares[row][col].addActionListener(this);
                                  squares[row][col].addKeyListener(this);
                                  xs_and_os.add(squares[row][col]);
                   this.add(xs_and_os);
                   this.add(statusLabel);
                   this.add(lbl_score);
                   this.add(lbl_scorelocal);
              public void keyPressed(KeyEvent e) {
              public void keyReleased(KeyEvent e) {
                   gameKeyReleased(e);
              public void keyTyped(KeyEvent e) {
         // main
         public static void main(String[] args) {
              SwingUtilities.invokeLater(new Runnable() {
                   @Override
                   public void run() {
                        new GameMain();
      thanks a lot for your time , consideration and efforts.
    jibby
    Edited by: jibbylala on Sep 20, 2010 6:06 PM

    jibbylala wrote:
    thanks for mentioning as i wasn't able to write complete context here.Yep thanks camickr. I think that Darryl's succinct reply applies here as well.

  • Problem with JTable and JPanel

    Hi,
    I'm having problems with a JTable in a JPanel. The code is basicly as follows:
    public class mainFrame extends JFrame
            public mainFrame()
                    //A menu is implemeted giving rise to the following actions:
                    public void actionPerformed(ActionEvent evt)
                            String arg = evt.getActionCommand();
                            if(arg.equals("Sit1"))
                            //cells, columnNames are initiated correctly
                            JTable table = new JTable(cells,columnNames);
                            JPanel holdingPanel = new JPanel();
                            holdingPanel.setLayout( new BorderLayout() );
                            JScrollPane scrollPane = new JScrollPane(holdingPanel);
                            holdingPanel.setBackground(Color.white);
                            holdingPanel.add(table,BorderLayout.CENTER);
                            add(scrollPane, "Center");
                            if(arg.equals("Sit2"))
                                    if(scrollPane !=null)
                                            remove(scrollPane);validate();System.out.println("ScrollPane");
                                    if(holdingPanel !=null)
                                            remove(holdingPanel);
                                    if(table !=null)
                                            remove(table);table.setVisible(false);System.out.println("table");
                            //Put other things on the holdingPanel....
            private JScrollPane scrollPane;
            private JPanel holdingPanel;
            private JTable table;
    }The problem is that the table isn't removed. When you choose another situation ( say Sit2), at first the table apparently is gone, but when you press with the mouse in the area it appeared earlier, it appears again. How do I succesfully remove the table from the panel? Removing the panel doesn't seem to be enough. Help is much appreciated...
    Best regards
    Schwartz

    If you reuse the panel and scroll pane throughout the application,
    instantiate them in the constructor, not in an often-called event
    handler. In the event handler, you only do add/remove of the table
    on to the panel. You can't remove the table from the container
    which does not directly contain it.
    if (arg.equals("Sit2")){
      holdingPanel.remove(table);
      holdingPanel.revalidate();
      holdingPanel.repaint(); //sometimes necessary
    }

  • Problems with GridBagLayout

    Hello all,
    I have to implement a JPanel with several input items and use the GridBagLayout manager. My problem is, that the size of some text items won't be set to the preferred size (they are only 1px width as long as I don't set the attribute "fill" to another value than "none").
    The Grid looks like this:
    c1 c2 c3 c4
    r1 label1 textitem1 label2 textitem2
    r2 label3 textarea1 textarea1 textarea1 -> span 3
    r3 label4 textarea2 textarea2 textarea2 -> span 3
    r4 label5 textitem3 -> span 1
    All constraints for the used item are the same except gridwidth, gridx and gridy. I already tried to set the widthx property to several values but that didn't work either.
    Has anybody any ideas?
    Thanks for your efforts and best regards,
    Jens

    [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use GridBagLayout
    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the posted code retains its original formatting.

  • Problem with JFrame and JPanel

    Okay, well I'm busy doing a lodge management program for a project and I have programmed this JFrame
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class FinalTest extends JFrame
         public JPanel contentPane;
         public JImagePanel imgPanel;
         private JLabel[] cottageIcon;
         private boolean keepMoving;
         private int selectedCottage;
         public FinalTest()
              super();
              initializeComponent();
              addActionListeners();
              this.setVisible(true);
         private void initializeComponent()
              contentPane = (JPanel)getContentPane();
              contentPane.setLayout(null);
              imgPanel = new JImagePanel("back.png");
               imgPanel.setLayout(null);
              imgPanel.setBackground(new Color(1, 0, 0));
                   addComponent(contentPane, imgPanel, 10,10,imgPanel.getImageWidth(),imgPanel.getImageHeight());
              cottageIcon = new JLabel[6];
              keepMoving = true;
              selectedCottage = 0;
              cottageIcon[0] =  new JLabel();
              //This component will never be added or shown, but needs to be there to cover for no cottage selected
              for(int a = 1; a < cottageIcon.length; a++)
                   cottageIcon[a] = new JLabel("C" + (a));
                   cottageIcon[a].setBackground(new Color(255, 0, 0));
                    cottageIcon[a].setHorizontalAlignment(SwingConstants.CENTER);
                    cottageIcon[a].setHorizontalTextPosition(SwingConstants.LEADING);
                    cottageIcon[a].setForeground(new Color(255, 255, 255));
                    cottageIcon[a].setOpaque(true);
                    addComponent(imgPanel,cottageIcon[a],12,(a-1)*35 + 12,30,30);
                this.setTitle("Cottage Chooser");
                this.setLocationRelativeTo(null);
              this.setSize(new Dimension(540, 430));
         private void addActionListeners()
              imgPanel.addMouseListener(new MouseAdapter()
                   public void mousePressed(MouseEvent e)
                        imgPanel_mousePressed(e);
                   public void mouseReleased(MouseEvent e)
                        imgPanel_mouseReleased(e);
                   public void mouseEntered(MouseEvent e)
                        imgPanel_mouseEntered(e);
              imgPanel.addMouseMotionListener(new MouseMotionAdapter()
                   public void mouseDragged(MouseEvent e)
                        imgPanel_mouseDragged(e);
         private void addComponent(Container container,Component c,int x,int y,int width,int height)
              c.setBounds(x,y,width,height);
              container.add(c);
         private void imgPanel_mousePressed(MouseEvent e)
              for(int a = 1; a < cottageIcon.length; a++)
                   if(withinBounds(e.getX(),e.getY(),cottageIcon[a].getBounds()))
                        System.out.println("B" + withinBounds(e.getX(),e.getY(),cottageIcon[a].getBounds()));
                        selectedCottage = a;
                        keepMoving = true;
         private void imgPanel_mouseReleased(MouseEvent e)
              System.out.println("called");
              selectedCottage = 0;
              keepMoving = false;
         private void imgPanel_mouseDragged(MouseEvent e)
               System.out.println("XXX" + Math.random() * 100);
              if(keepMoving)
                   int x = e.getX();
                    int y = e.getY();
                    if(selectedCottage!= 0)
                         cottageIcon[selectedCottage].setBounds(x-(30/2),y-(30/2),30,30);
                    if(!legalBounds(imgPanel,cottageIcon[selectedCottage]))
                        keepMoving = false;
                        cottageIcon[selectedCottage].setBounds(imgPanel.getWidth()/2,imgPanel.getHeight()/2,30,30);
              System.out.println(cottageIcon[selectedCottage].getBounds());
         private void imgPanel_mouseEntered(MouseEvent e)
               System.out.println("entered");
         private void but1_actionPerformed(ActionEvent e)
              String input = JOptionPane.showInputDialog(null,"Enter selected cottage");
              selectedCottage = Integer.parseInt(input) - 1;
         public boolean legalBounds(Component containerComponent, Component subComponent)
              int contWidth = containerComponent.getWidth();
              int contHeight = containerComponent.getHeight();
              int subComponentX = subComponent.getX();
              int subComponentY = subComponent.getY();
              int subComponentWidth = subComponent.getWidth();
              int subComponentHeight = subComponent.getHeight();
              if((subComponentX < 0) || (subComponentY < 0) || (subComponentX > contWidth) || (subComponentY > contHeight))
                   return false;
              return true;
         public boolean withinBounds(int mouseX, int mouseY, Rectangle componentRectangle)
              int componentX = (int)componentRectangle.getX();
              int componentY = (int)componentRectangle.getY();
              int componentHeight = (int)componentRectangle.getHeight();
              int componentWidth = (int)componentRectangle.getWidth();
              if((mouseX >= componentX) && (mouseX <= (componentX + componentWidth)) && (mouseY >= componentY) && (mouseY <= (componentY + componentWidth)))
                   return true;
              return false;
         public static void main(String[] args)
              JFrame.setDefaultLookAndFeelDecorated(true);
              //JDialog.setDefaultLookAndFeelDecorated(true);
              try
                   UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
              catch (Exception ex)
                   System.out.println("Failed loading L&F: ");
                   System.out.println(ex);
              FinalTest ft = new FinalTest();
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class JImagePanel extends JPanel
      private Image image;
      public JImagePanel(String imgFileName)
           image = new ImageIcon(imgFileName).getImage();
        setLayout(null);
      public void paintComponent(Graphics g)
        g.drawImage(image, 0, 0, null);
      public Dimension getImageSize()
                Dimension size = new Dimension(image.getWidth(null), image.getHeight(null));
             return size;
      public int getImageWidth()
                int width = image.getWidth(null);
                return width;
      public int getImageHeight()
              int height = image.getHeight(null);
              return height;
    }Now the problem I'm having is changing that class to a JFrame, it seems simple but I keep having problems like when it runs from another JFrame nothing pops up. I can do it like this:
    FinalTest ft = new FinalTest();
    ft.setVisible(false);
    JPanel example = ft.contentPanehowever I will probably be marked down on this for bad code. I'm not asking for the work to be done for me, but I'm really stuck on this and just need some pointers so I can carry on with the project. Thanks,
    Steve

    CeciNEstPasUnProgrammeur wrote:
    I'd actually consider your GUI being a JPanel instead of a JFrame quite good design - makes it easy to put the stuff into an applet when necessary...
    Anyway, you should set setVisible() to true to make it appear, not to false. Otherwise, I don't seem to understand your problem.That is actually my problem. I am trying to convert this JFrame to a JPanel

  • Problems with basic Java JPanel animation using paintcomponent

    okay so I am trying to animate a sprite moving across a background using a for loop within the paintComponent method of a JPanel extension
    public void paintComponent(Graphics g){
    Image simpleBG = new ImageIcon("Images/Backgrounds/Simple path.jpg").getImage();
    Image mMan = new ImageIcon("Images/Sprites/hMan.gif").getImage();
    if (!backgroundIsDrawn){
    g.drawImage(simpleBG, 0, 0, this);
    g.drawImage(mMan, xi, y, this);
    backgroundIsDrawn=true;
    else
    for (int i=xi; i<500; i++){
    g.drawImage(simpleBG, 0, 0, this);
    g.drawImage(mMan, i, y, this);
    try{
    Thread.sleep(10);
    } catch(Exception ex){System.out.println("damn");}
    The problem is that no matter what I set my thread to, it will not show the animation, it will just jump to the last location, even if the thread sleep is set high enough that it takes several minutes to calculate,, it still does not paint the intemediary steps.
    any solution, including using a different method of animation all together would be greatly appreciated. I am learning java on my own so i need all the help I can get.

    fysicsandpholds1014 wrote:
    even when I placed the thread sleep outside of the actual paintComponent in a for loop that called repaint(), it still did not work? Nope. Doing this will likely put the Event Dispatch Thread or EDT to sleep. Since this is the main thread that is responsible for Swing's painting and user interaction, you'll put your app to sleep with this. Again, use a Swing Timer
    and is there any easy animation method that doesnt require painting and repainting every time becasue it is very hard to change what I want to animate with a single paintComponent method? I don't get this.
    I find the internet tutorials either way too complicated or not nearly in depth enough. Maybe its just that I am new to programming but there doesn't seem to be a happy medium between dumbed down Swing tutorials and very complicated and sophisticated animation algorithmns.I can only advise that you practice, practice, practice. The more you use the tutorials, the easier they'll be to use.
    Here's another small demo or animation. It is not optimized (repaint is called on the whole JPanel), but demonstrates some points:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    @SuppressWarnings("serial")
    public class AnimationDemo2 extends JPanel {
      // use a publicly available sprite for this example
      private static final String SPRITE_PATH = "http://upload.wikimedia.org/" +
                "wikipedia/commons/2/2e/FreeCol_flyingDutchman.png";
      private static final int SIDE = 600;
      private static final Dimension APP_SIZE = new Dimension(SIDE, SIDE);
      // timer delay: equivalent to the Thread.sleep time
      private static final int TIMER_DELAY = 20;
      // how far to move in x dimension with each loop of timer
      public static final int DELTA_X = 1;
      // holds our sprite image
      private BufferedImage img = null;
      // the images x & y locations
      private int imageX = 0;
      private int imageY = SIDE/2;
      public AnimationDemo2() {
        setPreferredSize(APP_SIZE);
        try {
          // first get our image
          URL url = new URL(SPRITE_PATH);
          img = ImageIO.read(url);
          imageY -= 3*img.getHeight()/4;
        } catch (Exception e) { // shame on me for combining exceptions :(
          e.printStackTrace();
          System.exit(1); // exit if fails
        // create and start the Swing Timer
        new Timer(TIMER_DELAY, new TimerListener()).start();
      // all paintComponent does is paint -- nothing else
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.blue);
        int w = getWidth();
        int h = getHeight();
        g.fillRect(0, h/2, w, h);
        if (img != null) {
          g.drawImage(img, imageX, imageY, this);
      private class TimerListener implements ActionListener {
        // is called every TIMER_DELAY ms
        public void actionPerformed(ActionEvent e) {
          imageX += DELTA_X;
          if (imageX > getWidth()) {
            imageX = 0;
          // ask JVM to paint this JPanel
          AnimationDemo2.this.repaint();
      // code to place our JPanel into a JFrame and show it
      private static void createAndShowUI() {
        JFrame frame = new JFrame("Animation Demo");
        frame.getContentPane().add(new AnimationDemo2());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      // call Swing code in a thread-safe manner
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
    }

  • Problem with multiple transparent JPanels

    Hi,
    I'm currently trying to implement some kind of message box that can fade in and out. This message box is a subclass of JComponent, the message it contains can be an arbitrary Swing-Component.
    The message container and the message itself have the same background color. When fading, the (partly transparent) colors of the of the container and the message are added, which I do not want.
    Here are two images to illustrate my problem:
    What I have: http://www.inf.tu-dresden.de/~ab023578/javaforum/reality.gif
    What I want: http://www.inf.tu-dresden.de/~ab023578/javaforum/wish.gif
    Here is the code:
    import java.awt.*;
    import javax.swing.*;
    public class Main {
        static float transparency = 0f;
        public static void main(String[] args) {
            JPanel jpBack = new JPanel() {
                protected void paintComponent(Graphics g) {
                    Graphics2D g2d = (Graphics2D)g;
                    g2d.clearRect(0, 0, getWidth(), getHeight());
                    g2d.setColor(getBackground());
                    AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency);
                    g2d.setComposite(alpha);
                    g2d.fillRect(0, 0, getWidth(), getHeight());
            jpBack.setBackground(Color.WHITE);
            jpBack.setLayout(null);
            JPanel jpContainer = new JPanel();
            jpContainer.setBackground(Color.RED);
            jpContainer.setLayout(null);
            JPanel jpMessage = new JPanel();
            jpMessage.setBackground(Color.RED);
            JLabel jlMessage = new JLabel("MESSAGE");
            jpBack.add(jpContainer);
            jpContainer.add(jpMessage);
            jpMessage.add(jlMessage);
            jpContainer.setBounds(10, 10, 120, 100);
            jpMessage.setBounds(10, 10, 100, 50);
            JFrame frame = new JFrame();
            frame.setBounds(0, 0, 150, 150);
            frame.setBackground(Color.WHITE);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(jpBack);
            frame.setVisible(true);
            for (double a = 0; true; a += 0.01D) {
                try {
                    Thread.sleep(10);
                catch (InterruptedException e) {}
                transparency = (float)Math.abs(Math.sin(a));
                jpBack.repaint();
    }I understand that the Porter-Duff-Rule SRC_OVER causes the container to be drawn over the message (or vice versa) and both of them over the background. I think what I need is a way to handle the whole jpContainer-Panel and all its subcomponents as a single source image. Is that possible?

    Thank you for your answer, Sarcommand. Unfortunately the test program I provided is a bit too simple as your solution works here but not for my original problem. :|
    Let me explain what I want to achieve and why.
    I'm writing a tool for algorithm visualisation. Currently I am dealing with a parsing algorithm (having compontents such as an automaton with an input tape, output tape and a stack). Those components (containers) can contain multiple JComponents (GraphicalObjects), which display a BufferedImage that can be altered during the animation.
    I want to create my message windows the same way. A container that contains GraphicalObjects (or, if necessary, any other Swing component). One GraphicalObject has a very limited field of responsibility: mereley displaying its information.
    Aligning the GraphicalObject is one of the container's responsibilities.
    What I'm currently trying to do is to put my GraphicalMessage into the center of a container, leaving an offset on the left/right/upper/lower side. This is why I need the same background color for both the GraphicalMessage and the MessageContainer, I don't want the user to see that there are actually two JComponents on top of each other.
    While I could use another approach for text messages I prefer this one as I would be able to fade arbitrary Containers and their GraphicalObjects .
    Here is the code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.image.BufferedImage;
    public class Main {
        static float transparency = 0f;
        public static void main(String[] args) {
            JPanel jpBack = new JPanel() {
                protected void paintComponent(Graphics g) {
                    Graphics2D g2d = (Graphics2D)g;
                    g2d.clearRect(0, 0, getWidth(), getHeight());
                    g2d.setColor(getBackground());
                    AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency);
                    g2d.setComposite(alpha);
                    g2d.fillRect(0, 0, getWidth(), getHeight());
                    super.paintComponent(g);
            jpBack.setBackground(Color.WHITE);
            jpBack.setLayout(null);
            JPanel jpContainer = new JPanel();
            jpContainer.setBackground(Color.RED);
            jpContainer.setLayout(null);
            GraphicalMessage msg = new GraphicalMessage();
            jpBack.add(jpContainer);
            jpContainer.add(msg);
            jpContainer.setBounds(10, 10, 120, 100);
            msg.setBounds(10, 10, 160, 60);
            JFrame frame = new JFrame();
            frame.setBounds(0, 0, 150, 150);
            frame.setBackground(Color.WHITE);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(jpBack);
            frame.setVisible(true);
            float stepSize = 0.01f;
            float step = stepSize;
            for (float a = 0; true; a += step) {
                try {
                    Thread.sleep(10);
                catch (InterruptedException e) {}
                if (a >= 1) {
                    step = -stepSize;
                    a -= stepSize;
                if (a <= 0) {
                    step = stepSize;
                    a += stepSize;
                    msg.updateInternalContent();
                    msg.redraw();
                transparency = a;
                jpBack.repaint();
    class GraphicalMessage extends JComponent {
        private String text;
        private Graphics2D ig2d;
        private BufferedImage image;
        public GraphicalMessage() {
            setLayout(null);
            image = new BufferedImage(100, 50, BufferedImage.TYPE_INT_ARGB);
            ig2d = image.createGraphics();
            ig2d.setColor(Color.BLACK);
            ig2d.setBackground(Color.RED);
            ig2d.setFont(new Font("Courier New", Font.BOLD, 20));
            updateInternalContent();
            redraw();
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(image, 0, 0, null);
        public void redraw() {
            ig2d.clearRect(0, 0, image.getWidth(), image.getHeight());
            ig2d.drawString(text, 0, 20);
        public void updateInternalContent() {
            text = String.valueOf((int)(Math.random() * 10000));
    }A possibility to erase the text from the BufferedImage without having to draw a rectangle would help me as well.

  • I have a serious problem with GridBagLayout please help

    I have an working code of an Swing application that has used gridbaglayout it works very well in desktop pc on 2000/xp with 800 x 600 screen resolution but the problem is when i change the screen resolution to higher say at 1152 x 864 resolution then the same code all the components are left align and not properly distributed even though i have GridbagConstraints.BOTH for the fill variable of the Gridbaglayout. Also i am facing similar problem when i run the code on the laptop.
    Please help me and can anyone tell me what to do in case i want the layout to show in the same manner for higher resolution as it is showing for lower resolution.

    you may have to setPreferredSize to a calulation from the screen resolution, if there is 1 thing that is 1 pixel too big for it, i find it stuffs the entire container
    Drogo

  • Problem with Graphics and JPanel...

    I would like to draw a rectangle on a JPanel. Here is my code:
    JPanel panel = new JPanel();
    Graphics g = panel.getGraphics();
    g.drawRect(20,20,20,20);
    When I run my program, I have a NullPointerException...
    Do you any suggestions to fix the problem??
    thanks
    Pete

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    public class pete extends JFrame {
      public pete() {
        JPanel panel = new JPanel() {
          public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int midx = getSize().width / 2;
            int midy = getSize().height / 2;
            g2.draw(new Rectangle2D.Double(midx/2, midy/2, midx, midy));
        getContentPane().add(panel, "Center");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,300);
        setLocation(300,100);
        setVisible(true);
      public static void main(String[] args) {
        new pete();
    }

  • Problem with repaint() in JPanel

    Hi,
    This is the problem: I cyclically call the repaint()-method but there is no effect of it.
    When does it appear: The problem occurs by calling the repaint()-method of a JPanel -class.
    This is what i am doing: The repaint() is called from a different class which is my GUI. I do this call in an endless loop which is done within a Thread.
    I tried to add a KeyListener to the JPanel-class and there I called the repaint()-method when i press a Key. Then the Panel is repainted.
    so I implemented a "callRepaint"-method in the JPanel-class that does nothing else than call repaint() (just like the keyPressed()-method does). But when i cyclically call this "callRepaint"-method from my GUI nothing happens.
    Now a few codedetails:
    // JPanel-class contains:
    int i = 0;
    public void callRepaint(){
                    repaint();
    public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawLine(i++,0,200,200);
    public void keyPressed(KeyEvent e) {
            repaint();                       // This is working
    //GUI-class contains:
    // This method is called cyclically
    public void draw() {
                  lnkJPanelclass.repaint();             // This calling didn't work
                  // lnkJPanelclass.callRepaint();  // This calling didn't work     
    Thanks for your advices in advance!
    Cheers spike

    @ARafique:
    This works fine:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Date;
    import javax.swing.*;
    public class Test extends JFrame implements ActionListener{
        private JTextField label;
        private Timer timer;
        private Container cont;
        public Test() {
            label = new JTextField("",0);
            timer = new Timer(1000,this);
            cont = getContentPane();
            cont.add(label);
            setSize(250,70);
            setDefaultCloseOperation(3);
            setVisible(true);
            timer.start();
        public void actionPerformed(ActionEvent e){
            label.setText(new Date(System.currentTimeMillis()).toString());
        public static void main(String[] args){
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new Test();
    }no repaint()/revalidate() needed

  • Problem with JPanel.updateUI()

    Hello,
    i have a problem with updateUI(), here's a simple example what's wrong. just compile and run the following piece of code and look what happens after 2 seconds: the left panel will be decrease its width after calling updateUI(), but why? is it a bug of updateUI() or is it my fault?
    import java.awt.*;
    import javax.swing.*;
    public class Test extends JPanel
            private JList m_list;
            private DefaultListModel m_listModel;
            private JPanel m_buttons;
            public static GridBagConstraints creategbc (int x, int y, int w, int h, int wx, int wy, int f)
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridx = x;
                    gbc.gridy = y;
                    gbc.gridwidth = w;
                    gbc.gridheight = h;
                    gbc.weightx = wx;
                    gbc.weighty = wy;
                    gbc.fill = f;
                    gbc.insets = new Insets(5, 5, 5, 5); // kleinen Rahmen ziehen
                    return gbc;
            public Test ()
                    GridBagLayout gbl = new GridBagLayout();
                    setLayout(gbl);
                    GridBagConstraints gbc;
                    initButtons();
                    gbc = creategbc(0, 0, 1, 1, 20, 100, GridBagConstraints.NONE);
                    gbc.anchor = GridBagConstraints.NORTH;
                    gbl.setConstraints(m_buttons, gbc);
                    add(m_buttons);
                    initList();
                    JScrollPane sp = new JScrollPane(m_list);
                    gbc = creategbc(1, 0, 1, 1, 100, 100, GridBagConstraints.BOTH);
                    gbl.setConstraints(sp, gbc);
                    add(sp);
            public void addItem (String item)
                    m_listModel.addElement(item);
            public void initList ()
                    m_listModel = new DefaultListModel();
                    m_list = new JList(m_listModel);
            public void initButtons ()
                    m_buttons = new JPanel();
                    m_buttons.setLayout(new GridLayout(4, 1, 0, 20));
                    m_buttons.add(new JButton("Neu"));
                    m_buttons.add(new JButton("Bearbeiten"));
                    m_buttons.add(new JButton("L�schen"));
                    m_buttons.add(new JButton("Abfrage"));
            public static void main (String[] args)
                    JFrame f = new JFrame();
                    Test t = new Test();
                    f.setContentPane(t);
                    f.setSize(600, 450);
                    f.setVisible(true);
                    try
                            Thread.sleep(2000);
                    catch (Exception e)
                    t.addItem("Hallo");
                    t.updateUI();

    Hello,
    i have a problem with updateUI(), here's a simple
    example what's wrong. just compile and run the
    following piece of code and look what happens after 2
    seconds: the left panel will be decrease its width
    after calling updateUI(), but why? is it a bug of
    updateUI() or is it my fault?updateUI() its called when the L&F is changed, there is
    rarely a case when you need to call updateUI().
    Why do you call updateUI() if you remove the call to updateUI()
    everything will work OK.
    merry xmas

  • Problem with jpanel and image

    i have a jinternalframe with gridbaglayout which has several jpanels. one of the jpanels has to display an image.
    i have searched through the forum and i have made a method to draw the image,
    but the problem is that when i added to the jpanel the last (jpanel) gets much bigger and as a result to break down the layout.
    any help is appreciated!

    i have searched through the forum and i have made a method to draw the image,Just add the image to a JLabel.
    when i added to the jpanel the last (jpanel) gets much bigger[url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Managers

  • Problem with the GridBagLayout Manager

    Hello i am new to Java Swing and i am facing a problem with the GridBagLayout layout manager . the code in question is attached. First compile and run the code. It will execute w/o probs . Then go to the "Console" tab. There the diff components (6 buttons and 1 text area) are haphazardly arranged where as all measures where taken to prevent it in the code. The GridBagLayout manager for this tab is not working properly please help.
    The code in question:-
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class MainForm extends JFrame{
         JTabbedPane jtp = new JTabbedPane();
         Container generalContainer; // container for the general pane
         Container consoleContainer; // container for the console pane
         GridBagLayout consoleLayout = new GridBagLayout(); // GridBagLayout for the console
         GridBagConstraints consoleConstraints;// GridBagConstraints for the console
         public MainForm()
              super("Welcome to Grey Griffin -- Network Simulator");
              setSize(700,600);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              JPanel generalPane = new JPanel();
              generalPane.setLayout(new BoxLayout(generalPane, BoxLayout.Y_AXIS));
              JPanel consolePane = new JPanel();
              consolePane.setLayout(new BoxLayout(consolePane, BoxLayout.Y_AXIS));
              JPanel designPane = new JPanel();
              designPane.setLayout(new BoxLayout(designPane, BoxLayout.Y_AXIS));
              JPanel outputPane = new JPanel();
              outputPane.setLayout(new BoxLayout(outputPane, BoxLayout.Y_AXIS));
              //Setting up Layout for all the tabs
               //for general tab
               FlowLayout layout= new FlowLayout();
               generalContainer = generalPane;
               layout.setAlignment(FlowLayout.CENTER);
               generalContainer.setLayout( layout );
               //for  console tab
               consoleContainer = consolePane;
               consoleConstraints = new GridBagConstraints();
               // *******Finished********
              //********** All buttons text areas are declared here**********
                //*******for the general tab**********
              JButton generalCreate = new JButton("Create a New Network");
              JButton generalOpen = new JButton("Open an Existing Network");
              JButton generalSave = new JButton("Save the Network");
              JButton generalSaveAs = new JButton("Save As..........");
              JButton generalExit = new JButton("Exit");
              //******END******
             //*******for the console tab
                 //text area          
              JTextArea consoleCode = new JTextArea();
              consoleCode.setEditable(true);
              consoleCode.setMaximumSize(new Dimension(700,400));
              consoleCode.setAlignmentX(0.0f);
                   //text area complete
                 //*******for the Console tab**********
              JButton consoleCompile = new JButton("Compile Code");
              JButton consoleSimulate = new JButton("Simulate Code");
              JButton consoleReset = new JButton("Reset");
              JButton consoleOpen = new JButton("Open script files");
              JButton consoleSave = new JButton("Save script files");
              JButton consoleConvert = new JButton("Convert Script files to graphical form");
              //***************END****************
         //Adding buttons and text areas to there respective tabs
              // for the general tab
              generalContainer.add(generalCreate);
              generalContainer.add(generalOpen);
              generalContainer.add(generalSave);
              generalContainer.add(generalSaveAs);
             generalContainer.add(generalExit);
             //****END****
              // for the console tab          
              consoleConstraints.fill = GridBagConstraints.BOTH;
              addComp(consoleOpen,0,0,1,1);
              consoleConstraints.fill = GridBagConstraints.BOTH;
              addComp(consoleSave,1,1,1,1);
              consoleConstraints.fill = GridBagConstraints.BOTH;
              addComp(consoleConvert,1,2,1,1);
              consoleConstraints.fill = GridBagConstraints.BOTH;
              addComp(consoleCode,1,0,3,1);
              consoleConstraints.fill = GridBagConstraints.BOTH;
              addComp(consoleCompile,2,0,1,1);
              consoleConstraints.fill = GridBagConstraints.BOTH;
              addComp(consoleSimulate,2,1,1,1);
              consoleConstraints.fill = GridBagConstraints.BOTH;
              addComp(consoleReset,2,2,1,1);
              //****END****
              // adding the tabs
              jtp.addTab("General",null,generalPane,"Choose General Options");
              jtp.addTab("Design",null,designPane,"Design your own network!!");
              jtp.addTab("Console",null,consolePane,"Type commands in console for designing");
              jtp.addTab("Output",null,outputPane,"View output");
              getContentPane().add(jtp, BorderLayout.CENTER);
              //****END****
         //This method is used to ad the buttons in the GridBagLayout of the Console tab
         private void addComp( Component c,int row,int column,int width,int height)
            // set gridx and gridy
            consoleConstraints.gridx=column;
            consoleConstraints.gridy=row;
            //set gridwidth and grid height
            consoleConstraints.gridwidth=width;
            consoleConstraints.gridheight=height;
            //set constraints
            consoleLayout.setConstraints(c,consoleConstraints);
            consoleContainer.add(c);     
         class TabManager implements ItemListener
              Component tab;
              public TabManager(Component tabToManage)
                   tab = tabToManage;
         public void itemStateChanged(ItemEvent ie)
              int index=jtp.indexOfComponent(tab);
             if (index!=-1)
                  jtp.setEnabledAt(index,ie.getStateChange()== ItemEvent.SELECTED);
             MainForm.this.repaint();
    public static void main(String args[])
         MainForm form = new MainForm();
         form.setVisible(true);
    }

    Thanks for the suggestions. I did away with the GridBagLayout Altogether :-D
    and put all the buttons in a seperate JPanel and added that JPanel into the Console tabs container which was using a BorderLayout this time. Take a look
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class MainForm extends JFrame{
         JTabbedPane jtp = new JTabbedPane();
         public MainForm()
              super("Welcome to Grey Griffin -- Network Simulator");
              setSize(800,600);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              JPanel generalPane = new JPanel();
              generalPane.setLayout(new BoxLayout(generalPane, BoxLayout.Y_AXIS));
              JPanel consolePane = new JPanel();
              consolePane.setLayout(new BoxLayout(consolePane, BoxLayout.Y_AXIS));
              JPanel designPane = new JPanel();
              designPane.setLayout(new BoxLayout(designPane, BoxLayout.Y_AXIS));
              JPanel outputPane = new JPanel();
              outputPane.setLayout(new BoxLayout(outputPane, BoxLayout.Y_AXIS));
              //Setting up Layout for all the tabs
              //**for the general tab
               Container generalContainer;
               FlowLayout layoutGeneral= new FlowLayout();
               generalContainer = generalPane;
               layoutGeneral.setAlignment(FlowLayout.CENTER);
               generalContainer.setLayout( layoutGeneral );
               //**for the console tab
                Container consoleContainer;
                consoleContainer = consolePane;
                consoleContainer.setLayout(new BorderLayout() );
               //Creating buttonpanel for adding the buttons
                JPanel buttonPanel1 = new JPanel();
                buttonPanel1.setLayout(new GridLayout(1,3));
                JPanel buttonPanel2 = new JPanel();
                buttonPanel2.setLayout(new GridLayout(1,3));
              // All buttons / text areas / images are declared here
              //**Buttons for the general tab**//
              JButton generalCreate = new JButton("Create a New Network");
              JButton generalOpen = new JButton("Open an Existing Network");
              JButton generalSave = new JButton("Save the Network");
              JButton generalSaveAs = new JButton("Save As..........");
              JButton generalExit = new JButton("Exit");
              //declaring the buttons
              JButton consoleCompile = new JButton("Compile");
              JButton consoleRun = new JButton("Run");
              JButton consoleReset = new JButton("Reset");
              JButton consoleOpen = new JButton("Open script files");
              JButton consoleSave = new JButton("Save script files");
              JButton consoleConvert = new JButton("Convert Script files to graphical form");
              //declares the textarea where the code is written
              JTextArea consoleCode = new JTextArea();
              consoleCode.setEditable(true);
              consoleCode.setMaximumSize(new Dimension(500,600));
              consoleCode.setAlignmentX(0.0f);
              //Adding buttons and text areas to there respective tabs     
              //**Buttons and text pads for the general tab**
                   generalContainer.add(generalCreate);
              generalContainer.add(generalOpen);
              generalContainer.add(generalSave);
              generalContainer.add(generalSaveAs);
                 generalContainer.add(generalExit);
              //adding buttons to the button panel 1
              buttonPanel1.add(consoleOpen);
              buttonPanel1.add(consoleSave);
              buttonPanel1.add(consoleConvert);
              //adding buttons to the button panel2          
              buttonPanel2.add(consoleRun);
              buttonPanel2.add(consoleReset);
              buttonPanel2.add(consoleCompile);
              //adding button panels and textarea
              consoleContainer.add(buttonPanel1,BorderLayout.NORTH);
              consoleContainer.add(consoleCode,BorderLayout.CENTER);
              consoleContainer.add(new JScrollPane(consoleCode));
              consoleContainer.add(buttonPanel2,BorderLayout.SOUTH);
              //adding the tabs          
              jtp.addTab("General",null,generalPane,"Choose General Options");
              jtp.addTab("Design",null,designPane,"Design your own network!!");
              jtp.addTab("Console",null,consolePane,"Type commands in console for designing");
              jtp.addTab("Output",null,outputPane,"View output");
              getContentPane().add(jtp, BorderLayout.CENTER);
         class TabManager implements ItemListener
              Component tab;
              public TabManager(Component tabToManage)
                   tab = tabToManage;
         public void itemStateChanged(ItemEvent ie)
              int index=jtp.indexOfComponent(tab);
             if (index!=-1)
                  jtp.setEnabledAt(index,ie.getStateChange()== ItemEvent.SELECTED);
             MainForm.this.repaint();
    public static void main(String args[])
         MainForm form = new MainForm();
         form.setVisible(true);
    }

  • Problem with JPanel in JFrame

    hai ashrivastava..
    thank u for sending this one..now i got some more problems with that screen .. actually i am added one JPanel to JFrame with BorderLayout at south..the problem is when i am drawing diagram..the part of diagram bellow JPanel is now not visible...and one more problem is ,after adding 6 ro 7 buttons remaing buttons are not vissible..how to increase the size of that JPanel...to add that JPanel i used bellow code
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    f.getContentPane().add(BorderLayout.SOUTH, panel);

    Hi
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    // Add this line to ur code with ur requiredWidth and requiredHeight
    panel.setPreferredSize(new Dimension(requiredWidth,requiredHeight));
    f.getContentPane().add(BorderLayout.SOUTH, panel);
    This should solve ur problem
    Ashish

  • Problem with JPanel sizing!

    Hi,
    Just having a bit of difficulty with the sizing of JPanels. For example, if I create a JPanel and add it to a Frame, no matter what I set the size to the JPanel fills the entire Frame. I don't really know whats going on here and I'm just making guesses! If anyone has a good resource on the whole sizing thing or can point out what I'm doing wrong, that would be great. Thanks!
    // Constructor
    public void ClassName()
            mainWindow = new JFrame();
            mainWindow.setSize(mainWindowWidth, mainWindowHeight);
            mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel aPanel = new JPanel();
            aPanel.setSize(mainWindowWidth/2,mainWindowHeight/2); );
            aPanel.setBackground(Color.GREEN);
            mainWindow.add(aPanel);
            mainWindow.validate();
    }Edited by: LeWalrus on May 18, 2009 5:03 AM

    By default, JFrame uses a BorderLayout which gives the center component (your panel in this case) all available height and width.
    For absolute positioning you need to use a null layout.
    If you want a layout manager that can let your panel fill half the height/width, I think it might be possible with GridBagLayout or more recommended MiGLayout (but that adds a third party jar dependency).
    See the [layout tutorial|http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html].

Maybe you are looking for

  • Short dump error with InfoSource

    Hi All, When i try to open Accounts Payable Infosource in BW production, It is returning with the short dump error ' Assertion was violated'. R_AWBOBJ ->. may I know what is this error. I created generic datasource in ECC DEV and replicated into BW D

  • My bookmarks are disappearing/reappearing randomly and I cannot save new ones

    Since this morning 8/17 I have had an odd problem with my bookmarks. Initially both my Blogs and News and Views sub-folders came up empty while nothing else was affected and the bookmarks in them were not to be found elsewhere. Later, after a reboot,

  • Bapi to Create/Update/Delete/Block G/L Accounts Centrally

    Hello experts, I'm searching for a BAPI to achieve some functionalities already done throw transaction FS00. After uploading account data from an input file I should: - Create G/L accounts - Update G/L accounts - Mark for deletion G/L accounts - Bloc

  • Free table space

    Hi, is there a way to free blocks of deleted record? I mean: I have a table of 50Gb and I delete 5Gb; those 5Gb are still part of the table size looking at the dba_segment view. And is there a way to completely free those blocks without export and im

  • Still having problems with "testversion has experied"

    Have payed stands that i payed for creative cload for photografing under products its says i dont have any products registered to my account why? havent found any answere!! need it to work