i am stuck
hi everyone
i am new to jave
i am i got stuck while biulding an applet.
here is where i am stuck.
i am biuding a table of 10 by 10 of checkboxes.
so i have made 100 checkboxes old fashion way adding one by one(if anybody knows a faster way plz do tell) now i want to laythem in a way that they look like a 10 by 10 table any suggestions
thank for the help
[399 byte] By [
t123a] at [2007-10-2 4:59:12]

here is myy code
JPanel p = new JPanel();
int w = 10, h = 10;
p.setLayout(new GridLayout(w,h,0,0));
for(int i = 0; i< w*h; i++)
p.add(new JCheckBox(""+i));
why can't i see anything when i excute project how can i see the grid i made
thanks
t123a at 2007-7-16 1:03:17 >

> can't i see anything
Your code snippet doesn't tell nothing about that.
We should see how you handle your JPanel in other part of your program.
BTW, it is a good practice to enclose a bdoy of loop or conditional clause with a brace pair
even when the body is one-line statement.
JPanel p = new JPanel();
int w = 10, h = 10;
p.setLayout(new GridLayout(w,h,0,0));
for(int i = 0; i < w * h; ++i){
p.add(new JCheckBox("" + i));
}
hiwaa at 2007-7-16 1:03:17 >

i just statred my programme and i want to see how my table will look like
here is my complet code
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel
import javax.swing.JLabel;
import javax.swing.JCheckBox;
public class table extends Applet {
public void init()
{
JPanel p = new JPanel();
int w = 10, h = 10;
p.setLayout(new GridLayout(w,h,2,2));
p.setBackground(Color.yellow);
for(int i = 0; i< w*h; i++)
p.add(new JCheckBox(""+i));
}
}
why i'm i not seeing my table
thanks
t123a at 2007-7-16 1:03:17 >

Eh! You make a JPanel and add hundred JCheckBoxes on it.
Then, you do nohing about the JPanel -- a forlorn orphan in a nil land.
If you made a component, you should add it onto a container which in most case is
the application's main window. And the window should be set for its size and visiblity.
If you use Swing component, use JApplet instead of Applet.
If you don't use other panel-like subcontainers, you could do:
public class Table extends Applet{
public void init(){
int w = 10, h = 10;
setLayout(new GridLayout(w, h, 2, 2));
setBackground(Color.yellow);
for (int i = 0; i< w * h; ++i){
add(new CheckBox("" + i));
}
...
}
...
...
}
hiwaa at 2007-7-16 1:03:17 >

quesion i have all these boxes now how can i preform actionoon them example if two boxes are check return to the user two thakns in advacen
t123a at 2007-7-16 1:03:17 >

let me rephrase that how can
do thAT
public class house implemens ItemListener{
if(.isSelected()) \\ here how could i refer to each buttom are the number
//or named what
}
thanks
t123a at 2007-7-16 1:03:17 >

ItemEvent#getSource() returns a Checkbox.Checkbox#getLabel() returns a String which is "0", "1" etc.Read the API javadoc for java.awt.Checkbox for other available methods.If you want to scan all of the hudred Checkboxes, then store them in an array beforehand.
hiwaa at 2007-7-16 1:03:17 >

how could i store them in an array i uderstand what you are saying but to stroe them how can you show in code using the thanks alot
t123a at 2007-7-16 1:03:17 >

also i don't qiute understand what you are saying about ItemEvent#getSource() returns a Checkbox.Checkbox#getLabel() returns a String which is "0", "1" etc.can you explain it to me morethanks
t123a at 2007-7-16 1:03:17 >

Time to read the tutorial, perhaps? http://java.sun.com/docs/books/tutorial/uiswing/components/button.htmlScroll down a bit and you'll find a section called "How to Use Check Boxes".
can you just show me how using my code could i call eventlistern and determine whxih one of the 100 box get stated. i know how to do it for 1 checkbox which has a name but the 100 that i stored have no name or do they or could they thanks
t123a at 2007-7-16 1:03:17 >

is it possible to item lisnter on the poostion of each box relative to the gridlayout.for example is at(1,2) is selected to accually take note of it and if so what is the code to that.
t123a at 2007-7-16 1:03:17 >

Maybe the latest reply in one of your other threads on this topic will help you: http://forum.java.sun.com/thread.jspa?threadID=681968
You MUST do Java GUI tutorial at Sun's site, before writing your own code.
Let me repeat: it's a MUST!!!
Here's a simple event handling example:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class T123 extends Applet implements ItemListener{
Checkbox[] cbs;
String msg;
boolean isApplication = false;
public void init(){
setLayout(new GridLayout(10, 10, 2, 2));
cbs = new Checkbox[100];
for (int i = 0; i < 100; ++i){
cbs[i] = new Checkbox(String.valueOf(i));
add(cbs[i]);
cbs[i].addItemListener(this);
}
msg = "Selected checkboxes are: ";
}
public void itemStateChanged(ItemEvent e){
String s = msg;
String b = "";
String[] selected;
Checkbox triggeredCb = (Checkbox)(e.getSource());
int triggeredState = e.getStateChange();
s = "[" + triggeredCb.getLabel() + "]" + s;
if (triggeredState == ItemEvent.DESELECTED){
s = "-" + s;
// do whatever
}
else{ // SELECTED
s = "+" + s;
// do whatever
}
for (int i = 0; i < 100; ++i){
if (cbs[i].getState()){ // gather selected checkboxes
b += String.valueOf(i) + ", ";
}
}
int sfx = b.lastIndexOf(',');
if (sfx != -1){
b = b.substring(0, sfx); // trim last ", "
selected = b.split(", "); // get selected index, use them for whatever
}
s += b;
if (! isApplication){
showStatus(s);
}
System.out.println(s);
}
//// main() for testing
public static void main(String[] args){
Frame frame = new Frame();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
T123 t = new T123();
t.isApplication = true;
frame.add(t);
frame.setSize(400, 400);
t.init();
frame.setVisible(true);
}
}
hiwaa at 2007-7-16 1:03:17 >

thancks for the code i had a question is i wanted to cear all checed boxes with an action listen to a button called clear how would i do thatthank aganin
t123a at 2007-7-20 18:26:41 >

YOU SHOULD ALWAYS READ THE API DOCUMENTATION FOR THE CLASS THAT YOUARE GOING TO USE.--Yes, I'm shouting, screaming for A SPECIAL KIND OF RUDENESS on public fora.
hiwaa at 2007-7-20 18:26:41 >

Hey t123, I'm with Hiwa on this one. You've been told over and over again by different people that you need to read some tutorials. But you apparently can't be bothered to do that, you seem to prefer that people here do the job for you. Now, depending on your goal, that might be a very smart strategy, but it most probably won't be a successful one at this place.