Results 1 to 8 of 8
  1. #1
    elieN is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default enabling keystrokes

    hey, can anyone please help me why this code isn't accepting keyboard strokes and how to fix it?
    as it has been pointed out, this is INDEED a cross post between here and here for the obvious reason that my post remains unanswered.
    I apologize for any inconvenience; however, when I read FAQ's section about cross posting I thought it stands for posting on multiple categories on same forum.
    Java Code:
    import java.awt.* ;
    import java.awt.event.* ;
    import javax.swing.* ;
     
    public class Calculator {
        public static void main( String arg[] ) {
            CalculatorFrame f = new CalculatorFrame() ;
            f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
            f.setVisible(true);
        }
    }
     
    class CalculatorFrame extends JFrame {
        public CalculatorFrame() {
            setTitle( "Calculator" ) ;
            Container c = getContentPane() ;
            CalculatorPanel p = new CalculatorPanel() ;
            c.add( p ) ;
            pack() ;
        }
    }
     
    class CalculatorPanel extends JPanel {
        private JLabel display ;
        private double result ;
        private String lastCommand ;
        private boolean start ;
        private JPanel panel ;
        
        public CalculatorPanel() {
            setLayout( new BorderLayout() ) ;
            
            result = 0 ;
            lastCommand = "=" ;
            start = true ;
            
            display = new JLabel( "0" ) ;
            add( display,BorderLayout.NORTH ) ;// add ( display, "North" ) ;
            
            ActionListener insert = new InsertAction() ;
            ActionListener command = new CommandAction() ;
            
            panel = new JPanel() ;
            
            panel.setLayout( new GridLayout( 4 , 4 , 5 , 5 ) ) ;
            
            addButton( "7" , insert ) ;
            addButton( "8" , insert ) ;
            addButton( "9" , insert ) ;
            addButton( "/" , command ) ;
            addButton( "4" , insert ) ;
            addButton( "5" , insert ) ;
            addButton( "6" , insert ) ;
            addButton( "*" , command ) ;
            addButton( "1" , insert ) ;
            addButton( "2" , insert ) ;
            addButton( "3" , insert ) ;
            addButton( "-" , insert ) ;
            addButton( "0" , insert ) ;
            addButton( "." , insert ) ;
            addButton( "=" , command ) ;
            addButton( "+" , command ) ;
            
            add( panel , BorderLayout.CENTER ) ;
            
        }
        
        private void addButton( String label , ActionListener listener ) {
            JButton b = new JButton( label ) ;
            b.addActionListener( listener ) ;
            panel.add(b);
        }
        
        private class InsertAction implements ActionListener {
            public void actionPerformed( ActionEvent e ) {
                String input = e.getActionCommand() ;
                if ( start ) {
                    display.setText( "" ) ;
                    start = false ;
                }
                display.setText( display.getText() + input ) ;
            }
        }
        
        private class CommandAction implements ActionListener {
            public void actionPerformed( ActionEvent e ) {
                String command = e.getActionCommand() ;
                if (start) {
                    if ( command.equals( "-" ) ) {
                        display.setText("-");
                        start = false ;
                    } else
                        lastCommand = command ;
                }
                
                else {
                    calculate(Double.parseDouble(display.getText() ) ) ;
                    lastCommand = command ;
                    start = true ;
                }
            }
            
            public void calculate( double x ) {
                if ( lastCommand.equals( "+" ) ) result += x ;
                else if ( lastCommand.equals( "-" ) ) result -= x ;
                else if ( lastCommand.equals( "*" ) ) result *= x ;
                else if ( lastCommand.equals( "/" ) ) result /= x ;
                else if ( lastCommand.equals( "=" ) ) result = x ;
                display.setText("" + result ) ;
            }
        }
    }
    Last edited by elieN; 05-28-2011 at 04:43 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

  3. #3
    elieN is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    well yes, its a different forum, I thought maybe there are other people who can help here, since I didn't get answered there :) You're a moderator on TWO different forums!
    Impressive, you must be really good, help please :D

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please read this link about the cross-post issue: BeForthrightWhenCrossPostingToOtherSites. Please reply regarding this issue, and I will be glad to help, pending reading your response.

  5. #5
    elieN is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    I edited my post, so do I get your help :)?

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    this is INDEED a cross post between here and here for the obvious reason that my post remains unanswered.
    Actually you where given the answer in your other posting.

    One suggestion was to use Actions.

    The other was to use Key Bindings.

    The two suggestions work well together and give you lots of flexibility.
    Last edited by camickr; 05-28-2011 at 06:09 PM.

  7. #7
    elieN is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    well that part I realized on my own, but trying 2 implement it I got more errors than I could chew, would you be kind enough?? please :D

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I doubt you had time to read the link I provide and understand what I suggested and then create a simple test program, in 6 minutes.

    I got more errors than I could chew
    Start simple. Create a frame with one button, one Action and one KeyBinding.

    This is called a Short, Self Contained, Correct Example. If it works great, if not then you have something simple to post.

    Then once you undertand the basics you implement the code in your real application.
    Last edited by camickr; 05-28-2011 at 06:31 PM.

Similar Threads

  1. Enabling Javascript
    By drewsonoffergus in forum Networking
    Replies: 1
    Last Post: 08-06-2011, 08:42 PM
  2. Replies: 1
    Last Post: 12-27-2010, 11:24 PM
  3. Send Keystrokes from Java to Notepad
    By ramyal in forum AWT / Swing
    Replies: 3
    Last Post: 02-18-2010, 06:09 PM
  4. Send keystrokes to iframe
    By nloding in forum New To Java
    Replies: 3
    Last Post: 08-29-2008, 05:06 PM
  5. Showing how to add Actions for KeyStrokes
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:17 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
  •