Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 24 of 24

Thread: Compress my Code!

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [SOLVED] Compress my Code!

    Hi;

    I have this code:

    public class Lab3 extends JFrame {
     
        /**
         * Creates new form Lab3
         */
        public Lab3() {
            initComponents();
            txtArea.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent event) {
                    String usrInput = txtArea.getText();
                    usrInput = usrInput.trim();
     
                    txtAreaChange.setText(usrInput);
     
                }
            });
     
            cmdReset.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent event) {
                    String usrResult = txtAreaChange.getText();
     
                    txtAreaChange.setText("");
                    txtArea.setText("");
                }
            });
     
            ButtonActions btnAct = new ButtonActions();
            btnSmall.addMouseListener(btnAct);
        }
     
        public class ButtonActions implements MouseListener{
     
            public void mouseClicked(MouseEvent event) {
                String usrInput = txtArea.getText();
                usrInput = usrInput.trim();
                txtAreaChange.setText(usrInput);
                Font oldFont = txtAreaChange.getFont();
                Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 12);
                txtAreaChange.setFont(newFont);
            }
     
            public void mousePressed(MouseEvent event) {
            }
     
            public void mouseReleased(MouseEvent event) {
            }
     
            public void mouseEntered(MouseEvent event) {
            }
     
            public void mouseExited(MouseEvent event) {
            }
        }

    I am currently using NetBeans IDE and using a the JFrame Form to create a GUI.

    Now I know that it is the easy way out, but I'm going to need to refresh my memory based on the layout classes in the weeks to come, so far I have settled with this.

    Now my GUI looks like this:



    The process may not be clear if not already.

    In this next screenshot:



    Now instead of myself copy and pasting the ButtonActions Method over and over again for the remaining radio/checkbox buttons.

    I want to be able to incorporate an If Statement within the ButtonActions Method, inside the mouseClicked Method. To be able to indicate which button is clicked and perform the necessary code given within the block.(Implementing ActionListener as well)


    variables:

    btnSmall = Small Radio Button
    btnMedium = Medium Radio Button
    btnLarge = Large Radio Button


    PS.C:

    void mouseClicked(MouseEvent event)
    {
    String usrInput = txtArea.getText();
    usrInput = usrInput.trim();
    txtAreaChange.setText(usrInput);
    Font oldFont = txtAreaChange.getFont();

    if(event.getSource()==btnSmall)
    {
    Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 12);
    txtAreaChange.setFont(newFont);
    }
    else if(event.getSource()==btnMedium)
    {
    Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 14);
    txtAreaChange.setFont(newFont);
    }
    else if(event.getSource()==btnLarge)
    {
    Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 24);
    txtAreaChange.setFont(newFont);
    }
    else
    {
    keep txtAreaChange the same.
    }

    I'm not sure if this is being clear enough. If any clarity is needed I will respond a.s.a.p.

    Thank you for looking at this post!
    Last edited by aStudentofJava; March 4th, 2012 at 08:16 PM. Reason: Solved!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compress my Code!

    I want to be able to incorporate an If Statement
    Can you explain what your problem is? The last posted code looks like it does what you are asking about.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress my Code!

    The last posted code was just a pseudo I came up with.
    I tried doing what I typed in there but I get an error when I tried to event.getSource()==btnSmall.
    It says it cannot recognize it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compress my Code!

    What was not recognized?

    Make sure all variables are in scope.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress my Code!

    I tried this:

        public class ButtonActions implements MouseListener, ActionListener {
     
            public void mouseClicked(MouseEvent event) {
                String usrInput = txtArea.getText();
                usrInput = usrInput.trim();
                txtAreaChange.setText(usrInput);
                Font oldFont = txtAreaChange.getFont();
                public void actionPerformed(ActionEvent eventq)
                {
                if (eventq.getSource() == btnSmall) {
                    Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 12);
                    txtAreaChange.setFont(newFont);
                } else if (eventq.getSource() == btnMedium) {
                    Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 14);
                    txtAreaChange.setFont(newFont);
                } else if (eventq.getSource() == btnLarge) {
                    Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 24);
                    txtAreaChange.setFont(newFont);
                }
                }
            }
     
            public void mousePressed(MouseEvent event) {
            }
     
            public void mouseReleased(MouseEvent event) {
            }
     
            public void mouseEntered(MouseEvent event) {
            }
     
            public void mouseExited(MouseEvent event) {
            }
        }

    I get an error over ButtonActions saying I have not overwritten the mouseExited method yet.
    Also I get an error within the mouseClicked method which are my variables becoming unrecognized, and I'm unable to create Font Objects as well.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compress my Code!

    saying I have not overwritten the mouseExited method
    You must override ALL methods in an interface that you implement.

    my variables becoming unrecognized,
    What variables? Are they defined and are their definitions in scope for where you are trying to use them.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress my Code!

    These are the list of variables that I am using:

        private javax.swing.JCheckBox btnBold;
        private javax.swing.JRadioButton btnCentre;
        private javax.swing.JCheckBox btnItalic;
        private javax.swing.JRadioButton btnLarge;
        private javax.swing.JRadioButton btnLeft;
        private javax.swing.JRadioButton btnMedium;
        private javax.swing.JRadioButton btnRight;
        private javax.swing.JRadioButton btnSmall;
        private javax.swing.JComboBox cmbBack;
        private javax.swing.JComboBox cmbFore;
        private javax.swing.JButton cmdExit;
        private javax.swing.JButton cmdReset;
        private javax.swing.ButtonGroup grpAlign;
        private javax.swing.ButtonGroup grpTxtSize;
        private javax.swing.JLabel lblAign;
        private javax.swing.JLabel lblBackground;
        private javax.swing.JLabel lblChangeTxt;
        private javax.swing.JLabel lblFont;
        private javax.swing.JLabel lblForeground;
        private javax.swing.JLabel lblTextSize;
        private javax.swing.JPanel pnlBot;
        private javax.swing.JPanel pnlMid;
        private javax.swing.JPanel pnlRight;
        private javax.swing.JPanel pnlTopLeft;
        private javax.swing.JTextField txtArea;
        private javax.swing.JTextField txtAreaChange;

    These are all in the same class.

    After I implemented the MouseListener class for the ButtonsAction method I put all the necessary methods within that method.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compress my Code!

    Please post the full text of the error messages.
    If the compiler says there is an error, I believe the compiler. I need to see the full text of what the compiler is saying.
    I can not tell how the bits and pieces of your code are connected.

    I put all the necessary methods within that method.
    You can not nest method definitions within other method definitions.

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Compress my Code!

    If possible, paste the whole code here so that we could see the scope. Otherwise, paste the exception/error message here.

  10. #10
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress my Code!

     
     
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.Font;
     
    public class Lab3 extends JFrame {
     
        /**
         * Creates new form Lab3
         */
        public Lab3() {
            initComponents();
     
            txtAreaActions txtAreaAct = new txtAreaActions();
            txtArea.addActionListener(txtAreaAct);
     
            clearAll clear = new clearAll();
            cmdReset.addActionListener(clear);
     
            exit exitWindow = new exit();
            cmdExit.addActionListener(exitWindow);
     
            ButtonActions btnAct = new ButtonActions();
            btnSmall.addMouseListener(btnAct);
        }
     
        public class txtAreaActions implements ActionListener {
     
            public void actionPerformed(ActionEvent event) {
                String usrInput = txtArea.getText();
                usrInput = usrInput.trim();
     
                txtAreaChange.setText(usrInput);
     
            }
        }
     
        public class clearAll implements ActionListener{
            public void actionPerformed(ActionEvent event)
            {
                String  usrResult = txtAreaChange.getText();
     
                txtAreaChange.setText("");
                txtArea.setText("");
            }
        }
     
        public class exit extends JOptionPane implements ActionListener{
            public void actionPerformed(ActionEvent event)
            {
            int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to Exit?", "Exit", JOptionPane.YES_NO_OPTION);
            if(answer==0)
            {
               System.exit(answer);
            }
            }
        }
     
        public class ButtonActions implements MouseListener, ActionListener{
     
            public void mouseClicked(MouseEvent event) {
                public void actionPerformed(ActionEvent event)
                {
                String usrInput = txtArea.getText();
                usrInput = usrInput.trim();
                txtAreaChange.setText(usrInput);
                Font oldFont = txtAreaChange.getFont();
                if(event.getSource()==btnSmall)
                {
                    Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 12);
                    txtAreaChange.setFont(newFont);
                }
                }
     
     
            public void mousePressed(MouseEvent event) {
            }
     
            public void mouseReleased(MouseEvent event) {
            }
     
            public void mouseEntered(MouseEvent event) {
            }
     
            public void mouseExited(MouseEvent event) {
            }
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            grpTxtSize = new javax.swing.ButtonGroup();
            grpAlign = new javax.swing.ButtonGroup();
            pnlTopLeft = new javax.swing.JPanel();
            lblTextSize = new javax.swing.JLabel();
            lblAign = new javax.swing.JLabel();
            lblChangeTxt = new javax.swing.JLabel();
            btnSmall = new javax.swing.JRadioButton();
            btnMedium = new javax.swing.JRadioButton();
            btnLarge = new javax.swing.JRadioButton();
            btnLeft = new javax.swing.JRadioButton();
            btnCentre = new javax.swing.JRadioButton();
            btnRight = new javax.swing.JRadioButton();
            txtArea = new javax.swing.JTextField();
            pnlMid = new javax.swing.JPanel();
            txtAreaChange = new javax.swing.JTextField();
            pnlBot = new javax.swing.JPanel();
            cmdReset = new javax.swing.JButton();
            cmdExit = new javax.swing.JButton();
            pnlRight = new javax.swing.JPanel();
            lblFont = new javax.swing.JLabel();
            btnBold = new javax.swing.JCheckBox();
            btnItalic = new javax.swing.JCheckBox();
            lblForeground = new javax.swing.JLabel();
            cmbFore = new javax.swing.JComboBox();
            lblBackground = new javax.swing.JLabel();
            cmbBack = new javax.swing.JComboBox();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Lab 3 - Jonathan Tesoro");
     
            lblTextSize.setText("Text Size:");
     
            lblAign.setText("Alignment:");
     
            lblChangeTxt.setText("Change Text:");
     
            grpTxtSize.add(btnSmall);
            btnSmall.setText("Small");
     
            grpTxtSize.add(btnMedium);
            btnMedium.setText("Medium");
     
            grpTxtSize.add(btnLarge);
            btnLarge.setText("Large");
     
            grpAlign.add(btnLeft);
            btnLeft.setText("Left");
     
            grpAlign.add(btnCentre);
            btnCentre.setText("Centre");
     
            grpAlign.add(btnRight);
            btnRight.setText("Right");
     
            javax.swing.GroupLayout pnlTopLeftLayout = new javax.swing.GroupLayout(pnlTopLeft);
            pnlTopLeft.setLayout(pnlTopLeftLayout);
            pnlTopLeftLayout.setHorizontalGroup(
                pnlTopLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlTopLeftLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(pnlTopLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblTextSize)
                        .addComponent(lblAign)
                        .addComponent(lblChangeTxt))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(pnlTopLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnlTopLeftLayout.createSequentialGroup()
                            .addGroup(pnlTopLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btnSmall)
                                .addComponent(btnLeft))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(pnlTopLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btnMedium)
                                .addComponent(btnCentre))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(pnlTopLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btnRight)
                                .addComponent(btnLarge)))
                        .addComponent(txtArea, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            pnlTopLeftLayout.setVerticalGroup(
                pnlTopLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlTopLeftLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(pnlTopLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblTextSize)
                        .addComponent(btnSmall)
                        .addComponent(btnMedium)
                        .addComponent(btnLarge))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(pnlTopLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblAign)
                        .addComponent(btnLeft)
                        .addComponent(btnCentre)
                        .addComponent(btnRight))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(pnlTopLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblChangeTxt)
                        .addComponent(txtArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            txtAreaChange.setEditable(false);
            txtAreaChange.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
     
            javax.swing.GroupLayout pnlMidLayout = new javax.swing.GroupLayout(pnlMid);
            pnlMid.setLayout(pnlMidLayout);
            pnlMidLayout.setHorizontalGroup(
                pnlMidLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlMidLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(txtAreaChange)
                    .addContainerGap())
            );
            pnlMidLayout.setVerticalGroup(
                pnlMidLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlMidLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(txtAreaChange)
                    .addContainerGap())
            );
     
            cmdReset.setMnemonic('R');
            cmdReset.setText("Reset");
     
            cmdExit.setMnemonic('x');
            cmdExit.setText("Exit");
     
            javax.swing.GroupLayout pnlBotLayout = new javax.swing.GroupLayout(pnlBot);
            pnlBot.setLayout(pnlBotLayout);
            pnlBotLayout.setHorizontalGroup(
                pnlBotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlBotLayout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addComponent(cmdReset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addComponent(cmdExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(74, 74, 74))
            );
            pnlBotLayout.setVerticalGroup(
                pnlBotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBotLayout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnlBotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmdReset)
                        .addComponent(cmdExit))
                    .addContainerGap())
            );
     
            lblFont.setText("Font Options:");
     
            btnBold.setText("Bold");
     
            btnItalic.setText("Italic");
     
            lblForeground.setText("Foreground:");
     
            cmbFore.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Red", "Blue", "Green", "Black" }));
     
            lblBackground.setText("Background:");
     
            cmbBack.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Grey", "Yellow", "Pink", "Magenta" }));
     
            javax.swing.GroupLayout pnlRightLayout = new javax.swing.GroupLayout(pnlRight);
            pnlRight.setLayout(pnlRightLayout);
            pnlRightLayout.setHorizontalGroup(
                pnlRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlRightLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(pnlRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cmbFore, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblForeground)
                        .addComponent(btnItalic)
                        .addComponent(btnBold)
                        .addComponent(lblFont)
                        .addComponent(lblBackground)
                        .addComponent(cmbBack, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(24, Short.MAX_VALUE))
            );
     
            pnlRightLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cmbBack, cmbFore});
     
            pnlRightLayout.setVerticalGroup(
                pnlRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlRightLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(lblFont)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(btnBold)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(btnItalic)
                    .addGap(18, 18, 18)
                    .addComponent(lblForeground)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(cmbFore, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(lblBackground)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(cmbBack, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(80, Short.MAX_VALUE))
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(pnlTopLeft, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(pnlBot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(pnlMid, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(pnlRight, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(38, 38, 38))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(pnlTopLeft, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(pnlMid, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(18, 18, 18)
                            .addComponent(pnlBot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(82, 82, 82))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(pnlRight, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(88, 88, 88))))
            );
     
            pack();
        }// </editor-fold>                        
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /*
             * Set the Nimbus look and feel
             */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /*
             * If Nimbus (introduced in Java SE 6) is not available, stay with the
             * default look and feel. For details see
             * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Lab3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Lab3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Lab3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Lab3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new Lab3().setVisible(true);
                }
            });
     
     
        }
        // Variables declaration - do not modify                     
        private javax.swing.JCheckBox btnBold;
        private javax.swing.JRadioButton btnCentre;
        private javax.swing.JCheckBox btnItalic;
        private javax.swing.JRadioButton btnLarge;
        private javax.swing.JRadioButton btnLeft;
        private javax.swing.JRadioButton btnMedium;
        private javax.swing.JRadioButton btnRight;
        private javax.swing.JRadioButton btnSmall;
        private javax.swing.JComboBox cmbBack;
        private javax.swing.JComboBox cmbFore;
        private javax.swing.JButton cmdExit;
        private javax.swing.JButton cmdReset;
        private javax.swing.ButtonGroup grpAlign;
        private javax.swing.ButtonGroup grpTxtSize;
        private javax.swing.JLabel lblAign;
        private javax.swing.JLabel lblBackground;
        private javax.swing.JLabel lblChangeTxt;
        private javax.swing.JLabel lblFont;
        private javax.swing.JLabel lblForeground;
        private javax.swing.JLabel lblTextSize;
        private javax.swing.JPanel pnlBot;
        private javax.swing.JPanel pnlMid;
        private javax.swing.JPanel pnlRight;
        private javax.swing.JPanel pnlTopLeft;
        private javax.swing.JTextField txtArea;
        private javax.swing.JTextField txtAreaChange;
        // End of variables declaration                   
    }
    Last edited by aStudentofJava; March 2nd, 2012 at 11:27 AM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compress my Code!

    Where is the full text of the error messages?


    I put all the necessary methods within that method.
    You can not nest method definitions within other method definitions.
    Last edited by Norm; March 2nd, 2012 at 11:27 AM.

  12. #12
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress my Code!

    Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
    at prog24178.Lab3.<init>(Lab3.java:24)
    at prog24178.Lab3$1.run(Lab3.java:382)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101 )
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 675)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)
    Caused by: java.lang.RuntimeException: Uncompilable source code - prog24178.Lab3.txtAreaActions.ButtonActions is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
    at prog24178.Lab3$txtAreaActions.<clinit>(Lab3.java:6 9)
    ... 16 more

    my apologizes.

  13. #13
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Compress my Code!

    public class ButtonActions implements MouseListener, ActionListener{

    public void mouseClicked(MouseEvent event) {
    public void actionPerformed(ActionEvent event)
    {
    String usrInput = txtArea.getText();
    usrInput = usrInput.trim();
    txtAreaChange.setText(usrInput);
    Font oldFont = txtAreaChange.getFont();
    if(event.getSource()==btnSmall)
    {
    Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 12);
    txtAreaChange.setFont(newFont);
    }
    }
    In your code, compiler is unable to find the abstract method actionPerformed(...) for ActionListener interface as it's nested inside the mouseClicked(...)
    Implement mouseClicked(...) and actionPerformed(...) separately.

  14. #14
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress my Code!

    How would I do that separately?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compress my Code!

    Can you use the javac compiler program to compile your program? The IDE's output does not show the source lines where the errors are.
    Here is a sample of the output from the javac program:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    .txtAreaActions.ButtonActions is not abstract and does not override abstract method mouseExited
    This part of the message says that your code does not correctly implement all the methods for the interface it implements. See post#6

  16. #16
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Compress my Code!

    public void actionPerformed(ActionEvent event){
    }
    public void mouseClicked(MouseEvent event){
    }

  17. #17
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress my Code!

       public class ButtonActions implements MouseListener, ActionListener {
     
            public void actionPerformed(ActionEvent event) {
                if (event.getSource() == btnSmall) {
     
                    Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 12);
                    txtAreaChange.setFont(newFont);
                }
            }
     
            public void mouseClicked(MouseEvent event) {
                String usrInput = txtArea.getText();
                usrInput = usrInput.trim();
                txtAreaChange.setText(usrInput);
                Font oldFont = txtAreaChange.getFont();
     
            }

    I'm not being able to instantiate the font class? :S

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compress my Code!

    I'm not being able to instantiate the font class
    Please post the full text of the error message.

  19. #19
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress my Code!

    Quote Originally Posted by Norm View Post
    Please post the full text of the error message.

    I found the issue on why it would not instantiate.

    public class Lab3 extends JFrame {
     
        /**
         * Creates new form Lab3
         */
        public Lab3() {
            initComponents();
     
            txtAreaActions txtAreaAct = new txtAreaActions();
            txtArea.addActionListener(txtAreaAct);
     
            clearAll clear = new clearAll();
            cmdReset.addActionListener(clear);
     
            exit exitWindow = new exit();
            cmdExit.addActionListener(exitWindow);
     
            ButtonActions btnAct = new ButtonActions();
            btnSmall.addMouseListener(btnAct);
            btnSmall.addActionListener(btnAct);
        }
     
        public class txtAreaActions implements ActionListener {
     
            public void actionPerformed(ActionEvent event) {
                String usrInput = txtArea.getText();
                usrInput = usrInput.trim();
     
                txtAreaChange.setText(usrInput);
     
            }
        }
     
        public class clearAll implements ActionListener {
     
            public void actionPerformed(ActionEvent event) {
                String usrResult = txtAreaChange.getText();
     
                txtAreaChange.setText("");
                txtArea.setText("");
            }
        }
     
        public class exit extends JOptionPane implements ActionListener {
     
            public void actionPerformed(ActionEvent event) {
                int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to Exit?", "Exit", JOptionPane.YES_NO_OPTION);
                if (answer == 0) {
                    System.exit(answer);
                }
            }
        }
     
        public class ButtonActions implements MouseListener, ActionListener {
     
            public void actionPerformed(ActionEvent event) {
                if (event.getSource() == btnSmall) {
                    Font oldFont = txtAreaChange.getFont();
                    Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 12);
                    txtAreaChange.setFont(newFont);
                }else if(event.getSource()==btnMedium){
                       Font oldFont = txtAreaChange.getFont();
                    Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), 16);
                    txtAreaChange.setFont(newFont);
    }
            }
     
            public void mouseClicked(MouseEvent event) {
                String usrInput = txtArea.getText();
                usrInput = usrInput.trim();
                txtAreaChange.setText(usrInput);
                Font oldFont = txtAreaChange.getFont();
     
            }
     
            public void mousePressed(MouseEvent event) {
            }
     
            public void mouseReleased(MouseEvent event) {
            }
     
            public void mouseEntered(MouseEvent event) {
            }
     
            public void mouseExited(MouseEvent event) {
            }
        }

    So far I have no errors. But I'm wondering how am I going to make it when I mouseClick, it will use the actionPerformed method to determine which Radio Button is pressed?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compress my Code!

    Why are you having the two listeners on a button?

  21. #21
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress my Code!

    Quote Originally Posted by Norm View Post
    Why are you having the two listeners on a button?
    I was just testing to see if it would make a difference.
    Is there a way for my 'above post' possible?

    "But I'm wondering how am I going to make it when I mouseClick, it will use the actionPerformed method to determine which Radio Button is pressed?"

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compress my Code!

    What happens when you execute the code an click on the button?
    Add printlns to each listener and see when/if they are called.

  23. #23
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress my Code!

    With the two listeners on the button and I added printlns to each Listener. It does print out.

    Foolish of myself, the default size output without a Radio Button being pressed is 12, and the font settings for the Small Radio Button is to change the font size to 12.

    It is now working and I tried adding the Medium radio button to do the same thing. So far so good! The if Statement is doing the job.


    Thank you Norm & Mr.777 for taking your time to help with my situation.

  24. #24
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Compress my Code!

    Good Luck and marks this thread as SOLVED.

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  2. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  3. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  4. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM
  5. Compress Ipv6 Addresses
    By abhay8nitt in forum Java Networking
    Replies: 5
    Last Post: May 20th, 2010, 06:58 AM