0

Radio buttons selection in java code

asked 2012-12-06 21:18:29 +0800

kbsimm gravatar image kbsimm
153
http://www.ArivX.com/

The following example shows radio buttons in zul and in java. In the zul set selecting one button deselects the others. In the java set it does not. In java all selected buttons remain selected.

In the java code since the Radio buttons all have the same Radiogroup set I would expect the buttons to deselect when a new one is selected.

Any idea why this is not occurring.,

<window id="win" title="Radio Group test!!" border="normal" width="600px">

<label value="You are using: ${desktop.webApp.version}"/>

<vbox width="100%" align="stretch">
  <radiogroup>
    <grid>
      <rows>
        <row>
          <radio label="Red Zul" />
          <radio label="Blue Zul" />
          <radio label="Green Zul" />
        </row>
      </rows>
    </grid>
  </radiogroup>
</vbox>
<zk>

  <zscript><![CDATA[
  
    import org.zkoss.zk.ui.Component;
    import org.zkoss.zk.ui.Executions;
    import org.zkoss.zk.ui.util.GenericForwardComposer;
    import org.zkoss.zul.*;
    
    public class MyComposer extends GenericForwardComposer {
    public void doBeforeComposeChildren(Component comp) throws Exception {
        super.doBeforeComposeChildren(comp);
  
        win.appendChild(getGrid());

        }
  
    private Vbox getGrid()
    {
      Vbox mainbox = new Vbox();
      mainbox.setAlign("center");
    
      //Grid
      Grid grid = new Grid();
      grid.setParent(mainbox);
    
      // Build a rows component
      Rows rows = new Rows();
      rows.setParent(grid);
      
      // Build the rows
      Row row1 = new Row();
      Row row2 = new Row();
      
      row1.setParent(rows);
      row2.setParent(rows);
     
      // Radiogroup
      Radiogroup rg = new Radiogroup();
    
      // Radio Buttons
      Radio rb1 = new Radio("Red");
      rb1.setRadiogroup(rg);
      row1.appendChild(rb1);
      
      Radio rb2 = new Radio("Green");
      rb2.setRadiogroup(rg);
      row1.appendChild(rb2);
                  
      Radio rb3 = new Radio("Blue");
      rb3.setRadiogroup(rg);
      row1.appendChild(rb3);
                  
      return mainbox;
    }
  }
  ]]></zscript>
  <window apply="MyComposer">
  </window>
</zk>

</window>


thank you --Brad

delete flag offensive retag edit

8 Replies

Sort by ยป oldest newest

answered 2012-12-06 21:30:23 +0800

kbsimm gravatar image kbsimm
153
http://www.ArivX.com/

As I worked on writing this up I found the answer. Actually not the answer, rather a way to make it work. I am still hoping someone can tell me why?

One must give the RadioGroup a parent. I used

rg.setParent(row1)

I don't understand why the RadioGroup needs a relationship with the row that contains the radio buttons. Especially if I can add anther row of buttons for the same radiogroup as follows:

      Radio rb4 = new Radio("Orange");
      rb4.setRadiogroup(rg);
      row2.appendChild(rb4);
      
      Radio rb5 = new Radio("Yellow");
      rb5.setRadiogroup(rg);
      row2.appendChild(rb5);
                  
      Radio rb6 = new Radio("Purple");
      rb6.setRadiogroup(rg);
      row2.appendChild(rb6);
                  
      rg.setParent(row1);
      rg.setParent(row2);

These odd because now the RadioGroup is associated with two "collections" of buttons. Those in row1 and those in row2.

link publish delete flag offensive edit

answered 2012-12-07 09:59:46 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

PUSH.

This seems like a BUG. Tested with zk 6.0.0 ee eval.

<zk xmlns="http://www.zkoss.org/2005/zul" xmlns:h="http://www.w3.org/1999/xhtml"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:w="http://www.zkoss.org/2005/zk/client"
	xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">

	<window id="win" title="Radio Group test!!" border="normal" width="600px" apply="de.test.MyComposer">

		<label value="You are using: ${desktop.webApp.version}" />

		<vbox width="100%" align="stretch">
			<grid>
				<rows>
					<row>
						<radiogroup>
							<radio label="Red Zul" />
							<radio label="Blue Zul" />
							<radio label="Green Zul" />
						</radiogroup>
					</row>
				</rows>
			</grid>
		</vbox>

	</window>

</zk>

.

package de.test;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.Vbox;
import org.zkoss.zul.Window;

public class MyComposer extends GenericForwardComposer<Component> {

	private static final long serialVersionUID = 1L;

	private Window win;

	@Override
	public void doAfterCompose(Component window) throws Exception {
		super.doAfterCompose(window);

		win.appendChild(getGrid());
	}

	private Vbox getGrid() {
		Vbox mainbox = new Vbox();
		mainbox.setAlign("center");

		// Grid
		Grid grid = new Grid();
		grid.setParent(mainbox);

		// Build a rows component
		Rows rows = new Rows();
		rows.setParent(grid);

		// Build the rows
		Row row1 = new Row();
		Row row2 = new Row();

		row1.setParent(rows);
		row2.setParent(rows);

		// Radiogroup
		Radiogroup rg = new Radiogroup();
		// set Parent
		rg.setParent(row1);

		// Radio Buttons
		Radio rb1 = new Radio("Red");
		rb1.setParent(rg);
		// init
		rb1.setChecked(true);
		rb1.setParent(row1);

		Radio rb2 = new Radio("Green");
		rb2.setParent(rg);
		// init
		rb2.setChecked(false);
		rb2.setParent(row1);

		Radio rb3 = new Radio("Blue");
		rb3.setParent(rg);
		// init
		rb3.setChecked(false);
		rb3.setParent(row1);

		return mainbox;
	}
}

link publish delete flag offensive edit

answered 2012-12-10 14:29:05 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Push

link publish delete flag offensive edit

answered 2012-12-10 16:32:19 +0800

kbsimm gravatar image kbsimm
153
http://www.ArivX.com/

What does "Push" mean?

link publish delete flag offensive edit

answered 2012-12-10 20:25:56 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Get it on top,

So it does not forgotten.

link publish delete flag offensive edit

answered 2012-12-27 22:25:42 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Push again.

link publish delete flag offensive edit

answered 2012-12-28 14:59:16 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

It's not a bug. A Radiogroup is a normal ZK component that needs to be present in the desktop/page to work properly. Calling radio.setRadiogroup is not enough, because it only associates the radio with the group. In that case the Radiogroup still won't have a parent component, and will not exist on the page.

You can associate a Radiogroup with any amount of radio buttons. The location of the group in the page doesn't matter if you call setRadiogroup manually, so it doesn't have to be a parent of the radio components. As long as the group exists on the page somewhere, it will work with any radio components on the same page.

link publish delete flag offensive edit

answered 2012-12-29 14:23:56 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2012-12-29 14:24:38 +0800

You are exactly right gekkio.

Attached the corrected code:

	private Div getGrid2() {

		Div div = new Div();

		// Grid
		Grid grid = new Grid();
		grid.setParent(div);

		// Build a rows component
		Rows rows = new Rows();
		rows.setParent(grid);

		// Build the rows
		Row row1 = new Row();
		Row row2 = new Row();

		row1.setParent(rows);
		row2.setParent(rows);

		// Radiogroup
		Radiogroup rg = new Radiogroup();
		rg.setParent(row1);

		Radio rb1 = new Radio("Red");
		rb1.setRadiogroup(rg);
		rb1.setChecked(true);
		rb1.setParent(row2);

		Radio rb2 = new Radio("Green");
		rb2.setRadiogroup(rg);
		rb2.setParent(row1); // <--

		Radio rb3 = new Radio("Blue");
		rb3.setRadiogroup(rg);
		rb3.setParent(row2);

		return div;
	}

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-12-06 21:18:29 +0800

Seen: 239 times

Last updated: Dec 29 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More