Hi. I am trying to create a stand alone app but stuck somewhere.
When i click on a JCheckBox, it opens up another frame window. in this window if i click cancel then the JCheckBox in the previous frame is not checked. but if i click OK then this JCheckBox should be checked, but its not hapening. I have successfully done the cancel part but facing problems with the OK part.
Please help me out.

Recommended Answers

All 10 Replies

Java doesn't have any Swings. You find those in playgrounds with kids using them. Now, if you mean the Java Swing (note no s on the end) API, then say that.

You might want to post the relevant parts of your code. And, I hope, you are using a JDialog (or JOptionPane) for the "frame" that opens and not a JFrame. If not, using one of those may solve your problem almost in and of itself.

how to connectivity is done between two or more frames.

Here's the code for the first JFrame

import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;

class QueryBuilderDemo extends JFrame implements ActionListener, ItemListener
{	JComboBox jcb1,jcb2,jcb3;
	DefaultListModel lm1,lm2;
	JList jls1,jls2;
	JButton jb3,jb4,jb5,jb6,jb7,jb8,jb9;
	JLabel jl2,jl3,jl4,jl5,jl6;
	static JCheckBox jc;
	JScrollPane jsp1,jsp2;
	JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7,jp8,jp9,jp10;
	JRadioButton jrb1,jrb2,jrb3;
	JTextField jtf1;
	String dsn;
	ButtonGroup bg=new ButtonGroup();
	String query="select";
	String from="from";
	String where="where";
	

	QueryBuilderDemo(String arg)	
	{	
		
		// Initializing Variables		

		jl2=new JLabel("Tables");
		
		jcb1=new JComboBox();
		jcb1.addActionListener(this);
		jcb2=new JComboBox();
		jcb2.addActionListener(this);
		jcb3=new JComboBox();
		jcb3.addActionListener(this);
		jcb3.addItem("=");
		jcb3.addItem("!=");
		jcb3.addItem("<");
		jcb3.addItem("<=");
		jcb3.addItem(">");
		jcb3.addItem(">=");
		jcb3.addItem("like");
		jcb3.addItem("between");
		jcb3.addItem("in");
		jcb3.addItem("soundex");
		
		lm1=new DefaultListModel();
		jls1=new JList(lm1);
		lm2=new DefaultListModel();
		jls2=new JList(lm2);
		
		jsp1=new JScrollPane(jls1,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		jsp2=new JScrollPane(jls2,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		jb3=new JButton("Refresh");
		jb3.setActionCommand("Refresh");
		jb3.addActionListener(this);

		jb4=new JButton(">>");
		jb4.setActionCommand("Add");
		jb4.addActionListener(this);

		jb5=new JButton("<<");
		jb5.setActionCommand("Remove");
		jb5.addActionListener(this);

		jb6=new JButton("<<<");
		jb6.setActionCommand("RemoveAll");
		jb6.addActionListener(this);

		jb7=new JButton("Go");
		jb7.setActionCommand("Go");
		jb7.addActionListener(this);

		jb8=new JButton("Query");
		jb8.setActionCommand("Query");
		jb8.addActionListener(this);

		jb9=new JButton("Exit");
		jb9.setActionCommand("Exit");
		jb9.addActionListener(this);
		
		jc=new JCheckBox("Order By");
		jc.setActionCommand("OrderBy");
		jc.addItemListener(this);
		
		jl3=new JLabel("Criteria");
		jl4=new JLabel("Fields:");
		jl5=new JLabel("Operators");
		jl6=new JLabel("Values");
		
		jrb1=new JRadioButton("AND");
		jrb2=new JRadioButton("OR");
		jrb3=new JRadioButton("NONE",true);
		
		bg=new ButtonGroup();
		bg.add(jrb1);
		bg.add(jrb2);
		bg.add(jrb3);


		jtf1=new JTextField();

		
		//Tables ComboBox
	
		jp1=new JPanel(new FlowLayout());
		jp1.add(jl2);
		jp1.add(jcb1);
		

		// Middle Buttons(Refresh,>>,<<,>>>)
		
		jp2=new JPanel(new GridLayout(5,1));
		jp2.add(jb3);
		jp2.add(jb4);			
		jp2.add(jb5);
		jp2.add(jb6);
		jp2.add(jc);
		

		// TextAreas and Middle Buttons

		jp3=new JPanel(new FlowLayout());
		jp3.add(jsp1);
		jp3.add(jp2);
		jp3.add(jsp2);
			


		// Complete Tables Panel
		
		jp9=new JPanel(new GridLayout(2,1));
		jp9.add(jp1);
		jp9.add(jp3);
		

		// Criteria Panel

		// Fields, Operators, Values		

		jp4=new JPanel(new GridLayout(3,2));
		jp4.add(jl4);
		jp4.add(jcb2);
		jp4.add(jl5);
		jp4.add(jcb3);
		jp4.add(jl6);
		jp4.add(jtf1);
		

		jp5=new JPanel(new GridLayout(2,1));
		jp5.add(jl3);
		jp5.add(jp4);


		// RadioButtons		

		jp6=new JPanel(new GridLayout(3,1));
		jp6.add(jrb1);
		jp6.add(jrb2);
		jp6.add(jrb3);


		// Go, Exit, Query Buttons
	
		jp7=new JPanel(new GridLayout(3,1));
		jp7.add(jb7);
		jp7.add(jb8);
		jp7.add(jb9);


		// Complete Criteria Panel

		jp8=new JPanel(new FlowLayout());
		jp8.add(jp5);
		jp8.add(jp6);
		jp8.add(jp7);


		setLayout(new GridLayout(2,1));
		getContentPane().add(jp9);
		getContentPane().add(jp8);

		setTitle("Choose Tables");
		setSize(400,600);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		pack();

		
		//Database Connection & Records Retrieval

/*		try
		{	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection c=DriverManager.getConnection("jdbc:odbc:"+dsn,"system","admin");
			DatabaseMetaData dbmd=c.getMetaData();
			ResultSet rs=dbmd.getTables(null,null,null,new String[]{"TABLES"});
			while(rs.next())
			{	jcb1.addItem(rs.getString(3));
			}
			c.close()
		}
		catch(Exception ce)
		{	System.out.println(ce);
		}

*/	}
	public void actionPerformed(ActionEvent ae)
	{	
		if(ae.getActionCommand()=="Exit")
		{	System.exit(0);
		}
		else if(ae.getActionCommand()=="Add")
		{	Object selItem = jls1.getSelectedValue();
			lm2.addElement(selItem);
		}
		else if(ae.getActionCommand()=="Remove")
		{	int index=jls2.getSelectedIndex();
			lm2.remove(index);
		}
		else if(ae.getActionCommand()=="RemoveAll")
		{	lm2.removeAllElements();
		}
		else if(ae.getActionCommand()=="Go")
		{	Object obj[]=jls2.getSelectedValues();
			query=query+" "+lm2.getElementAt(0).toString();
			if(obj.length>0)
			{	for(int i=1;i<obj.length;i++)
				{	query=query+" , "+obj[i].toString();	
				}
			}
			from=from+jcb1.getSelectedItem().toString();
			GoResult gr=new GoResult(query,from,where,dsn);
			setVisible(false);
			gr.setVisible(true);
		}
		else if(ae.getActionCommand()=="Query")
		{	Object field[]=lm2.toArray();
			for(int i=0;i<field.length;i++)	
			{	query=query+field[i].toString();
			}
		}
		else if(ae.getActionCommand()=="AND")
		{	where=where+" and "+jcb2.getSelectedItem().toString()+jcb3.getSelectedItem().toString()+jtf1.getText();
		}
		else if(ae.getActionCommand()=="OR")
		{	where=where+" or "+jcb2.getSelectedItem().toString()+jcb3.getSelectedItem().toString()+jtf1.getText();
		}
		else if(ae.getActionCommand()=="NONE")
		{	where=where+" "+jcb2.getSelectedItem().toString()+jcb3.getSelectedItem().toString()+jtf1.getText();
		}
	}
	public void itemStateChanged(ItemEvent ie)
	{	if(ie.getSource()==jcb1)
		{	from=from+jcb1.getSelectedItem().toString()+" ";
		}
		else if(ie.getSource()==jc)
		{	if(ie.getStateChange()==1)
			{	OrderBy ob=new OrderBy();
				ob.setVisible(true);
			}
		}
	}
	public static void main(String []arg)
	{	new QueryBuilderDemo("mydsn");
	}
	
}

Here the code for the second JFrame

import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;

public class OrderBy extends JFrame implements ActionListener
{	JLabel jl1;
	JComboBox jcb;
	JRadioButton jrb1,jrb2;
	ButtonGroup bg;
	JButton jb1,jb2,jb3;
	JPanel jp1,jp2;
	OrderBy()
	{	JLabel jl1=new JLabel("ORDER By");
		JComboBox jcb=new JComboBox();
		JButton jb1=new JButton("ADD");
		jb1.setActionCommand("ADD");
		JButton jb2=new JButton("OK");
		jb2.setActionCommand("OK");
		JButton jb3=new JButton("CANCEL");
		jb3.setActionCommand("CANCEL");
		JRadioButton jrb1=new JRadioButton("ASC",true);
		JRadioButton jrb2=new JRadioButton("DESC");
		ButtonGroup bg=new ButtonGroup();

		JPanel jp1=new JPanel(new GridLayout(3,1));
		jp1.add(jb1);
		jb1.addActionListener(this);
		jp1.add(jb2);
		jb2.addActionListener(this);
		jp1.add(jb3);
		jb3.addActionListener(this);

		bg.add(jrb1);
		bg.add(jrb2);
		
		JPanel jp2=new JPanel();
		jp2.add(jrb1);
		jp2.add(jrb2);


		getContentPane().add(jl1);
		getContentPane().add(jcb);
		getContentPane().add(jp2);
		getContentPane().add(jp1);
		

		setTitle("ORDER BY");
		setLayout(new FlowLayout());
		setSize(250,200);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		pack();
	}
	
	public void actionPerformed(ActionEvent ae)
	{	if(ae.getActionCommand()=="ADD")
		{
		}
		else if(ae.getActionCommand()=="CANCEL")
		{	dispose();
			QueryBuilderDemo.jc.setSelected(false);
		}
		else if(ae.getActionCommand()=="OK")
		{	dispose();
			QueryBuilderDemo.jc.setSelected(true);
		}
	}
	public static void main(String []arg)  throws Exception
	{	new OrderBy();
	}
	
}

Please put that in code tags! [*CODE] [*/CODE] (without *s)

Here's the code for the first JFrame, for only JCheckBox.

jc=new JCheckBox("Order By");
jc.setActionCommand("OrderBy");
jc.addItemListener(this);

public void itemStateChanged(ItemEvent ie)
{ if(ie.getSource()==jcb1)
{ from=from+jcb1.getSelectedItem().toString()+" ";
}
else if(ie.getSource()==jc)
{ if(ie.getStateChange()==1)
{ OrderBy ob=new OrderBy();
ob.setVisible(true);


Here the code for the second JFrame
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;

public class OrderBy extends JFrame implements ActionListener
{ JLabel jl1;
JComboBox jcb;
JRadioButton jrb1,jrb2;
ButtonGroup bg;
JButton jb1,jb2,jb3;
JPanel jp1,jp2;
OrderBy()
{ JLabel jl1=new JLabel("ORDER By");
JComboBox jcb=new JComboBox();
JButton jb1=new JButton("ADD");
jb1.setActionCommand("ADD");
JButton jb2=new JButton("OK");
jb2.setActionCommand("OK");
JButton jb3=new JButton("CANCEL");
jb3.setActionCommand("CANCEL");
JRadioButton jrb1=new JRadioButton("ASC",true);
JRadioButton jrb2=new JRadioButton("DESC");
ButtonGroup bg=new ButtonGroup();

JPanel jp1=new JPanel(new GridLayout(3,1));
jp1.add(jb1);
jb1.addActionListener(this);
jp1.add(jb2);
jb2.addActionListener(this);
jp1.add(jb3);
jb3.addActionListener(this);

bg.add(jrb1);
bg.add(jrb2);

JPanel jp2=new JPanel();
jp2.add(jrb1);
jp2.add(jrb2);


getContentPane().add(jl1);
getContentPane().add(jcb);
getContentPane().add(jp2);
getContentPane().add(jp1);


setTitle("ORDER BY");
setLayout(new FlowLayout());
setSize(250,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pack();
}

public void actionPerformed(ActionEvent ae)
{ if(ae.getActionCommand()=="ADD")
{
}
else if(ae.getActionCommand()=="CANCEL")
{ dispose();
QueryBuilderDemo.jc.setSelected(false);
}
else if(ae.getActionCommand()=="OK")
{ dispose();
QueryBuilderDemo.jc.setSelected(true);
}
}
public static void main(String []arg) throws Exception
{ new OrderBy();
}

}

Well at least you tried. Those "*" don't belong in the code tags and cut-n-pasting the unformatted code from an earlier post, rather than pasting the original code, doesn't help, as you can see above. So, I went back and added code tags to the "original" code post, as well.

In this line

QueryBuilderDemo.jc.setSelected(true);

You need an actual instance of QueryBuilderDemo, not the Class.

so you mean that i should not define the JCheckBox variable as 'static'. coz dat is wat i've done.
Do let me know.

Okay, now I've seen that you've declared it static. To tell you the truth, I wouldn't though. It's just bad form, at the very least. Anyway ...

Call validate() and or repaint() on the contentPane of the frame. Another note is the description of the "setSelected" method.

setSelected

public void setSelected(boolean b)

Sets the state of the button. Note that this method does not trigger an actionEvent. Call doClick to perform a programatic action change.

Parameters:
b - true if the button is selected, otherwise false

U the man....

thanx, i got it fixed with th doClick()...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.