LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java Click me only once (https://www.linuxquestions.org/questions/programming-9/java-click-me-only-once-187268/)

alaios 05-29-2004 05:32 AM

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?

Looking_Lost 05-29-2004 08:59 AM

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

alaios 05-29-2004 11:16 AM

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
        }


Looking_Lost 05-29-2004 01:40 PM

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);
  }


alaios 05-30-2004 09:23 AM

thx
 
really thx.....works great


All times are GMT -5. The time now is 11:18 PM.