The Artima Developer Community
Sponsored Link •

Java Answers Forum
What's the name of that group box thingamajig

1 reply on 1 page. Most recent reply: Dec 3, 2004 6:00 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Gibran Shah

Posts: 27
Nickname: gibby
Registered: Jun, 2004

What's the name of that group box thingamajig Posted: Nov 29, 2004 12:12 PM
Reply to this message Reply
Advertisement
Hello,

This question should be a simple one: What's the name of that box you put around swing components that shows that they belong together in a group. In C++'s QT, it's called a QGroupBox.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: What's the name of that group box thingamajig Posted: Dec 3, 2004 6:00 AM
Reply to this message Reply
Java works differently.

1. Create a JPanel.
2. Select a TitledBorder for that panel.
3. Put your options (Radiobuttons, JToggleButtons) in this panel.

So much for the optical part.

Now to the logic:
I guess you want that just one button should be selected at one time.
1. Create a JButtonGroup.
2. Assign this JButtonGroup to all your RadioButtons.


Here is an example created with NetBeans IDE:
public class MyFrame extends javax.swing.JFrame {
    public MyFrame() {
        initComponents();
    }
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
 
        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
 
        getContentPane().setLayout(null);
 
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
 
        jPanel1.setLayout(new java.awt.GridBagLayout());
 
        jPanel1.setBorder(new javax.swing.border.TitledBorder("Some title"));
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("jRadioButton1");
        buttonGroup1.add(jRadioButton1);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jRadioButton1, gridBagConstraints);
 
        jRadioButton2.setText("jRadioButton2");
        buttonGroup1.add(jRadioButton2);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jRadioButton2, gridBagConstraints);
 
        jRadioButton3.setText("jRadioButton3");
        buttonGroup1.add(jRadioButton3);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jRadioButton3, gridBagConstraints);
 
        getContentPane().add(jPanel1);
        jPanel1.setBounds(20, 20, 130, 150);
 
        pack();
        setSize (640,480);
    }
    
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    
    public static void main(String args[]) {
        new MyFrame().show();
    }
    
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
}


You don't need to create the panel containing the buttons, the ButtonGroup works without it, too.

Flat View: This topic has 1 reply on 1 page
Topic: problem with scroll Previous Topic   Next Topic Topic: Using Javabeans with Tomcat

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use