Results 1 to 2 of 2
  1. #1
    techbossmb is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default displaying a formatted output on a jTextField

    Please could anyone help me, i am having a bit of trouble with displaying a formatted output on a textfield. I actually registerd a mousedragged event to get x coordinate and doing some manipulations to display a double value on a textfield. But the problem is that instead of getting a value at a particular point, what i see is a string of values(initial values are also been displayed. Here is the code
    Java Code:
    private void jLabel2MouseDragged(java.awt.event.MouseEvent evt) {
             Component c = evt.getComponent();
             xC = c.getX()+evt.getX();
             double xCd = xC * 1.0;
             double acp;
             int ac;
            if((c.getX()+evt.getX()) <= 46){
                c.setLocation(46, 50);
                acp = ExperimentControl.experiment.getLength() * 0.0;
                ac = (int)acp;
               formatter.format("%.2f",acp);
                jTextField2.setText(formatter.toString());
                
    
            }
            else if((c.getX()+evt.getX()) >= 479){
                c.setLocation(479, 50);
                acp = ExperimentControl.experiment.getLength() * 1.0;
                ac = (int)acp;
                formatter.format("%.2f",acp);
                jTextField2.setText(formatter.toString());
            }
            else{
                c.setLocation( c.getX()+evt.getX(), 50 );
                acp = ((ExperimentControl.experiment.getLength())* (xCd - 46.0))/433;
                ac = (int)acp;
                formatter.format("%.2f",acp);
                jTextField2.setText(formatter.toString());
            }
    }

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    In an experiment, I could not reproduce "what i see is a string of values"
    and the layout manager was not cooperative. I had more success when
    I installed the label on the LayeredPane instead of the content pane.

    This could be done, I imagine, by replacing an existing "add(jLabel2);"
    with something like
    getRootPane().getLayeredPane().add(jLabel2,
    JLayeredPane.PALETTE_LAYER);
    jLabel2.setBounds( --initial location and size-- );



    ((PS. The code could be more readable and maintainable by reducing the
    if-else block to just set the values for two variable, say xLoc and factor.
    Then the common code could be extracted to a single instance:
    Java Code:
             c.setLocation(xLoc, 50);   // move label
             // display current value
             formatter.format("%.2f", 
                      ExperimentControl.experiment.getLength() * factor);
             jTextField2.setText(formatter.toString());
    This way, if the common code needs to change, there is one one place to change.
    There will be no bugs due to making different changes
    in different instances of the same code.))

Similar Threads

  1. Replies: 8
    Last Post: 08-13-2009, 05:23 AM
  2. Replies: 0
    Last Post: 06-29-2009, 03:40 PM
  3. Replies: 1
    Last Post: 12-22-2008, 12:24 AM
  4. No output displaying
    By Rgfirefly24 in forum New To Java
    Replies: 6
    Last Post: 04-27-2008, 09:37 PM
  5. Replies: 5
    Last Post: 01-17-2008, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •