-5

I am building a standalone application which hides the text onto the image (Stenography)

I am novice and facing error while implementing the fuction of GridBagLayout

Here , is the following algo which I am using with the errors (I have mentioned all the errors in comments i.e. //ERROR:.........)

  import java.awt.Color;
    import java.awt.Insets;
    import java.awt.Container;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;

    import javax.swing.JMenu;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JTextArea;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.BorderFactory;


    public class Steganography_View extends JFrame
    {
        //sie variables for window
        private static int WIDTH  = 500;
        private static int HEIGHT = 400;

        //elements for JPanel
        private JTextArea   input;
        private JScrollBar  scroll,scroll2;
        private JButton     encodeButton,decodeButton;
        private JLabel      image_input;

        //elements for Menu
        private JMenu       file;
        private JMenuItem   encode;
        private JMenuItem   decode;
        private JMenuItem   exit;


        public Steganography_View(String name)
        {
            //set the title of the JFrame
            super(name);

            //Menubar
            JMenuBar menu = new JMenuBar();

            JMenu file = new JMenu("File"); file.setMnemonic('F');
            encode = new JMenuItem("Encode"); encode.setMnemonic('E'); file.add(encode);
            decode = new JMenuItem("Decode"); decode.setMnemonic('D'); file.add(decode);
            file.addSeparator();
            exit = new JMenuItem("Exit"); exit.setMnemonic('x'); file.add(exit);

            menu.add(file);
            setJMenuBar(menu);

            // display rules
            setResizable(true);                     //allow window to be resized: true?false
            setBackground(Color.lightGray);         //background color of window: Color(int,int,int) or Color.name
            setLocation(100,100);                   //location on the screen to display window
            setDefaultCloseOperation(EXIT_ON_CLOSE);//what to do on close operation: exit, do_nothing, etc
            setSize(WIDTH,HEIGHT);                  //set the size of the window
            setVisible(true);                       //show the window: true?false
        }


        public JMenuItem    getEncode()     { return encode;            }

        public JMenuItem    getDecode()     { return decode;            }

        public JMenuItem    getExit()       { return exit;              }

        public JTextArea    getText()       { return input;             }

        public JLabel       getImageInput() { return image_input;       }

        public JPanel       getTextPanel()  { return new Text_Panel();  }

        public JPanel       getImagePanel() { return new Image_Panel(); }

        public JButton      getEButton()    { return encodeButton;      }

        public JButton      getDButton()    { return decodeButton;      }


        private class Text_Panel extends JPanel
        {

            public Text_Panel()
            {
                //setup GridBagLayout
                GridBagLayout layout = new GridBagLayout(); 
                GridBagConstraints layoutConstraints = new GridBagConstraints(); 
                setLayout(layout);

                input = new JTextArea();
                layoutConstraints.gridx     = 0; layoutConstraints.gridy = 0; 
                layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1; 
                layoutConstraints.fill      = GridBagConstraints.BOTH; 
                layoutConstraints.insets    = new Insets(0,0,0,0); 
                layoutConstraints.anchor    = GridBagConstraints.CENTER; 
                layoutConstraints.weightx   = 1.0; layoutConstraints.weighty = 50.0;
                JScrollPane scroll = new JScrollPane(input,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,

//Error;The constructor JScrollPane(JTextArea, int, int) is undefined

                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//Error;The constructor JScrollPane(JTextArea, int, int) is undefined
                layout.setConstraints(scroll,layoutConstraints);//Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JScrollPane, GridBagConstraints)
                scroll.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));// Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JScrollPane, GridBagConstraints)
                add(scroll);//Error;The method add(Component) in the type Container is not applicable for the arguments (JScrollPane)

                encodeButton = new JButton("Encode Now");
                layoutConstraints.gridx     = 0; layoutConstraints.gridy = 1; 
                layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1; 
                layoutConstraints.fill      = GridBagConstraints.BOTH; 
                layoutConstraints.insets    = new Insets(0,-5,-5,-5); 
                layoutConstraints.anchor    = GridBagConstraints.CENTER; 
                layoutConstraints.weightx   = 1.0; layoutConstraints.weighty = 1.0;
                layout.setConstraints(encodeButton,layoutConstraints);//Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JButton, GridBagConstraints)
                add(encodeButton);//Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JButton, GridBagConstraints)

                //set basic display
                setBackground(Color.lightGray);
                setBorder(BorderFactory.createLineBorder(Color.BLACK,1));//Error;The method setBorder(Border) is undefined for the type Steganography_View.Text_Panel
            }
        }


        private class Image_Panel extends JPanel
        {

            public Image_Panel()
            {
                //setup GridBagLayout
                GridBagLayout layout = new GridBagLayout(); 
                GridBagConstraints layoutConstraints = new GridBagConstraints(); 
                setLayout(layout);

                image_input = new JLabel();
                layoutConstraints.gridx     = 0; layoutConstraints.gridy = 0; 
                layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1; 
                layoutConstraints.fill      = GridBagConstraints.BOTH; 
                layoutConstraints.insets    = new Insets(0,0,0,0); 
                layoutConstraints.anchor    = GridBagConstraints.CENTER; 
                layoutConstraints.weightx   = 1.0; layoutConstraints.weighty = 50.0;
                JScrollPane scroll2 = new JScrollPane(image_input,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,//Error;The constructor JScrollPane(JLabel, int, int) is undefined
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//Error;The constructor JScrollPane(JLabel, int, int) is undefined
                layout.setConstraints(scroll2,layoutConstraints);//Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JScrollPane, GridBagConstraints)
                scroll2.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));//Error;The method setBorder(Border) is undefined for the type JScrollPane
                image_input.setHorizontalAlignment(JLabel.CENTER);
                add(scroll2);//Error;The method setBorder(Border) is undefined for the type JScrollPane

                decodeButton = new JButton("Decode Now");
                layoutConstraints.gridx     = 0; layoutConstraints.gridy = 1; 
                layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1; 
                layoutConstraints.fill      = GridBagConstraints.BOTH; 
                layoutConstraints.insets    = new Insets(0,-5,-5,-5); 
                layoutConstraints.anchor    = GridBagConstraints.CENTER; 
                layoutConstraints.weightx   = 1.0; layoutConstraints.weighty = 1.0;
                layout.setConstraints(decodeButton,layoutConstraints);//Error;The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JButton, GridBagConstraints)
                add(decodeButton);//Error;The method add(Component) in the type Container is not applicable for the arguments (JButton)

                //set basic display
                setBackground(Color.lightGray);
                setBorder(BorderFactory.createLineBorder(Color.BLACK,1));//Error;The method add(Component) in the type Container is not applicable for the arguments (JButton)
            }
         }


        public static void main(String args[])
        {
            new Steganography_View("Secure Data Transmission");
        }
    }

Please Help me to debug this.

1 Answers1

2

Use the Java documentation in your favor, so you can clearly know how its library methods work. The complete documentation can be found here.

Your struggle can be seen as pure lazyness or lack of knowledge regarding Java Documentation (JavaDocs).

Please take a look at your errors as they clearly direct you to the solution.

Examples:


The constructor JScrollPane(JTextArea, int, int) is undefined.

JScrollPane has four constructors.

  • public JScrollPane()
  • public JScrollPane(Component view)
  • public JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
  • public JScrollPane(int vsbPolicy, int hsbPolicy)

You need to use one of the provided ones to build your object, otherwise it will not work...


The method add(Component) in the type Container is not applicable for the arguments (JButton)

Please take a look at the Component documentation to see how to use the add() method...


The method setConstraints(Component, GridBagConstraints) in the type GridBagLayout is not applicable for the arguments (JScrollPane, GridBagConstraints)

You have provided arguments different than the expected ones for the setConstraints method of GridBagLayout, as you passed different arguments than the ones clearly explained here:

public void setConstraints(Component comp, GridBagConstraints constraints)

  • comp - the component to be modified
  • constraints - the constraints to be applied

... and so on.

Bruno Toffolo
  • 1,504
  • 19
  • 24