• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

JCheckBox Allowable Selections

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know there is a way to only allow one JCheckBox to be selected, but is there a way to allow only 2 to be selected?
Thanks.
 
Joe Pawlak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, besides just being able to select 2, they should still be able to select 1 instead of 2.
 
Joe Pawlak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better yet, I have this code that will do what I need it to once I change the code a bit. But the problem is, the code allows the user to select as many radio buttons in a column as (s)he wants. But what I need is the user to select one option from each column.
I tried to change the code, but it didn't quite work. So if anyone could help it would be greatly appreciated, thanks.
CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScrollDemo extends JFrame {
JScrollPane scrollpane;
public ScrollDemo() {
super("JScrollPane Demonstration");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
setVisible(true);
}
public void init() {
JRadioButton form[][] = new JRadioButton[13][3];
String counts[] = { "", "1", "2" };
String categories[] = { "Household", "Office", "Extended Family",
"Company (US)", "Company (World)", "Team",
"Will", "Birthday Card List", "High School",
"Country", "Continent", "Planet" };
JPanel p = new JPanel();
p.setSize(600, 400);
p.setLayout(new GridLayout(13, 3, 10, 0));
for (int row = 0; row < 13; row++) {
ButtonGroup bg = new ButtonGroup();
for (int col = 0; col < 3; col++) {
if (row == 0) {
p.add(new JLabel(counts[col]));
}
else {
if (col == 0) {
p.add(new JLabel(categories[row - 1]));
}
else {
form[row - 1][col - 1] = new JRadioButton();
bg.add(form[row -1][col - 1]);
p.add(form[row -1][col - 1]);
}
}
}
}
scrollpane = new JScrollPane(p);
getContentPane().add(scrollpane, BorderLayout.CENTER);
}
public static void main(String args[]) {
new ScrollDemo();
}
}
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want the buttons in the same column have an exclusive selection? Just place them in the same ButtonGroup.
 
Joe Pawlak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried making two separate arrays of JRadioButtons and then putting those into ButtonGroups, but that is not working.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this?
 
Joe Pawlak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Craig, that works, but is there any way to decrease the distance between the two columns of the Radio Buttons? I tried making the panel smaller and took out the hgaps and vgaps, but those didn't seem to work.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Here's a GridBag layout. You can use the insets to control spacing
gbc.insets = new Insets(2,2,2,2);
among the components. [top, left, bottom, right]
 
Joe Pawlak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far I have this:

But the columns of Radio Buttons are too far apart, and are causing the questions to be cut off with an ...
The JPanel can be a little wider, but I don't want to make it as wide as it has to be right now in order for the entire question to be shown.
Thanks.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,
There is nothing you can do about the spacing when you use GridLayout. GridLayout ignores the preferred size of the components it is given to lay out. Instead it tries to make all the components the same size to just fill the space available. The column width for each radio button will always be the same as the column width for the labels. It's just a big grid.

Using other layout managers is the key to freedom here. Here's another run at it with GridBagLayout. This one expands during resize where the earlier one didn't. This is easy and elegant.

If this is unacceptable you can explore other options. How about three vertical panels laid out side-by-side with FlowLayout in a panel that goes into the "Center" of the content pane, each of the three panels with a GridLayout and holding the components of one column (labels, button, button)?

I won't pester you about GridBag anymore but will be glad to help if you ask.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic