LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-29-2004, 05:32 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
Java Click me only once


Hi
Let's take the following example....

I need two checkbox .. One for bold letters and one for italics...

I have written the following function

Code:
 
    public boolean action(Event evt,Object arg){
        if (evt.target instanceof Checkbox)
            handleCheck((Checkbox)evt.target,evt);
        if (evt.target instanceof Choice){
             handleChoice((Choice)evt.target,evt);
         }
        return false;
    }

The prolem is that it is executed 2 times.. One for mouse up event and one for mouse down event


What do u suggest me?


The following is the function called when a checkbox is clicked

Code:
 
    void handleCheck(Checkbox c,Event evt){
        if ( (c.getLabel()=="Bold")  ){
            TextFont = new Font(FontName,Font.BOLD+FontStyle,FontSize);        
            WriteText.setFont(TextFont);
        }else if ( (c.getLabel()=="Italic") ){
            TextFont = new Font(FontName,Font.ITALIC+FontStyle,FontSize);        
            WriteText.setFont(TextFont);
            // ?You must add code for kseclick
        }         
    }
The problem is that when i click theBold button i loose the italic attribute and vice-versa

What i must do?
 
Old 05-29-2004, 08:59 AM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Keeping it nice and simple and assuming box1 and box2 are both JCheckbox you could define an event listener like this

Code:
private Action checkboxAction=new AbstractAction(){

            public void actionPerformed(ActionEvent evt){
               
                JCheckBox eventSource=(JCheckBox)evt.getSource();

		if(eventSource==box1)
			handleBox1Event();
                else
                  if(eventSource==box2)
			handleBox2Event();     
		 }   	
                    
             };

box1.addActionListener(checkBoxAction);
box2.addActionListener(checkBoxAction);
then call the methods that handle what to do with each box

Code:
private void handleBox1Event(){

 
    if(box1.isSelected())
       System.out.println("You selected the box1");
    else
       System.out.println("You deselected the box1");

   }

  private void handleBox2Event(){
 
	if(box2.isSelected())
           System.out.println("You selected the box2");
    else
           System.out.println("You deselected the box2");

    }
that sort of thing
 
Old 05-29-2004, 11:16 AM   #3
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
thx

really thx.....
But how i can preserve the bold and italic attributes concurrently?

Code:
   void handleCheck(Checkbox c,Event evt){
        if ( (c.getLabel()=="Bold")  ){
            if (( c.getState()))
                   TextFont = new Font(FontName,Font.BOLD+FontStyle,FontSize);     
            else
                    TextFont = new Font(FontName,FontStyle,FontSize);     
            WriteText.setFont(TextFont);
        }else if ( (c.getLabel()=="Italic") ){
            if (( c.getState()))
                TextFont = new Font(FontName,Font.ITALIC+FontStyle,FontSize);        
            else
                TextFont = new Font(FontName,FontStyle,FontSize);        
            WriteText.setFont(TextFont);
            // ?You must add code for kseclick
        }
 
Old 05-29-2004, 01:40 PM   #4
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Basically you subtract ( or add) the value of the style you want to remove/add to/ from the current style and use that new value for the style, example of one of numerous ways is easier to show than explain:


Code:
  private void handleBox1Event(){
  
    Font currentFont=writeText.getFont();
    Font newFont;
     
    if(! currentFont.isBold())
	newFont=currentFont.deriveFont(currentFont.getStyle() + Font.BOLD);    
      else
         newFont=currentFont.deriveFont(currentFont.getStyle() - Font.BOLD);
   
    writeText.setFont(newFont);
}

  private void handleBox2Event(){
 
	Font currentFont=writeText.getFont();
        
        Font newFont;
         
	if( ! currentFont.isItalic()) 
	     newFont=currentFont.deriveFont(currentFont.getStyle() + Font.ITALIC); 
	 else
             newFont=currentFont.deriveFont(currentFont.getStyle() - Font.ITALIC); 

	writeText.setFont(newFont);
   }
 
Old 05-30-2004, 09:23 AM   #5
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Talking thx

really thx.....works great
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
mouse: single-click becomes double-click kinzlaw Linux - Hardware 2 08-24-2005 07:55 PM
java-gnome Menu click event(nothing happens) rmanocha Programming 0 09-18-2004 03:15 PM
Gnome 2.6.1: Single Click acts like Double Click Der_tolle_Emil Linux - Software 2 09-13-2004 08:59 AM
executable jar in java (cannot double click) spyghost Programming 3 09-29-2003 09:13 AM
upgraded wm --> no right-click ataraktos Linux - Software 0 03-05-2002 04:50 PM

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

All times are GMT -5. The time now is 11:34 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