Results 1 to 12 of 12
  1. #1
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Post adding checkboxes to forms

    hi guys,am new to this forum nd am interested in working with java.am trying to add check boxes to the existing code which creates a form but i do not know how.i have created a form with this code:

    import javax.swing.*;
    import java.util.Date;
    import java.util.Calendar;
    import java.util.GregorianCalendar;

    public class ApplicantApplet extends JApplet implements Runnable
    {
    /* Declare panels */
    static JPanel panel;

    /* Declare labels */
    JLabel labelAppId;
    JLabel labelAppName;
    JLabel labelAppPosition;

    /* Declare TextArea */
    JTextField textAppID;
    JTextField textAppName;

    JComboBox comboAppPosition;

    /* Declare a Thread variable */
    Thread datimeThread;
    /* Declare a Date variable */
    Date date;
    /* Declare a GregorianCalendar variable */
    GregorianCalendar calendar;
    /* Declare String variables to store date time and status bar messages. */
    String strDate, strTime, strStatus;
    /* init method of applet */
    public void init()
    {
    panel = new JPanel();
    getContentPane().add(panel);
    labelAppId = new JLabel("Applicant ID");
    labelAppName = new JLabel("Applicant Name");
    labelAppPosition = new JLabel("Position");

    textAppID = new JTextField(5);
    textAppName = new JTextField(5);

    String positions[] = {"Manager", "Executive", "Associate"};
    comboAppPosition = new JComboBox(positions);

    panel.add(labelAppId);
    panel.add(textAppID);

    panel.add(labelAppName);
    panel.add(textAppName);

    panel.add(labelAppPosition);
    panel.add(comboAppPosition);

    dateTime();

    }

    public void dateTime()
    {
    /* Initialize thread */
    datimeThread = new Thread(this);

    /* Starting thread */
    datimeThread.start();
    }

    public void run() /* body of the thread */
    {
    while(datimeThread != null)
    {
    display();// This method displays date
    try
    {
    datimeThread.sleep(1000);
    }
    catch(InterruptedException e)
    {
    showStatus("Thread interrupted");
    }
    } /*end of while loop */
    } /* end of run method */

    /* displays date and time on the status bar */
    public void display()
    {
    date = new Date();
    calendar = new GregorianCalendar();
    calendar.setTime(date);
    strTime = calendar.get(Calendar.HOUR)+":"+calendar.get(Calen dar.MINUTE)+":"+calendar.get(Calendar.SECOND);
    strDate = (calendar.get(Calendar.MONTH)+1)+"/"+calendar.get(Calendar.DATE)+"/"+calendar.get(Calendar.YEAR);
    strStatus=strTime+" "+strDate;
    showStatus(strStatus);
    }

    }/* end of program */

    can somebody help me please!,edit my code and change font color where changes or additions are made.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You need to give more details on just what you're trying to do.
    Anyone can add a checkbox by simply, well adding one:
    Java Code:
    import javax.swing.*;
    import java.util.Date;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    
    public class ApplicantApplet extends JApplet implements Runnable
    {
      static JPanel panel;
    
      JLabel labelAppId;
      JLabel labelAppName;
      JLabel labelAppPosition;
      JTextField textAppID;
      JTextField textAppName;
      JComboBox comboAppPosition;
      
      // added
      JCheckBox checkBox = new JCheckBox("My Check Box");
    
      Thread datimeThread;
      Date date;
      GregorianCalendar calendar;
      String strDate, strTime, strStatus;
    
      public void init()
      {
        panel = new JPanel();
        getContentPane().add(panel);
        labelAppId = new JLabel("Applicant ID");
        labelAppName = new JLabel("Applicant Name");
        labelAppPosition = new JLabel("Position");
        textAppID = new JTextField(5);
        textAppName = new JTextField(5);
    
        String positions[] =
        {
          "Manager", "Executive", "Associate"
        };
        comboAppPosition = new JComboBox(positions);
        panel.add(labelAppId);
        panel.add(textAppID);
        panel.add(labelAppName);
        panel.add(textAppName);
        panel.add(labelAppPosition);
        panel.add(comboAppPosition);
    
        // added
        panel.add(checkBox);
    
        dateTime();
      }
    
      public void dateTime()
      {
        datimeThread = new Thread(this);
        datimeThread.start();
      }
    
      public void run()
      {
        while (datimeThread != null)
        {
          display();
          try
          {
            datimeThread.sleep(1000);
          }
          catch (InterruptedException e)
          {
            showStatus("Thread interrupted");
          }
        }
      }
    
      public void display()
      {
        date = new Date();
        calendar = new GregorianCalendar();
        calendar.setTime(date);
        strTime = calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE)
            + ":" + calendar.get(Calendar.SECOND);
        strDate = (calendar.get(Calendar.MONTH) + 1) + "/"
            + calendar.get(Calendar.DATE) + "/" + calendar.get(Calendar.YEAR);
        strStatus = strTime + " " + strDate;
        showStatus(strStatus);
      }
    }
    but without knowing what you are doing with this, my code is meaningless. Also, please use code tags when posting code as it makes your code a lot easier to read.

    Best of luck.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also, why is your panel object static?

  4. #4
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    hi Fubarable,thanx alot for ur help with adding checkboxes i got the idea now.
    i want to modify the following form such that when the user selects a course stream,the corresponding courses are displayed.its like creating a list.sy the user selects "networking" as the stream then "CCNA" appears as the corresponding course.


    import javax.swing.*;

    public class ApplicantApplet extends JApplet
    {
    /* Declare panels */
    static JPanel panel;

    /* Declare labels */
    JLabel labelAppName;
    JLabel labelAppGender;
    JLabel labelAppDOB;
    JLabel labelAppQualification;
    JLabel labelAppAddress;
    JLabel labelAppPhone;
    JLabel labelAppStream;
    JLabel labelAppCourse;


    /* Declare TextArea */
    JTextField textAppName;
    JTextField textAppDOB;
    JTextField textAppQualification;
    JTextField textAppAddress;
    JTextField textAppPhone;

    JComboBox comboAppGender;
    JComboBox comboAppStream;
    JCheckBox checkAppCourse;

    /* init method of applet */
    public void init()
    {
    panel = new JPanel();
    getContentPane().add(panel);
    labelAppName = new JLabel("Applicant Name");
    labelAppGender = new JLabel("Applicant Gender");
    labelAppDOB = new JLabel("Date Of Birth");
    labelAppQualification = new JLabel("Qualification");
    labelAppAddress = new JLabel("Address");
    labelAppPhone = new JLabel("Phone");
    labelAppStream = new JLabel("Stream");
    labelAppCourse = new JLabel("Course");


    textAppName = new JTextField(20);
    textAppDOB = new JTextField(20);
    textAppQualification = new JTextField(30);
    textAppAddress = new JTextField("Address here");
    textAppPhone = new JTextField(12);

    String Gender[] = {"Male", "Female"};
    comboAppGender = new JComboBox(Gender);

    String Stream[] = {"Software Engineering", "DataBase Systems", "Computer Networks"};
    comboAppStream = new JComboBox(Stream);

    String Course[] = {"Webpage Design", "SQL", "CCNA"};
    checkAppCourse = new JCheckBox("Webpage Design");
    checkAppCourse = new JCheckBox("SQL");


    panel.add(labelAppName);
    panel.add(textAppName);

    panel.add(labelAppGender);
    panel.add(comboAppGender);

    panel.add(labelAppDOB);
    panel.add(textAppDOB);


    panel.add(labelAppQualification);
    panel.add(textAppQualification);


    panel.add(labelAppAddress);
    panel.add(textAppAddress);

    panel.add(labelAppPhone);
    panel.add(textAppPhone);

    panel.add(labelAppStream);
    panel.add(comboAppStream);

    panel.add(labelAppCourse);
    panel.add(checkAppCourse);


    }

    }/* end of program */

    Use a different font color where edited

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    A few recommendations:
    1) Gear your application to create a JPanel rather than a JApplet or JFrame. This way you can place this panel into a JFrame or JApplet (or into another JPanel) if desired.
    2) Read up on how to use the layout managers and nest layouts to create a better visual display of your app. Start here:
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI with JFC/Swing)
    3) Again, use code tags when posting code into this forum, if you want your code to be readable (and thus have a greater chance of having others read your code and help you).

    For instance, a sample class that creates a JPanel that can be obtained here by calling getComponent():
    Fuplicant.java
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Fuplicant
    {
      private static final int FIELD_SIZE = 18;
      private JPanel mainPanel = new JPanel();
      private JTextField lastNameField = new JTextField(FIELD_SIZE);
      private JTextField firstNameField = new JTextField(FIELD_SIZE);
      private JComboBox sexCombo = new JComboBox(new String[]{"Male", "Female"});
      private JTextField addressField = new JTextField(FIELD_SIZE);
      private JTextField cityField = new JTextField(FIELD_SIZE);
      private JTextField zipField = new JTextField(FIELD_SIZE);
      
      private String[] labelStrings =
      {
        "Last Name", "First Name", "Sex", "Address", 
        "City", "Zip"  
      };
      private JComponent[] components = 
      {
        lastNameField, firstNameField, sexCombo, addressField,
        cityField, zipField
      };
      
      public Fuplicant()
      {
        int vertGap = 10;
        JPanel labelPanel = new JPanel(new GridLayout(0, 1, 0, vertGap));
        JPanel componentPanel = new JPanel(new GridLayout(0, 1, 0, vertGap));
        for (String labelString : labelStrings)
        {
          labelPanel.add(new JLabel(labelString));
        }
        for (JComponent component : components)
        {
          componentPanel.add(component);
        }
        
        JButton fuButton = new JButton("Foo!");
        JButton bazButton = new JButton("Baz!");
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 0));
        buttonPanel.add(fuButton);
        buttonPanel.add(bazButton);
        JPanel southPanel = new JPanel(new BorderLayout(0, 10));
        southPanel.add(new JSeparator(), BorderLayout.NORTH);
        southPanel.add(buttonPanel, BorderLayout.CENTER);
        
        int ebGap = 8;
        mainPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
        mainPanel.setLayout(new BorderLayout(10, 10));
        mainPanel.add(labelPanel, BorderLayout.WEST);
        mainPanel.add(componentPanel, BorderLayout.CENTER);
        mainPanel.add(southPanel, BorderLayout.SOUTH);
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    }
    Here I place the above class into a JFrame:
    Fuframe.java
    Java Code:
    import javax.swing.JFrame;
    
    public class Fuframe
    {
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("My Application");
        frame.getContentPane().add(new Fuplicant().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }
    and here I place it into a JApplet
    Fupplet.java
    Java Code:
    import javax.swing.JApplet;
    
    public class Fupplet extends JApplet
    {
      public void init()
      {
        try
        {
          javax.swing.SwingUtilities.invokeAndWait(new Runnable()
          {
            public void run()
            {
              createGUI();
            }
          });
        }
        catch (Exception e)
        {
          System.err.println("createGUI didn't successfully complete");
        }
      }
    
      private void createGUI()
      {
        getContentPane().add(new Fuplicant().getComponent());
      }
    }
    YMMV. Good luck.

  6. #6
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default creating list on form

    ur codes are not compiling,am using jGrasp.

  7. #7
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    ur codes for Fuframe and FuAppletare not compiling,am using JGrasp

  8. #8
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default implememnting action listner

    Hi guys,i need help,i have created a feedback form now i wnt to add action listner on the submit button and reset so that an ction is performed when buttons r invoked.cn somebody help me,show where edited on ma code.


    import javax.swing.*;
    import java.awt.*;
    public class BankSwing
    {
    static JFrame frame;
    static JPanel panel;
    JLabel lbankName, lname, lemailid, lsubject, lmessage, blabel;
    JTextField tname, temailid, tsubject;
    JTextArea tamessage;
    JButton submit, reset;
    GridLayout g1;
    public BankSwing()
    {
    g1 = new GridLayout(2,2);
    panel = new JPanel();
    frame = new JFrame("My Frame");
    frame.getContentPane().add(panel);
    panel.setLayout(new GridLayout(6,2,20,20));
    lbankName = new JLabel("MoneySaver Corporation");
    lbankName.setFont(new Font("Arial", Font.BOLD+Font.ITALIC, 25));
    blabel = new JLabel(" ");
    lname =new JLabel(" Name");
    lemailid =new JLabel(" E-Mail Id");
    lsubject =new JLabel(" Subject");
    lmessage =new JLabel(" Message");
    tname = new JTextField(2);
    temailid = new JTextField(2);
    tsubject = new JTextField("Feedback",2);
    tamessage = new JTextArea("Type your text here", 3,20);
    submit = new JButton("Submit");
    reset = new JButton("Reset");
    panel.add(lbankName);
    panel.add(blabel);
    panel.add(lname);
    panel.add(tname);
    panel.add(lemailid);
    panel.add(temailid);
    panel.add(lsubject);
    panel.add(tsubject);
    panel.add(lmessage);
    panel.add(tamessage);
    panel.add(submit);
    panel.add(reset);
    }
    public static void main(String args[])
    {
    BankSwing bs;
    bs=new BankSwing();
    frame.setSize(600,600);
    frame.setVisible(true);
    }
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by karabo101 View Post
    ur codes for Fuframe and FuAppletare not compiling,am using JGrasp
    They compile fine for me. What errors are you getting when you try to compile?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    And again (for the third time) when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

  11. #11
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Post implememnting action listner

    Hi guys,i need help,i have created a feedback form now i wnt to add action listner on the submit button and reset so that an ction is performed when buttons r invoked.cn somebody help me,show where edited on ma code.


    import javax.swing.*;
    import java.awt.*;
    public class BankSwing
    {
    static JFrame frame;
    static JPanel panel;
    JLabel lbankName, lname, lemailid, lsubject, lmessage, blabel;
    JTextField tname, temailid, tsubject;
    JTextArea tamessage;
    JButton submit, reset;
    GridLayout g1;
    public BankSwing()
    {
    g1 = new GridLayout(2,2);
    panel = new JPanel();
    frame = new JFrame("My Frame");
    frame.getContentPane().add(panel);
    panel.setLayout(new GridLayout(6,2,20,20));
    lbankName = new JLabel("MoneySaver Corporation");
    lbankName.setFont(new Font("Arial", Font.BOLD+Font.ITALIC, 25));
    blabel = new JLabel(" ");
    lname =new JLabel(" Name");
    lemailid =new JLabel(" E-Mail Id");
    lsubject =new JLabel(" Subject");
    lmessage =new JLabel(" Message");
    tname = new JTextField(2);
    temailid = new JTextField(2);
    tsubject = new JTextField("Feedback",2);
    tamessage = new JTextArea("Type your text here", 3,20);
    submit = new JButton("Submit");
    reset = new JButton("Reset");
    panel.add(lbankName);
    panel.add(blabel);
    panel.add(lname);
    panel.add(tname);
    panel.add(lemailid);
    panel.add(temailid);
    panel.add(lsubject);
    panel.add(tsubject);
    panel.add(lmessage);
    panel.add(tamessage);
    panel.add(submit);
    panel.add(reset);
    }
    public static void main(String args[])
    {
    BankSwing bs;
    bs=new BankSwing();
    frame.setSize(600,600);
    frame.setVisible(true);
    }
    }

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hm, if you ignore recommendations given multiple times above to use code tags, your result may be that forum folk could ignore you. Continue at your own peril.

Similar Threads

  1. ComboBox with CheckBoxes
    By heba.farouk in forum AWT / Swing
    Replies: 14
    Last Post: 06-09-2010, 11:03 AM
  2. How to use Swing CheckBoxes
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:44 PM
  3. Replies: 2
    Last Post: 04-28-2008, 10:56 AM
  4. Adding checkboxes on frame
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 08:40 AM
  5. Adding command buttons on MIDLet Forms
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-22-2007, 09:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •