Results 1 to 4 of 4
  1. #1
    ksaketh2 is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Post Button Group problems

    1.import java.awt.Dimension;
    2.import java.awt.Font;
    3.
    4.import javax.swing.BorderFactory;
    5.import javax.swing.ButtonGroup;
    6.import javax.swing.JButton;
    7.import javax.swing.JLabel;
    8.import javax.swing.JPanel;
    9.import javax.swing.JRadioButton;
    10.import javax.swing.JTextField;
    11.
    12.public class EmployeePanel extends JPanel {
    13.
    14. private JLabel empID, lastName, firstName, creditRate, classRate, numCred, numClass, salary, hours;
    15. private JTextField empIDField, lastField, firstField, creditRateField, classRateField, numCredField,
    16. numClassField, salaryField, hourField;
    17. private JButton addEmp, writeReport, displayReport, readFile, exit;
    18. private JRadioButton fullTime, partTime, admin, maint;
    19.
    20.
    21. public EmployeePanel() {
    22. //JLabels
    23. empID = new JLabel("Employee ID");
    24. lastName = new JLabel("Last Name");
    25. firstName = new JLabel("First Name");
    26. creditRate = new JLabel("Credit Rate");
    27. classRate = new JLabel("Class Rate");
    28. numCred = new JLabel("Credit #");
    29. numClass = new JLabel("Class #");
    30. salary = new JLabel("Salary");
    31. hours = new JLabel("Hours");
    32.
    33. classRate.setFont(new Font("Arial", Font.PLAIN, 17));
    34. numClass.setFont(new Font("Arial", Font.PLAIN, 17));
    35. salary.setFont(new Font("Arial", Font.PLAIN, 17));
    36. hours.setFont(new Font("Arial", Font.PLAIN, 17));
    37. empID.setFont(new Font("Arial", Font.PLAIN, 17));
    38. lastName.setFont(new Font("Arial", Font.PLAIN, 17));
    39. firstName.setFont(new Font("Arial", Font.PLAIN, 17));
    40. creditRate.setFont(new Font("Arial", Font.PLAIN, 17));
    41. numCred.setFont(new Font("Arial", Font.PLAIN, 17));
    42.
    43.
    44. //JRadioButtons
    45. fullTime = new JRadioButton("Full Time Faculty", true);
    46. partTime = new JRadioButton("Part Time Faculty");
    47. admin = new JRadioButton("Administration");
    48. maint = new JRadioButton("Maintenance");
    49.
    50. //JButtons
    51. addEmp = new JButton("Add Employee");
    52. writeReport = new JButton("Write Report to File");
    53. displayReport = new JButton("Display Summary Pay Report");
    54. readFile = new JButton("Read From File");
    55. exit = new JButton("Exit");
    56.
    57. //JTextFields
    58. empIDField = new JTextField(11);
    59. lastField = new JTextField(11);
    60. firstField = new JTextField(11);
    61. creditRateField = new JTextField(11);
    62. classRateField = new JTextField(11);
    63. numCredField = new JTextField(11);
    64. numClassField = new JTextField(11);
    65. salaryField = new JTextField(11);
    66. hourField = new JTextField(11);
    67.
    68. //Buttongroup for RadioButtons
    69. ButtonGroup type = new ButtonGroup();
    70. type.add(fullTime);
    71. type.add(partTime);
    72. type.add(admin);
    73. type.add(maint);
    74.
    75. //Radio Button Panel
    76. JPanel radioPanel = new JPanel();
    77. radioPanel.setBorder(BorderFactory.createTitledBor der("Type of Employee"));
    78. radioPanel.add(fullTime);
    79. radioPanel.add(partTime);
    80. radioPanel.add(admin);
    81. radioPanel.add(maint);
    82.
    83. JPanel firstButtons = new JPanel();
    84. firstButtons.add(addEmp);
    85. firstButtons.add(writeReport);
    86.
    87. JPanel secButtons = new JPanel();
    88. secButtons.add(displayReport);
    89. secButtons.add(readFile);
    90. secButtons.add(exit);
    91.
    92. JPanel labels = new JPanel();
    93. labels.add(empID);
    94. labels.add(lastName);
    95. labels.add(firstName);
    96. labels.add(creditRate);
    97. labels.add(numCred);
    98. labels.setPreferredSize(new Dimension(100, 150));
    99.
    100. JPanel fields = new JPanel();
    101. fields.add(empIDField);
    102. fields.add(lastField);
    103. fields.add(firstField);
    104. fields.add(creditRateField);
    105. fields.add(numCredField);
    106. fields.setPreferredSize(new Dimension(130, 150));
    107.
    108. add(radioPanel);
    109. add(labels);
    110. add(fields);
    111. add(firstButtons);
    112. add(secButtons);
    113.
    114. setPreferredSize (new Dimension(480, 300));
    115. }
    116.}



    in the above code, how do we get the selected radio button's label from the button group "type" while working from the application?

    plz help


    thanks in advance...

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Button Group problems

    There are several options; all need to check each button in the group (and see if it is selected); the ButtonGroup remembers the ButtonModels it is managing; so, you can also check each button and compare its ButtonModel with the selected one from the ButtonGroup.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ksaketh2 is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: Button Group problems

    ya thank you for the reply... but I just need help for this code

    String s1= bg1.getSelected().getActionCommand();

    here, bg1 is a button group object with 3 member jradio buttons. I am not able to get the label of the selected jradio button. the above code is resulting in "null". how do I go about?

    i'll provide the necessary code if required...

    P.S this code is error free when compiled in eclipse kepler...

  4. #4
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Button Group problems

    Firstly, please wrap your code with [code] tags so that it is easier to read, e.g.,
    [code]
    // your code here
    [/code]

    gives
    Java Code:
    // your code here
    There are several ways to do this without needing to check each button in the group.

    By default you cannot attach a listener to a ButtonGroup. You can take a look at its API (ButtonGroup (Java Platform SE 7 )) to see that the method to do so is absent. However you can extend ButtonGroup to provide the necessary method to add an ActionListener. This is described at java - Listening on ButtonGroup for "child" changes, and print selected JRadioButton's text - Stack Overflow. Note that this requires each of the radio buttons to have an action command explicitly set (e.g., maint.setActionCommand("Maintenance"))

    Another way to do this is you can use "type/bg1.getSelection().getActionCommand()" as you suggested (but not getSelected() though). However this too requires each of the radio buttons to have an action command explicitly set otherwise getActionCommand() will return a null as you've found.

    The more conventional way to find out the selected radio button is to add an action listener to each of the radio buttons. This is described at How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components). Note that in the code example there, each of the radio buttons also has an action command set. However in my tests I've found that the resultant ActionEvent object's getActionCommand() returns the radio button's label without needing to set the action command.

Similar Threads

  1. Button and ActionListner problems
    By reader34 in forum Java Applets
    Replies: 2
    Last Post: 04-28-2014, 11:52 AM
  2. Replies: 0
    Last Post: 11-21-2013, 06:13 PM
  3. Button Problems D:
    By mildog1234 in forum New To Java
    Replies: 4
    Last Post: 11-08-2013, 10:16 AM
  4. Subframe, clear and exit button problems
    By dhiy0 in forum New To Java
    Replies: 1
    Last Post: 02-17-2013, 08:09 PM
  5. Button Group in Netbeans.
    By makpandian in forum NetBeans
    Replies: 2
    Last Post: 03-22-2010, 01:26 PM

Posting Permissions

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