• Post Reply Bookmark Topic Watch Topic
  • New Topic

checkbox problem  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating check box using
import java.awt.*
public class Checkboxes extends QuittableFrame {
public static void main(String[] args) {
new Checkboxes();
}
public Checkboxes() {
super("Checkboxes");
setFont(new Font("Helvetica", Font.BOLD, 18));
setLayout(new GridLayout(0, 2));
Checkbox box;
for(int i=0; i<12; i++) {
box = new Checkbox("Checkbox " + i);
if (i%2 == 0)
box.setState(true);
add(box);
}
pack();
show();
}
}
I get check box with a "tick" if selected. I want to change that to a cross.
Is that possible?
Please help.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's very hard with a CheckBox. It's easier with a JCheckBox but still not that easy. Are you sure it's worth it ?
If so, one way to do it is to create a subclass of ButtonUI and call setUI on the JCheckBox.
D.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you provide me a sample code. I have no knowledge of AWT or swing. I got the example from internet. Please atleast give me a outline of what method I am supposed to use.
Thanks
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to make the cross a tick ? It seems counterintuitive that a cross means "selected".
D.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my client insists that the checkstyle must be a "cross" and not a "tick".
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your client know if he wants you to do this in Swing or AWT?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SWING/AWT ...anything is fine. I will let him know depending on which works out well for us wrt to the checkstyle.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing will be much easier. Search the forums at java.sun.com, I'm fairly sure I saw a solution to this there.
D.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the cross image instead of tick but it is not clickable. What should I
do to make it clickable.
I have never written any program in swing and the below code is from sun's web site. Please help me.

This is my code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class cbox {
public static void main(String[] args) {
JFrame f = new JFrame("Lister v1.0");
f.setSize(300, 150);
f.setLocation(200, 200);
f.addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent we) { System.exit(0); }
});
JPanel entreePanel = new JPanel( );
final ButtonGroup entreeGroup = new ButtonGroup( );
JRadioButton radioButton;
entreePanel.add(radioButton = new JRadioButton("Beef"));
radioButton.setActionCommand("Beef");
entreeGroup.add(radioButton);
entreePanel.add(radioButton = new JRadioButton("Chicken"));
radioButton.setActionCommand("Chicken");
entreeGroup.add(radioButton);
entreePanel.add(radioButton = new JRadioButton("Veggie", true));
radioButton.setActionCommand("Veggie");
entreeGroup.add(radioButton);
final JPanel condimentsPanel = new JPanel( );
UIManager.put("CheckBox.icon", new ImageIcon("c:/sh/chkbox/ico.gif"));
condimentsPanel.add(new JCheckBox("Ketchup"));
condimentsPanel.add(new JCheckBox("Mustard"));
condimentsPanel.add(new JCheckBox("Pickles"));
JPanel orderPanel = new JPanel( );
JButton orderButton = new JButton("Place Order");
orderPanel.add(orderButton);
Container content = f.getContentPane( );
content.setLayout(new GridLayout(3, 1));
content.add(entreePanel);
content.add(condimentsPanel);
content.add(orderPanel);
orderButton.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
String entree =
entreeGroup.getSelection().getActionCommand( );
System.out.println(entree + " sandwich");
Component[] components = condimentsPanel.getComponents( );
for (int i = 0; i < components.length; i++) {
JCheckBox cb = (JCheckBox)components;
if (cb.isSelected( ))
System.out.println("With " + cb.getText( ));
}
}
});
f.setVisible(true);
}
}
[ March 04, 2004: Message edited by: shan java ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to do it in Swing is to just replace all the icons for all checkboxes with a modified version. This is possible because Swing has PLAF (Pluggable Look and Feel). Unfortunatly, most of the code for building the checkbox icon is buried in protected or package private classes, so instead of just overriding one method to draw the check differently, you have to cut and paste most of the icon drawing methods.


[ March 04, 2004: Message edited by: Nathan Pruett ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Your codde is perfect.
But since I am new to swing , I would like to know what part of the code basically changes the "tick" to a "plus" symbol. I want to change it to a
"multiplication symbol *".
Can you please explain the code with comments.
Thanks for your help and I appreciate it.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "drawCheck()" method... it originally drew the check mark, I changed it to draw a '+' using two rectangles.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
g.fillRect( x + 2, y + ( SIZE / 2 ) - 1, SIZE - 5, 2 ); g.fillRect( x + ( SIZE / 2 ) - 1, y + 2, 2, SIZE - 5 );
Can you expalin the parameters and how you came with it.
Thanks once again for ur reply.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x and y are the coordinates that the icon is told to paint at, and SIZE is the size of the icon (which is defined statically in the icon class).

The full specification of the fillRect() method, including descriptions of parameters, is:


Most of the values were tweaked by trial and error to see what looked good.

To draw a '*', you could either use an actual '*' using -


And mess with x and y values to try and position it, but I would not recommend this, because fonts can vary by quite a large degree across systems, and what might look good on your computer might look horrible elsewhere.

I would actually draw the '*' using the "drawLine()" method-


You would basically just draw 4 lines, using the x, y, and SIZE values to calculate how they are drawn:
1.) vertical line (x+(SIZE/2), y + 2, x+(SIZE/2), y+(SIZE-4)),
2.) horizontal line (x+2, y+(SIZE/2), x+(SIZE-4), y+(SIZE/2)),
3.) diagonal line from upper left to lower right (x+2, y+2, x+(SIZE-4), y+(SIZE-4)),
4.) diagonal line from upper right to lower left (x+(SIZE-4), y+2, x+2, y+(SIZE-4)).

You'll probably have to test out the drawing code a few times to make sure everything looks OK. Usually you have to mess with centering, spacing from sides, etc. to get stuff to look right.
[ March 05, 2004: Message edited by: Nathan Pruett ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!