LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2007, 06:30 AM   #1
xface66
LQ Newbie
 
Registered: Dec 2006
Posts: 29

Rep: Reputation: 15
2 jtextField must work like radio button groups


hi everyone,

i have 2 jtextfields. but user will be able to input one of them.

when user inputs textfield1, textfield2 will be uneditable, or user can input textfield2 and textfield1 will be uneditable.

how can i do this? is there a way like grouping radio buttons?
 
Old 09-11-2007, 07:51 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
This is not the very accurate answer to your problem (nor the very best way to do this), but it will put you on track. I wrote it fast, so I can't say for sure it will compile. The basic idea is, add a listener to both JTextField. When, for example, the user clicks on the second JTextField, set the first JTextField focusable method to "false". Something like this:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
public class Test extends JFrame {
	
	private static JTextField jtf1 = new JTextField();
	private static JTextField jtf2 = new JTextField();
	
	public Test(){
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		add(jtf1, BorderLayout.NORTH);
		add(jtf2, BorderLayout.SOUTH);
		
		jtf1.addMouseListener(new MouseAdapter() {
	    	public void mouseClicked(MouseEvent e) {
	    		jtf1.setFocusable(true);
			jtf2.setFocusable(false);
	    	}	
		});
		
		jtf2.addMouseListener(new MouseAdapter() {
	    	public void mouseClicked(MouseEvent e) {
	    		jtf2.setFocusable(true);
			jtf1.setFocusable(false);
	    	}	
		});
				
		pack();
		setVisible(true);		
	}
		
	
	public static void main(String[] args) {
		new Test();
	}
}
 
Old 09-11-2007, 09:54 AM   #3
xface66
LQ Newbie
 
Registered: Dec 2006
Posts: 29

Original Poster
Rep: Reputation: 15
thanks for your reply but what if user wants to change if his mind after selecting one of the fields he doesnt input it and wants to input the other?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
radio button: how to detect which was last checked in a multi group of buttons rblampain Programming 2 09-11-2007 08:42 AM
how can I find the length of all radio button? rblampain Programming 3 08-12-2007 09:35 PM
After battery died, radio button for wireless doesn't work! Shioni Linux - Hardware 3 11-16-2006 02:01 PM
Segmentation Fault after adding a radio button into my compiled app/ Gnarg Programming 2 10-18-2005 06:47 AM
php radio button question jabfinger Programming 5 08-14-2005 10:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration