Results 1 to 17 of 17
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default private access error

    I have programmed several classes and run into the following problem (too much code to display):
    1) class A
    2) class B
    3) class C

    in class A there is an object created of class B: objB
    in class B there is an object created of class C: objC

    Class C contains a getFirstName method.

    So why I cannot say in class A:
    objB.objC.getFirstName();

    I get a private access in class B error message, which makes no sense because the classes and the method and the references of the objects are public!

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    part of the code maybe this helps

    Java Code:
    class DeskMonitor extends JFrame  {
        private JPanel mainPanel, infopanel;
        private JLabel label, infolabel;
        private final static int MAXROOM = 90;
        private RoomInfoPanel[] roominfopanel;
        private SingleRoomPanel singleroompanel;
        private ImageIcon icon1, icon2;
        private MouseHandler Mhandler;
                   StudentMonitor smonitor;
    ..
    
    smonitor = new StudentMonitor();
    
    System.out.println(smonitor.rdialog.getFirstName());
    System.out.println(smonitor.rdialog.getlastName());   
    
    ====================================
    
    class StudentMonitor extends JPanel {
    
        private String firstname, lastname;
        private int threadcount, roomindex;
        private JFrame frame;
        private JPanel panel, fastSearchPanel,
                buttonPanel, infoPanel, infoleft, inforight;
        private JButton bt, sbt;
        private JLabel info1, info2, info3, pflabel;
        private JCheckBox pf;
        private ImageIcon icon1, icon2;
        private Random randGen;
    
        private final static int MAXROOM = 90;
        private RoomInfo[] roominfo;
        private ActionListener buttonlistener;
        private Border etched, titled;
                RegisterDialog rdialog;
    
    
        public StudentMonitor() {
         // setTitle("Student Monitor Thread");
         // setSize(700, 500);
    
          roominfo = new RoomInfo[MAXROOM]; // initialize the  roominfo array
          for (int i = 0; i < roominfo.length; i++) {
              roominfo[i] = new RoomInfo();
          }
    
          randGen = new Random();
    
          icon1 = createImageIcon("images/keyboard.gif");
          icon2 = createImageIcon("images/lic.gif");
          setRoomsFree(); // set all roomfree true
          setPianoRoom(); // set a sellecttion of the piano rooms
    
          buttonlistener = new  ButtonListener();
    
          rdialog = new RegisterDialog();  // activates the student register panel
    
    ======================================
    
    class RegisterDialog extends JFrame  {
        
        
        private String firstname, lastname;
       
        private ImageIcon icon;
        private ActionListener dlistener;
        private JPasswordField passwField;
        private JTextField firstnField, lastnField;
        
        public RegisterDialog()  {
        
          icon = createImageIcon("images/lic2.gif");
         
          dlistener = new DialogListener();
    
          setTitle("Student Monitor");  // activates student register panel
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setBounds(300, 300, 250, 180);
          add(createRegisterDialog());
          setVisible(true);
          
        }
    
    ..
    
    
     public String getFirstName() {
            return firstname;
         }
    
         public String getlastName() {
            return lastname;
         }

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

    Default Re: private access error

    While the methods are public, the variables aren't. But having said that, don't make the variables public. Consider either creating a getter method to get the private variables you need, so you can call,

    Java Code:
    objB.getObjC().getFirstName();
    Or creating a public method for this:

    Java Code:
    objB.getObjCFirstName();
    Where ObjectB has the method referred to above and returns the firstName object obtained by calling c's getter:

    Java Code:
    class B {
       public String getObjCFirstName() {
          return objC.getFirstName();
       }
    }
    Edit: as an important aside, why does RegisterDialog extend *JFrame*? It's a dialog window and thus is dependent on a parent window, and so shouldn't it instead extend *JDialog*, and also important, shouldn't that dialog be modal? Note that having said this, I've written a lot of Swing code, and I almost never extend JFrame *or* JDialog.
    Last edited by Fubarable; 03-23-2013 at 08:06 PM.

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    Fubarable , I do not understand: all the fields are private and the get methods are public of my code....
    see please the code fragments up
    You'r right I'd look into extending JDialog!

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

    Default Re: private access error

    Quote Originally Posted by willemjav View Post
    Fubarable , I do not understand: all the fields are private and the get methods are public of my code....
    Exactly. You've got private fields, and these are unreachable by outside classes. What is confusing you about this?

    see please the code fragments up
    It should be obvious that I already have.

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    this is in class B;

    Java Code:
    public String getFirstName() { // object of class C rdialog
            return rdialog.getFirstName();
         }
    
         public String getlastName() {
            return rdialog.getLastName();
         }
    this is in class C:

    Java Code:
    public String getFirstName() {
            return firstname;
         }
    
         public String getlastName() {
            return lastname;
         }
    so I should be able to say in class A:

    Java Code:
    System.out.println(smonitor.getFirstName()); // object of class B smonitor
    System.out.println(smonitor.getlastName());
    in class B it is saying: can not find method: getFirstName()
    An as a suggestion it says that I should create that method:
    but the funny thing is, it is there!

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    OK found it a problem concerning headers LastName/ Lastname....
    sorry how stupyed again

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

    Default Re: private access error

    Please show me where in your code above does the StudentMonitor class have a getFirstName() method? Sorry, but I don't see it, and believe me, I've looked.

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    no the erros stay.... what is going on here:


    Java Code:
        public String getFirstName() {
            return firstname;
         }
    
         public String getLastName() {
            return lastname;
         }
    
    public String getFirstName() {
            return rdialog.getFirstName();
         }
    
         public String getlastName() {
            return rdialog.getLastName();
         }
    
     System.out.println(smonitor.getFirstName());
     System.out.println(smonitor.getLastName());

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package roomassigment;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.*;
    import javax.swing.border.Border;
    
    /**
     *
     * 
     */
    class StudentMonitor extends JPanel {
    
       // private String firstname, lastname;
        private int threadcount, roomindex;
        private JFrame frame;
        private JPanel panel, fastSearchPanel,
                buttonPanel, infoPanel, infoleft, inforight;
        private JButton bt, sbt;
        private JLabel info1, info2, info3, pflabel;
        private JCheckBox pf;
        private ImageIcon icon1, icon2;
        private Random randGen;
    
        private final static int MAXROOM = 90;
        private RoomInfo[] roominfo;
        private ActionListener buttonlistener;
        private Border etched, titled;
                RegisterDialog rdialog;
    
    
        public StudentMonitor() {
         // setTitle("Student Monitor Thread");
         // setSize(700, 500);
    
          roominfo = new RoomInfo[MAXROOM]; // initialize the roominfo array
          for (int i = 0; i < roominfo.length; i++) {
              roominfo[i] = new RoomInfo();
          }
    
          randGen = new Random();
    
          icon1 = createImageIcon("images/keyboard.gif");
          icon2 = createImageIcon("images/lic.gif");
          setRoomsFree(); // set all roomfree true
          setPianoRoom(); // set a sellecttion of the piano rooms
    
          buttonlistener = new  ButtonListener();
    
          rdialog = new RegisterDialog();  // activates the student register panel
     
       }
    
        public JPanel CreateButtons() { // create the room select buttons
            int arraycount= 0;          // panel CENTER
            buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(10,10));
            for (int i = 3; i <6; i++) {
                for (int ii = 0; ii <=29; ii++) {
                    int room = (i*100)+ii;
                    roominfo[arraycount].setRoomNumber(room);
    
                    bt = new JButton("" + room);
                    roominfo[arraycount].setRoomButton(bt);
    
                    if (roominfo[arraycount].getPiano())
                    bt.setIcon(icon1);
                    bt.setHorizontalTextPosition( SwingConstants.LEFT );
                    buttonPanel.add(bt);
                    bt.addActionListener(buttonlistener);
                    arraycount++;
                }
            }
            return buttonPanel;
        }
        
        public JPanel createSearchButton()  {  // random button plus piano check
           fastSearchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
           sbt = new JButton("SEARCH ROOM");  // on SOUTH panel
           pf = new JCheckBox("PIANO");
           pflabel = new JLabel(icon1);
           //new ImageIcon("images/middle.gif")
    
           //  pf.setHorizontalTextPosition( JCheckBox.LEFT );
           // pf.setIcon(icon1);
           fastSearchPanel.add(sbt);
           fastSearchPanel.add(pf);
           fastSearchPanel.add(pflabel);
           sbt.addActionListener(new
              ActionListener()
              {
                 public void actionPerformed(ActionEvent event)
                 {
                   // System.out.println(" fast button pressed check box "
                   //         + pf.isSelected());
                    fastSearchRoom(pf.isSelected()); // read if piano box is checked
                 }
              });
    
           return fastSearchPanel;
        }
    
    
        public JPanel createinfoPanel()  { // creat the SOUTH info panel
           infoPanel = new JPanel(new GridLayout(1,2));
           infoleft = new JPanel();
           inforight = new JPanel(new GridLayout(2,1));
    
           info1 = new JLabel(icon2, JLabel.RIGHT);
           info2 = new JLabel("Use the roombuttons or the fast button "
                   + "to select a room");
           info3 = new JLabel("When you are a pianist check piano checkbox");
    
           etched = BorderFactory.createEtchedBorder();
           titled = BorderFactory.createTitledBorder(etched, "Monitor Info");
           inforight.setBorder(titled);
           infoleft.add(info1);
           inforight.add(info2);
           inforight.add(info3);
    
           infoPanel.add(infoleft);
           infoPanel.add(inforight);
    
           return infoPanel;
        }
    
        public void createConfirmDialog()  {
            JPanel confirmDialogPanel = new JPanel();
    
            Object[] possibilities2 = {"30 min", "1 hour", "1.30", "2 hours"};
            String ss = (String)JOptionPane.showInputDialog(
                    confirmDialogPanel,
             //       fristname + "\n" +
                    "Confirm please the room selected\n" +
                    "Choose the time from the menu\n" +
                    "and press the OK button\n",
                    "Conformation Dialog",
                    JOptionPane.PLAIN_MESSAGE,
                    icon2,
                    possibilities2,
                    "1.30");
        }
    
        public void setPianoRoom() { // set thrue for room with piano
    
             for (int i = 0; i <30; i++) {
              roominfo[i].setPiano(true);
            }
            roominfo[33].setPiano(true);
            roominfo[35].setPiano(true);
            roominfo[37].setPiano(true);
            roominfo[40].setPiano(true);
            roominfo[20].setPiano(true);
            roominfo[51].setPiano(true);
            roominfo[63].setPiano(true);
            roominfo[62].setPiano(true);
            roominfo[73].setPiano(true);
            roominfo[71].setPiano(true);
            roominfo[72].setPiano(true);
            roominfo[70].setPiano(true);
        }
    
        public void setRoomsFree() {
             for (int i = 0; i < roominfo.length; i++)
               roominfo[i].setFreeRoom(true);
        }
    
        
         public class ButtonListener implements ActionListener {// roombuttons
    
           public void actionPerformed(ActionEvent e)
           {
               int arraynumber = 0;
               int rn = Integer.parseInt(e.getActionCommand()); //button label
    
               arraynumber = getRoomNumberIndex(rn);
    
               roominfo[arraynumber].getRoomButton().setEnabled(false);
               roominfo[arraynumber].setFreeRoom(false);
    
               System.out.println(" room number " + rn);
               System.out.println(" array number " + arraynumber );
               System.out.println(" array number room " +
                       roominfo[arraynumber].getRoomNumber());
               System.out.println(" piano " + roominfo[arraynumber].getPiano());
    
          }
       }
    
      
       public void fastSearchRoom(boolean pf)  {  // random button plus piano check
          int rand=0;                             // on SOUTH panel
          if (pf)  do {
                rand = randGen.nextInt(MAXROOM); // piano rooms
         } while (!roominfo[rand].getPiano() ||
                  (!roominfo[rand].getFreeRoom()));
    
         else do {
                rand = randGen.nextInt(MAXROOM); // non piano rooms
          } while (roominfo[rand].getPiano() ||
                  (!roominfo[rand].getFreeRoom()));
    
          roominfo[rand].setFreeRoom(false);
          roominfo[rand].getRoomButton().setEnabled(false);
    
          System.out.print(" rand " + rand);
          System.out.println(" randroom " + roominfo[rand].getRoomNumber()
                  + " freeroom " + roominfo[rand].getFreeRoom() +
                  " pfbutton selected " + pf +
                  " pfroom selected " + roominfo[rand].getPiano() +
                  " room index " + getRoomNumberIndex(roominfo[rand].getRoomNumber()));
       }
    
    
        public class RoomInfo  { // principle class holds all room info
           private int roomnumber;
           private JButton roombutton;
           private String studentname;
           private boolean piano, roomfree;
           private long endtime;
    
           public void setRoomNumber(int rm)  {
               roomnumber = rm;
           }
           public int getRoomNumber()  {
               return roomnumber;
           }
    
           public void setRoomButton(JButton jb)  {
               roombutton = jb;
           }
           public JButton getRoomButton()  {
               return roombutton;
           }
    
            public void setStudentName(String sn)  {
               studentname = sn;
           }
           public String getStudentName()  {
               return studentname;
           }
    
            public void setPiano(Boolean pf)  {
               piano = pf;
           }
           public Boolean getPiano()  {
               return piano;
           }
    
            public void setFreeRoom(Boolean rf)  {
               roomfree = rf;
           }
           public Boolean getFreeRoom()  {
               return roomfree;
           }
    
           public void setEndTime(long et)  {
               endtime = et;
           }
           public long getEndTime()  {
               return endtime;
           }
    
         }
        
        public String getFirstName() {
            return rdialog.getFirstName(); // erroe !!!
         }
    
         public String getlastName() {
            return rdialog.getLastName();  // error !!!
         }
    
    
    
         public int getRoomNumberIndex(int roomnum)  {// input roomnumber output index
           int index =-1;
           for (int i = 0; i < roominfo.length; i++) {
               if (roominfo[i].getRoomNumber() == roomnum)
                   index = i;
           }
               return index;
        }
    
    
        protected static ImageIcon createImageIcon(String path) { // loads the icons
            java.net.URL imgURL = StudentMonitor.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    
    }

  11. #11
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package roomassigment;
    
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    
    /**
     *
     * 
     */
    class DeskMonitor extends JFrame  {
        private JPanel mainPanel, infopanel;
        private JLabel label, infolabel;
        private final static int MAXROOM = 90;
        private RoomInfoPanel[] roominfopanel;
        private SingleRoomPanel singleroompanel;
        private ImageIcon icon1, icon2;
        private MouseHandler Mhandler;
                StudentMonitor smonitor;
    
        public DeskMonitor()  {
          
          roominfopanel = new RoomInfoPanel[MAXROOM]; // initialize the
          for (int i = 0; i < roominfopanel.length; i++) { // roominfopanel array
              roominfopanel[i] = new RoomInfoPanel();
          }
    
          icon1 = createImageIcon("images/keyboard.gif");
          icon2 = createImageIcon("images/lic2.gif");
    
          setTitle("Principle Desk Monitor");
          setSize(1600, 900);
          mainPanel = new JPanel(new BorderLayout());
          Mhandler = new MouseHandler();
          // object which constructs ind room panels
          singleroompanel = new SingleRoomPanel();
    
          mainPanel.add(CreatePanels(), BorderLayout.CENTER);
          mainPanel.add(createTopInfoPanel(), BorderLayout.NORTH);
    
          // set a listener for some keys
          mainPanel.setFocusable(true);
          //mainPanel.requestFocusInWindow();
          mainPanel.addKeyListener (new KeyAdapter() { // read some key events
                public void keyPressed(KeyEvent e) {
                    int keyCode = e.getKeyCode();
                  //  System.out.println("You Pressed " + keyCode);
                    if (keyCode == KeyEvent.VK_SPACE)  {
                       System.out.println(" spacekey");
                       // activate studentmonitor
                       smonitor = new StudentMonitor();
                    }
                  // this simulates the studentroom card
                    if (keyCode == 84)  {
                       System.out.println(" T key pressed");
                       System.out.println(smonitor.getFirstName());
                       System.out.println(smonitor.getLastName());       // only for this line error MAKES NO SENSE          
                    }
                }
          });
          
          Container contentPane = getContentPane();
          contentPane.add(mainPanel);
         // myGlassPane.setVisible(true);
        }
    
    
        public JPanel CreatePanels() {
    
            int arraycount= 0;          // panel CENTER
            JPanel roomPanel = new JPanel();
            roomPanel.setLayout(new GridLayout(10,10));
            
            for (int i = 3; i <6; i++) {
                for (int ii = 0; ii <=29; ii++) {
                    int room = (i*100)+ii;
                    
                    JPanel rp = singleroompanel.CreateSingleRoomPanel(new
                                                    JPanel(), room);
    
                    roominfopanel[arraycount].setRoomNumber(room);
                    roominfopanel[arraycount].setPanelRef(rp);
    
                    if (roominfopanel[arraycount].getPianoRoom());
                    
                    rp.addMouseListener(Mhandler);
                    roomPanel.add(rp);
                    arraycount++;
                }
            }
            
            return roomPanel;
         }
    
    
         public JPanel createTopInfoPanel()  {
             infopanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
             
             infolabel = new JLabel(icon2);
             infopanel.add(infolabel);
             return infopanel;
         }
    
    
         public class RoomInfoPanel  { // principle class holds all room info
           private int roomnumber;
           private JPanel panelref;
           private String studentfirstname, studentlastname;
           private boolean piano, roomfree, reserved;
           private long endtime;
    
           public void setRoomNumber(int rm)  {
               roomnumber = rm;
           }
           public int getRoomNumber()  {
               return roomnumber;
           }
    
           public void setPanelRef(JPanel jp)  {
               panelref = jp;
           }
           public JPanel getPanelRef()  {
               return panelref;
           }
    
           public void setStudentFirstName(String sfn)  {
               studentfirstname = sfn;
           }
           public String getStudentFirstName()  {
               return studentfirstname;
           }
           public void setStudentLastName(String sn)  {
               studentlastname = sn;
           }
    
           public String getStudentLastName()  {
               return studentlastname;
           }
    
            public void setPianoRoom(Boolean pf)  {
               piano = pf;
           }
           public Boolean getPianoRoom()  {
               return piano;
           }
    
            public void setFreeRoom(Boolean rf)  {
               roomfree = rf;
           }
           public Boolean getFreeRoom()  {
               return roomfree;
           }
    
           public void setEndTime(long et)  {
               endtime = et;
           }
           public long getEndTime()  {
               return endtime;
           }
           public void setReserved(Boolean rs)  {
               reserved = rs;
           }
           public Boolean getReserved()  {
               return reserved;
           }
    
         }
    
           // input roomnumber output index
           public int getRoomNumberIndex(int roomnum)  {
           int index =-1;
           for (int i = 0; i < roominfopanel.length; i++) {
               if (roominfopanel[i].getRoomNumber() == roomnum)
                   index = i;
           }
               return index;
         }
    
    
        protected static ImageIcon createImageIcon(String path) { // loads the icons
            java.net.URL imgURL = StudentMonitor.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    
        public class MouseHandler implements MouseListener {
            Object comp = null;
            public void mousePressed(MouseEvent e) {
                   // System.out.println(" mouse pressed at " + e);
                     //   e.getClickCount()
            }
    
            public void mouseReleased(MouseEvent e) {
    
            }
    
            public void mouseEntered(MouseEvent e) {
               comp = e.getSource();
              
            }
    
            public void mouseExited(MouseEvent e) {
    
            }
    
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() >= 2) {
                    // roominfopanrl array
                    for (int i = 0; i < roominfopanel.length; i++) { 
                            if (e.getSource() == roominfopanel[i].getPanelRef())
                                System.out.println(" bingo " + i);
                    }
                }
            }
       }
    }

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    so here is it all in order deskmonitor, student monitor regiter dialog

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package roomassigment;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Arrays;
    import javax.swing.*;
    
    /**
     *
     * 
     */
     class RegisterDialog extends JFrame  {
        
        
        private String firstname, lastname;
       
        private ImageIcon icon;
        private ActionListener dlistener;
        private JPasswordField passwField;
        private JTextField firstnField, lastnField;
        
        public RegisterDialog()  {
        
          icon = createImageIcon("images/lic2.gif");
         
          dlistener = new DialogListener();
    
          setTitle("Student Monitor");  // activates student register panel
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setBounds(300, 300, 250, 180);
          add(createRegisterDialog());
          setVisible(true);
          
        }
    
        public JPanel createRegisterDialog()  {
    
            firstnField = new JTextField(10);
            firstnField.setMaximumSize(firstnField.getPreferredSize());
            lastnField = new JTextField(10);
            lastnField.setMaximumSize(lastnField.getPreferredSize());
            passwField = new JPasswordField("****", 4);
            passwField.setMaximumSize(passwField.getPreferredSize());
    
            JPanel regDialogPanel = new JPanel();
            JLabel firstnLabel = new JLabel("Firstname  ");
            JLabel lastnLabel = new JLabel("Lastname  ");
            JLabel passwLabel = new JLabel("Key Code  ");
            JLabel iconLabel = new JLabel(icon);
           
            JButton okButton = new JButton("OK");
            JButton chancelButton = new JButton("Chancel");
    
            passwField.setActionCommand("passwField");
            okButton.setActionCommand("okButton");
            chancelButton.setActionCommand("chancelButton");
            passwField.addActionListener(dlistener);
            okButton.addActionListener(dlistener);
            chancelButton.addActionListener(dlistener);
    
            regDialogPanel.setLayout(new BoxLayout(regDialogPanel,
                    BoxLayout.PAGE_AXIS));
    
            Box bh1 = Box.createHorizontalBox();
            Box bh2 = Box.createHorizontalBox();
            Box bh3 = Box.createHorizontalBox();
            Box bh4 = Box.createHorizontalBox();
            Box bh5 = Box.createHorizontalBox();
    
            Box bv1 = Box.createVerticalBox();
            Box bv2 = Box.createVerticalBox();
            Box bv3 = Box.createVerticalBox();
    
            bh1.add(iconLabel);
            bh1.add(Box.createGlue());
            bh2.add(Box.createHorizontalGlue());
            bh2.add(firstnLabel);
            bh2.add(firstnField);
            bh2.add(Box.createHorizontalStrut(20));
            bh3.add(Box.createGlue());
            bh3.add(lastnLabel);
            bh3.add(lastnField);
            bh3.add(Box.createHorizontalStrut(22));
            bh4.add(Box.createGlue());
            bh4.add(passwLabel);
            bh4.add(passwField);
            bh4.add(Box.createHorizontalStrut(95));
         //   bh5.add(Box.createHorizontalStrut(5));
            bh5.add(okButton);
            bh5.add(Box.createGlue());
            bh5.add(chancelButton);
         //   bh5.add(Box.createHorizontalStrut(5));
    
            bv1.add(bh1);
            bv1.add(bh1);
            bv2.add(bh2);
            bv2.add(bh3);
            bv2.add(bh4);
            bv3.add(bh5);
    
            regDialogPanel.add(bv1);
            regDialogPanel.add(bv2);
            regDialogPanel.add(bv3);
    
            return regDialogPanel;
        }
    
        
    
         public class DialogListener implements ActionListener {// roombuttons
    
           public void actionPerformed(ActionEvent e)
           {
               String cmd = e.getActionCommand();
    
                if ((cmd.equals("passwField")) || (cmd.equals("okButton")))
                { //Process the password.
                        char[] input = passwField.getPassword();
                        if (isPasswordCorrect(input)) {
                            System.out.println(" password correct ");
                            firstname = firstnField.getText();
                            lastname = lastnField.getText();
                            setVisible(false);
                           
                        } else {
                            System.out.println(" password not correct ");
                        }
                        //Zero out the possible password, for security.
                        Arrays.fill(input, '0');
                        passwField.selectAll();
                        resetFocus();
                }
    
               if (cmd.equals("chancelButton"))
                    System.exit(0);
          }
       }
    
         public void setStudentButtonPanel() {
    
      //    add(CreateButtons(), BorderLayout.CENTER);
      //    add(createSearchButton(), BorderLayout.SOUTH);
       //   add(createinfoPanel(), BorderLayout.NORTH);
         }
         
         public String getFirstName() {
            return firstname;
         }
    
         public String getLastName() {
            return lastname;
         }
    
        
    
         private static boolean isPasswordCorrect(char[] input) {
            boolean isCorrect = true;
            char[] correctPassword = { '5', '5', '5', '5' };
    
            if (input.length != correctPassword.length) {
                isCorrect = false;
            } else {
                isCorrect = Arrays.equals (input, correctPassword);
            }
            //Zero out the password.
            Arrays.fill(correctPassword,'0');
    
            return isCorrect;
        }
    
          protected void resetFocus() {
            passwField.requestFocusInWindow();
        }
    
    
    
        protected static ImageIcon createImageIcon(String path) { // loads the icons
            java.net.URL imgURL = StudentMonitor.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    }

  13. #13
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    this is really crazy the second has an error the first not:

    Java Code:
     String getFirstName() {
            return firstname;
        }
         
         String getFirstName() {
            return firstname;
         }

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    Now the error suddenly disappears, wow how can that be, did not do anything,
    the compiler is crazy....

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    so I have this now in class c (RegisterDialog)

    Java Code:
     public String getFirstName() {
            return firstname;
         }
    
    
              public String getLastName() {
            return lastname;
         }
    and do this in class A (DeskMonitor)

    Java Code:
    if (keyCode == 84)  {
                       System.out.println(" T key pressed");
                       System.out.println(smonitor.rdialog.getFirstName());
                       System.out.println(smonitor.rdialog.getLastName());
                    }
    and it freaken works now (before not) what is going on

  16. #16
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: private access error

    Fubarable

    I changed JFrame into
    class RegisterDialog extends JDialog {
    and took out
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    because the dialog closes anyway on
    setVisible(false);

    is that better?

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

Similar Threads

  1. Java Error: '____ has private access in ____'
    By Josh Fedman in forum New To Java
    Replies: 6
    Last Post: 11-08-2012, 07:54 AM
  2. Replies: 4
    Last Post: 08-26-2012, 01:09 AM
  3. has private access in error
    By Big-D in forum New To Java
    Replies: 1
    Last Post: 04-06-2012, 02:53 PM
  4. Default / package-private access
    By genkuro in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 06:08 AM
  5. has private access error message
    By ibrahimyoussof in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 04-13-2010, 04: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
  •