have problem in creating forms in swing.

problem in making form in swing

Page 1 of 1

1 Replies - 2699 Views - Last Post: 21 August 2007 - 04:53 PM Rate Topic: -----

#1 maillaksh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 16-August 07

have problem in creating forms in swing.

Post icon  Posted 16 August 2007 - 11:13 AM

I am having severals problems with my project.I am creating a form with 6 tabs.
Now here I am posting a part of my coding.In which I have The following problems.
1.I want to create a separate class for the action events for the buttons I have placed in the forms.
2.I need to connect this tab to database(MS Access) so that all the data filled can be saved.
3.I need to fill data in the top textfieds and combos,and when I press ADD button it should be saved in database and the data shows up in the table.

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.border.*;
import javax.swing.JTable.*;
import javax.swing.table.TableColumn;
import javax.swing.JScrollPane;
import javax.swing.Box;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Design extends JFrame
{
 Blistener bl=new Blistener();
 JButton can;
 public Design()
 {
  super("Tenant Form");
  setLayout(new BorderLayout());
  JTabbedPane tabbedPane=new JTabbedPane();		 //creating tabbed buttons
															   //<--------coding for first tab--------->
  JPanel panel1=new JPanel();															
  tabbedPane.addTab("Detail of Landlord",null,panel1,"first tab");				  //here panel1 is created for tab1

  GridBagConstraints gbc=new GridBagConstraints();
  gbc.insets=new Insets(2,2,2,2);
  gbc.anchor=GridBagConstraints.WEST;	
  
  JPanel jp11 = new JPanel();				   //1st panel in 1st tab for top labels and buttons
  jp11.setLayout(new GridBagLayout());
  
  JLabel l241 = new JLabel("Name Of LandLord");
  jp11.add(l241,gbc);
  
  JComboBox jc01=new JComboBox();
  jc01.addItem("Select");
  jc01.addItem("Mr.");
  jc01.addItem("Mrs.");
  gbc.gridx=1;
  jp11.add(jc01,gbc);
  
  JTextField f01=new JTextField(10);								
  gbc.gridx=2;
  jp11.add(f01,gbc);
  
  JLabel l251 = new JLabel("Sex");
  gbc.gridx=5;
  gbc.insets=new Insets(2,20,2,2);	
  jp11.add(l251,gbc);

  JComboBox jc11=new JComboBox();
  jc11.addItem("Select");
  jc11.addItem("Male");
  jc11.addItem("Female");
  gbc.gridx=6;
  gbc.insets=new Insets(2,2,2,2);
  jp11.add(jc11,gbc);
  
  JLabel l261 = new JLabel("Age(Yrs)");
  gbc.gridx=8;
  gbc.insets=new Insets(2,20,2,2);
  jp11.add(l261,gbc);
  
  JTextField f11=new JTextField(3);
  gbc.gridx=9;
  gbc.insets=new Insets(2,2,2,2);
  jp11.add(f11,gbc);
 
  JLabel l271 = new JLabel("Occupation");
  gbc.gridx= 11;
  gbc.insets=new Insets(2,20,2,2);
  jp11.add(l271,gbc);

  JComboBox jc21=new JComboBox();
  jc21.addItem("Select");
  jc21.addItem("Engineer");
  jc21.addItem("Business");
  gbc.gridx=12;
  gbc.insets=new Insets(2,2,2,2);
  jp11.add(jc21,gbc);

  JButton ab1=new JButton("ADD");
  gbc.gridx=14;
  gbc.insets=new Insets(2,20,2,2);
  jp11.add(ab1,gbc);
  
  panel1.add(jp11);
										  //<--coding for adding table with scroll pane #yet to be coded#-->
  String colheads11[]={"S.No.","NAME OF LANDLORD","SEX","AGE","OCCUPATION"};
  Object data11[][]={{new Integer(1),"laksh","male",new Integer(21),"student"}};
  JTable jtab11=new JTable(data11,colheads11);
  JScrollPane scroll31=new JScrollPane(jtab11,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  jtab11.setPreferredScrollableViewportSize(new Dimension(750, 100));
  TableColumn column11 = null;
  for (int i = 0; i < 5; i++) 
  {
   column11 = jtab11.getColumnModel().getColumn(i);
   if (i==0)
   {
	column11.setPreferredWidth(30);
   }
   if (i == 1||i==4)
   {
	column11.setPreferredWidth(150); //third column is bigger
   }
   else
   {
	column11.setPreferredWidth(50);
   }
  }
  panel1.add(scroll31);
  
										  //start of p21 panel. 1st of two titledborder panels in tab 1
  JPanel jp21=new JPanel();
  
  jp21.setBorder(new TitledBorder("Address Of Landlord Property"));  
  jp21.setLayout(new GridBagLayout());
  
  JLabel l11=new JLabel("Property/House/Building Address");
  gbc.gridx=0;
  gbc.gridy=0;
  jp21.add(l11,gbc);
  
  JTextArea ta11=new JTextArea(3,15);
  ta11.setLineWrap(true);
  JScrollPane scroll11=new JScrollPane(ta11,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

  gbc.gridx=1;
  jp21.add(scroll11,gbc);
  
  JLabel l21=new JLabel("Land Mark");
  gbc.gridx=0;
  gbc.gridy=1;
  gbc.ipady=0;
  gbc.gridheight=1;
  jp21.add(l21,gbc);
 
  JTextField tf21=new JTextField(15);
  gbc.gridx=1;
  gbc.gridy=1;
  jp21.add(tf21,gbc);

  JLabel l31=new JLabel("Country");
  gbc.gridx=0;
  gbc.gridy=2;
  jp21.add(l31,gbc);

  JComboBox c11=new JComboBox();
  c11.addItem("India");
  c11.addItem("US");
  c11.addItem("Australia");
  gbc.gridx=1;
  gbc.gridy=2;
  jp21.add(c11,gbc);

  JLabel l41=new JLabel("State");
  gbc.gridx=0;
  gbc.gridy=3;
  jp21.add(l41,gbc);

  JComboBox c21=new JComboBox();
  c21.addItem("Rajasthan");
  c21.addItem("Delhi");
  c21.addItem("Maharastra");
  gbc.gridx=1;
  gbc.gridy=3;
  jp21.add(c21,gbc);

  JLabel l51=new JLabel("District");
  gbc.gridx=0;
  gbc.gridy=4;
  jp21.add(l51,gbc);

  JComboBox c31=new JComboBox();
  c31.addItem("jaipur");
  c31.addItem("ajmer");
  c31.addItem("alwar");
  gbc.gridx=1;
  gbc.gridy=4;
  jp21.add(c31,gbc);

  JLabel l61=new JLabel("City/Town");
  gbc.gridx=0;
  gbc.gridy=5;
  jp21.add(l61,gbc);

  JTextField tf31=new JTextField(15);
  gbc.gridx=1;
  gbc.gridy=5;
  jp21.add(tf31,gbc);

  JLabel l71=new JLabel("Police District");
  gbc.gridx=0;
  gbc.gridy=6;
  jp21.add(l71,gbc);

  JComboBox c41=new JComboBox();
  c41.addItem("Jaipur");
  c41.addItem("Alwar");
  c41.addItem("Ajmer");
  gbc.gridx=1;
  gbc.gridy=6;
  jp21.add(c41,gbc);
 
  JLabel l81=new JLabel("Police Circle");
  gbc.gridx=0;
  gbc.gridy=7;
  jp21.add(l81,gbc);

  JComboBox c51=new JComboBox();
  c51.addItem("India");
  c51.addItem("US");
  c51.addItem("Australia");
  gbc.gridx=1;
  gbc.gridy=7;
  jp21.add(c51,gbc);

  JLabel l91=new JLabel("Police station");
  gbc.gridx=0;
  gbc.gridy=8;
  jp21.add(l91,gbc);

  JComboBox c61=new JComboBox();
  c61.addItem("Bani Park");
  c61.addItem("Raja Park");
  c61.addItem("Malviya Nagar");
  gbc.gridx=1;
  gbc.gridy=8;
  jp21.add(c61,gbc);

  JLabel l101=new JLabel("Pin No.");
  gbc.gridx=0;
  gbc.gridy=9;
  jp21.add(l101,gbc);

  JTextField tf41=new JTextField(15);
  gbc.gridx=1;
  gbc.gridy=9;
  jp21.add(tf41,gbc);

  JLabel l111=new JLabel("Phone No.(R)");
  gbc.gridx=0;
  gbc.gridy=10;
  jp21.add(l111,gbc);
  
  JTextField tf51=new JTextField(15);
  gbc.gridx=1;
  gbc.gridy=10;
  jp21.add(tf51,gbc);
											 //start of p31.2nd of two titledborder panels in tab 1
  JPanel jp31=new JPanel();
  jp31.setBorder(new TitledBorder("Address Of Landlord Office"));
  jp31.setLayout(new GridBagLayout());
  
  JLabel l121=new JLabel("Office Address");
  gbc.gridx=0;
  gbc.gridy=0;
  jp31.add(l121,gbc);
  
  JTextArea ta61=new JTextArea(3,15);
  ta61.setLineWrap(true);
  JScrollPane scroll2=new JScrollPane(ta61,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  gbc.gridx=1;
  gbc.gridy=0;
  jp31.add(scroll2,gbc);
  
  JLabel l131=new JLabel("Country");
  gbc.gridx=0;
  gbc.gridy=1;
  gbc.gridheight=1;
  jp31.add(l131,gbc);

  JComboBox c12=new JComboBox();
  c12.addItem("India");
  c12.addItem("US");
  c12.addItem("Australia");
  gbc.gridx=1;
  gbc.gridy=1;
  jp31.add(c12,gbc);

  JLabel l141=new JLabel("State");
  gbc.gridx=0;
  gbc.gridy=2;
  jp31.add(l141,gbc);

  JComboBox c22=new JComboBox();
  c22.addItem("Rajasthan");
  c22.addItem("Delhi");
  c22.addItem("Maharastra");
  gbc.gridx=1;
  gbc.gridy=2;
  jp31.add(c22,gbc);

  JLabel l151=new JLabel("District");
  gbc.gridx=0;
  gbc.gridy=3;
  jp31.add(l151,gbc);

  JComboBox c32=new JComboBox();
  c32.addItem("jaipur");
  c32.addItem("ajmer");
  c32.addItem("alwar");
  gbc.gridx=1;
  gbc.gridy=3;
  jp31.add(c32,gbc);

  JLabel l161=new JLabel("City/Town");
  gbc.gridx=0;
  gbc.gridy=4;
  jp31.add(l161,gbc);

  JTextField tf71=new JTextField(15);
  gbc.gridx=1;
  gbc.gridy=4;
  jp31.add(tf71,gbc);

  JLabel l171=new JLabel("Police District");
  gbc.gridx=0;
  gbc.gridy=5;
  jp31.add(l171,gbc);

  JComboBox c42=new JComboBox();
  c42.addItem("Jaipur");
  c42.addItem("Alwar");
  c42.addItem("Ajmer");
  gbc.gridx=1;
  gbc.gridy=5;
  jp31.add(c42,gbc);
 
  JLabel l181=new JLabel("Police Circle");
  gbc.gridx=0;
  gbc.gridy=6;
  jp31.add(l181,gbc);

  JComboBox c52=new JComboBox();
  c52.addItem("India");
  c52.addItem("US");
  c52.addItem("Australia");
  gbc.gridx=1;
  gbc.gridy=6;
  jp31.add(c52,gbc);

  JLabel l191=new JLabel("Police station");
  gbc.gridx=0;
  gbc.gridy=7;
  jp31.add(l191,gbc);

  JComboBox c62=new JComboBox();
  c62.addItem("Bani Park");
  c62.addItem("Raja Park");
  c62.addItem("Malviya Nagar");
  gbc.gridx=1;
  gbc.gridy=7;
  jp31.add(c62,gbc);

  JLabel l201=new JLabel("Pin No.");
  gbc.gridx=0;
  gbc.gridy=8;
  jp31.add(l201,gbc);

  JTextField tf81=new JTextField(15);
  gbc.gridx=1;
  gbc.gridy=8;
  jp31.add(tf81,gbc);

  JLabel l211=new JLabel("Phone No.(O)");
  gbc.gridx=0;
  gbc.gridy=9;
  jp31.add(l211,gbc);
  
  JTextField tf91=new JTextField(15);
  gbc.gridx=1;
  gbc.gridy=9;
  jp31.add(tf91,gbc);

  JLabel l221=new JLabel("Phone No.(M)");
  gbc.gridx=0;
  gbc.gridy=10;
  gbc.gridheight=1;
  jp31.add(l221,gbc);
 
  JTextField tf101=new JTextField(15);
  gbc.gridx=1;
  gbc.gridy=10;
  jp31.add(tf101,gbc);

  JLabel l231=new JLabel("E-mail");
  gbc.gridx=0;
  gbc.gridy=11;
  gbc.gridheight=1;
  jp31.add(l231,gbc);
 
  JTextField tf111=new JTextField(15);
  gbc.gridx=1;
  gbc.gridy=11;
  jp31.add(tf111,gbc);


  JPanel jp41=new JPanel();							 //adding above two panels p21 and p31 to p41 panel.
  jp41.setLayout(new BoxLayout(jp41,BoxLayout.X_AXIS));
  jp41.add(jp21);
  jp41.add(Box.createHorizontalStrut(50));
  jp41.add(jp31);
  panel1.add(jp41);									//adding p41 panel to panel1 

															   //<--------coding for second tab--------->add(tabbedPane);
											 //lower panel for cancel button
  JPanel jp1=new JPanel();
  jp1.setLayout(new FlowLayout(FlowLayout.RIGHT));
  can=new JButton("Cancel");	 //initialising cancel button
  can.addActionListener(bl);
  jp1.add(can);
  add(jp1,BorderLayout.SOUTH);
 }
}

class Blistener implements ActionListener	 // class for action listener
{
 public void actionPerformed(ActionEvent ae)
 {
  Object obj=ae.getSource();
 try{
  if(obj == can)							 //condition for cancel button
  {
   System.exit(0);
  }
   }catch(Exception e)
{System.out.println(e);
}
 }
}
public class Project
{
 public static void main(String args[])			   //main method for the program
 {
  Design tabbedwin= new Design();
  tabbedwin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  tabbedwin.setSize(900,700);
  tabbedwin.setVisible(true);
 }
}



Is This A Good Question/Topic? 0
  • +

Replies To: have problem in creating forms in swing.

#2 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: have problem in creating forms in swing.

Posted 21 August 2007 - 04:53 PM

those items are not problems they are requests.... what works and what doesn't, what do need help with. We are not going to implement the whole thing for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1