Checkbox

Hello,I hope someone can help me... I'm editing a record of my table and I'm using a checkbox to indicate if my record is active or not. But when I edit it I don't know how to make my checkbox appears checked if my field is 1 or appears unchecked if it is 0...? Thanks
[292 byte] By [Jaquelinea] at [2007-11-26 15:24:10]
# 1
JCheckBox yourBox= new JCheckBox(...);yourBox.setSelected(oneOrZero == 1);kind regards,Jos
JosAHa at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 2

> I hope someone can help me... I'm editing a record of

> my table and I'm using a checkbox to indicate if my

> record is active or not. But when I edit it I don't

> know how to make my checkbox appears checked if my

> field is 1 or appears unchecked if it is 0...

If by a checkbox, you mean JCheckBox you have to call the setSelected() method on it with true or false depending on the value your field contains. If you meant HTML checkbox, you can use the javascript checked property on the checkbox.

myusernamea at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 3

Indeed. You'll have to sat more about the setting. I thought you were refering to a Swing JTable. In which case, the simplest way to go is to make sure your table model returns Boolean for the column class, then the JTable will automatically render the column with a check box. (And you should you booleans rather than 0 and 1!)

Demo:

import javax.swing.*;

import javax.swing.table.*;

public class TableExample {

public static void main(String[] args) {

Object[][] data = {

{"true", Boolean.TRUE},

{"false", Boolean.FALSE}

};

String[] columnNames = {"Strings", "Booleans"};

JTable table = new JTable(new MyModel(data, columnNames));

JFrame f = new JFrame("TableExample");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new JScrollPane(table));

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

}

class MyModel extends DefaultTableModel {

public MyModel(Object[][] data, Object[] columnNames) {

super(data, columnNames);

}

public boolean isCellEditable(int rowIndex, int columnIndex) {

return false;

}

public Class<?> getColumnClass(int columnIndex) {

return (getRowCount() == 0) ? Object.class : getValueAt(0, columnIndex).getClass();

}

}

DrLaszloJamfa at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 4
I still can't resolve my problem....I have this:Integer estatus = (Integer) EUsuarioDataProvider.getValue("tynEstatus");if (estatus.intValue() == 1){// check my JCheckBox}
Jaquelinea at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 5

I still can't resolve my problem....

I have this:

Integer estatus = (Integer) EUsuarioDataProvider.getValue("tynEstatus");

if (estatus.intValue() == 1){

// setchecked my JCheckbox

chkActivo.setSelected(*);

}

* what I need to write as parameter in setSelected method?

Jaquelinea at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 6
What is your GUI? Swing?
DrLaszloJamfa at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 7
mmhhh? I'm using JCreator Studio
Jaquelinea at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 8
> mmhhh? I'm using JCreator StudioI'm not familar with "JCreator", but my question was about your application, not the IDE you are using to build it. Is your application's GUI done with Swing? Is the "table" you refered to in the original post a JTable?
DrLaszloJamfa at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 9
I hope this answer your question.... my application is for web using JSF
Jaquelinea at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 10

> I hope this answer your question.... my application is for web using JSF

At last! The choice of technology is crucial to answering your problem. You should have mentioned JSF in the very first post. My JSF is rusty. I suggest you post your question to the JSF forum:

http://forum.java.sun.com/forum.jspa?forumID=427

And it goes without saying that there are no JCheckBoxs involved!

DrLaszloJamfa at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 11
Jaqueline ele perguntou se vc est?usando Swing (GUI) , ou seja,interfaces graficas provenientes de Swing , como JTable, JComboBox, Containers.. etc.. se a sua Table ? = JTable sorry about the weird language lol, it was just to make her to understand what was he asking.
Java__Estudantea at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 12

Thanx both DrLaszlo and Estudante

I write to this forum 'cause I'm really New in Java....

But right now I'm developing my first application with JSF in JCreator Studio. JosAH and myusername maybe could help me but they don't reply

I wrote my question in http://forum.java.sun.com/forum.jspa?forumID=881 but still nobody reply it.

Jaquelinea at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...
# 13
I'd say your question is about JSF, not the tool you are using to create your code, so you should post to the Jave ServerFaces forum. (When I worked with JSF, I used IBM Websphere -- the tool doesn't matter).
DrLaszloJamfa at 2007-7-8 21:39:34 > top of Java-index,Java Essentials,New To Java...