//Author - Sameer Panse //In this code I have implemented many swing compenents. //code="SwingDemo.java" width=420 height=300 import javax.swing.*; import javax.swing.border.Border; import java.awt.*; import java.awt.event.*; public class SwingDemo extends JApplet implements ActionListener { JPanel pCandidate,pEmployee; JLabel lCandId,lCandName,lCandAddress,lCandSkills,lEmpId1; JLabel lEmpId,lEmpName,lCandId1; JTextField tCandId,tCandName,tCandAddress,tCandSkills,tEmpId1; JTextField tEmpId,tEmpName,tCandId1; //JButton btnChangeLnfC,btnChangeLnfE; JTabbedPane tPane; JMenuBar mb; JMenu mLnf,mAbout; JMenuItem mItem; GridBagConstraints gbc; GridBagLayout g; public void init() { try { //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); } catch(Exception e) { } ImageIcon iconC = new ImageIcon("Candidate.gif"); ImageIcon iconE = new ImageIcon("Employee.gif"); lCandId=new JLabel("ID"); lCandName=new JLabel("NAME"); lCandAddress=new JLabel("ADDRESS"); lCandSkills=new JLabel("SKILLS"); lEmpId1=new JLabel("EMP ID"); tCandId = new JTextField(5); tCandName = new JTextField(20); tCandAddress = new JTextField(30); tCandSkills = new JTextField(25); tEmpId1 = new JTextField(5); lEmpId = new JLabel("EMP ID"); lEmpName = new JLabel("NAME"); lCandId1 = new JLabel("CAND ID"); tEmpId=new JTextField(5); tEmpName=new JTextField(20); tCandId1=new JTextField(5); //btnChangeLnfC = new JButton("Set Java Look And Feel"); //btnChangeLnfE = new JButton("Set Windows Look And Feel"); pCandidate=new JPanel(); pEmployee=new JPanel(); tPane = new JTabbedPane(); mb = new JMenuBar(); mLnf = new JMenu("Look And Feel"); mAbout = new JMenu("About"); setJMenuBar(mb); mb.add(mLnf); mb.add(mAbout); mItem = new JMenuItem("Java"); mItem.addActionListener(this); mLnf.add(mItem); mItem = new JMenuItem("Windows"); mItem.addActionListener(this); mLnf.add(mItem); mItem = new JMenuItem("Author"); mItem.addActionListener(this); mAbout.add(mItem); //Border padding = BorderFactory.createEmptyBorder(20,20,5,20); //pCandidate.setBorder(padding); //pEmployee.setBorder(padding); getContentPane().add(tPane); tPane.addTab("Candidate Details",iconC,pCandidate,"Details of Candidate refered"); tPane.addTab("Employee Details",iconE,pEmployee,"Details of Employee referring Candidate"); g=new GridBagLayout(); pCandidate.setLayout(g); gbc=new GridBagConstraints(); gbc.insets=new Insets(5,5,0,0); //Common Constraints to all gbc.anchor=GridBagConstraints.NORTHWEST; //Candidate Panel components gbc.gridx=1; gbc.gridy=1; g.setConstraints(lCandId,gbc); pCandidate.add(lCandId); gbc.gridx=4; g.setConstraints(tCandId,gbc); pCandidate.add(tCandId); gbc.gridx=1; gbc.gridy=4; g.setConstraints(lCandName,gbc); pCandidate.add(lCandName); gbc.gridx=4; g.setConstraints(tCandName,gbc); pCandidate.add(tCandName); gbc.gridx=1; gbc.gridy=7; g.setConstraints(lCandAddress,gbc); pCandidate.add(lCandAddress); gbc.gridx=4; g.setConstraints(tCandAddress,gbc); pCandidate.add(tCandAddress); gbc.gridx=1; gbc.gridy=10; g.setConstraints(lCandSkills,gbc); pCandidate.add(lCandSkills); gbc.gridx=4; g.setConstraints(tCandSkills,gbc); pCandidate.add(tCandSkills); gbc.gridx=1; gbc.gridy=13; g.setConstraints(lEmpId1,gbc); pCandidate.add(lEmpId1); gbc.gridx=4; g.setConstraints(tEmpId1,gbc); pCandidate.add(tEmpId1); //pCandidate.add(btnChangeLnfC); pEmployee.setLayout(g); gbc.gridx=1; gbc.gridy=1; g.setConstraints(lEmpId,gbc); pEmployee.add(lEmpId); gbc.gridx=4; g.setConstraints(tEmpId,gbc); pEmployee.add(tEmpId); gbc.gridx=1; gbc.gridy=4; g.setConstraints(lEmpName,gbc); pEmployee.add(lEmpName); gbc.gridx=4; g.setConstraints(tEmpName,gbc); pEmployee.add(tEmpName); gbc.gridx=1; gbc.gridy=7; g.setConstraints(lCandId1,gbc); pEmployee.add(lCandId1); gbc.gridx=4; g.setConstraints(tCandId1,gbc); pEmployee.add(tCandId1); //pEmployee.add(btnChangeLnfE); //btnChangeLnfC.addActionListener(this); //btnChangeLnfE.addActionListener(this); } public void actionPerformed(ActionEvent e) { JMenuItem mSource = (JMenuItem)e.getSource(); if(mSource.getText() == "Java") { showStatus("Look and Feel - Java"); try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(this); } catch(Exception e1){} } if(mSource.getText() == "Windows") { showStatus("Look And Feel - Windows"); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(this); } catch(Exception e2){} } if(mSource.getText() == "Author") { //String msg="Author: Sameer Panse. You can read, modify and freely distribute this code"; //JOptionPane.showMessageDialog(this,msg,"Autor Information",JOptionPane.PLAIN_MESSAGE); JOptionPane pane = new JOptionPane("You know my name, don't you?",JOptionPane.PLAIN_MESSAGE); JDialog dialog = pane.createDialog(this,"About Author"); dialog.show(); } } }