Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default JFrame and two JPanel Classes

    Hello. I want to send information from Panel1 to Panel2. Start and Panel1 works 100% but, Panel2 doesn't print anything in paintComponent; I don't think it even goes from Panel1 to Panel2. Here's what I'm basically trying to do: (I edited a lot of it out)

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Start extends JFrame
    {
        private Image[] image =  new Image[13];
        AllImages allImages = new AllImages();
        public Start()
        {
            super("Swing");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(806, 626);
            this.getContentPane().add(new Panel1(images));
            this.setResizable(false);
            this.setVisible(true);
        }
        
        public static void main(String[] arguments)
        {
            new Start();
        }
    
        public void paintComponent(Graphics g)
        {
        }
        public class AllImages
        {
            public AllImages()
            {
                //images
            }
        }
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    class Panel1 extends JPanel implements MouseListener
    {
        private Image[] images;
        private String[] newInfo;
        public Panel1(Image[] aimages)
        {
            images = aimages;
            addMouseListener(this);
        }
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            //images
        }
         public void mouseClicked(MouseEvent e) 
        {
            int x = e.getX();
            int y = e.getY();
            if (x > 163 && x < 163 + 153 && y > 445 && y < 445 + 41) //Where the button is placed and the height and width of it.
            {
                //accept button
                String getName = "blah blah blah";
                String getID = "213123141231";
                
                getName = name.getText(); //name and ID are JTextFields
                getID = ID.getText();
    
                newInfo[0] = getName;
                newInfo[1] = getID;
    
                new Panel2(newInfo);
            }
        }
        public void mousePressed (MouseEvent e) {}
        public void mouseReleased (MouseEvent e) {} 
        public void mouseEntered (MouseEvent e){}
        public void mouseExited (MouseEvent e){}
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Panel2 extends JPanel implements MouseListener
    {
        private String[] Info;
        public Panel2(String[] newInfo)
        {
            Info = newInfo;
            addMouseListener(this);
        }
        public void paintComponent(Graphics g)
        {
            g.setColor(Color.black);
            g.drawString(Info[0],10,50);
            g.drawString(Info[1],10, 100);
        }
        public void mouseClicked (MouseEvent e) {}
        public void mousePressed (MouseEvent e) {}
        public void mouseReleased (MouseEvent e) {} 
        public void mouseEntered (MouseEvent e){}
        public void mouseExited (MouseEvent e){}
    }
    Any help is very much appreciated.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Seems to me there are few errors. In Panel1 class,

    Java Code:
    getName = name.getText(); //name and ID are JTextFields
    what's getText() ?

  3. #3
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Seems to me there are few errors. In Panel1 class,

    Java Code:
    getName = name.getText(); //name and ID are JTextFields
    what's getText() ?
    getText() changes JTextFields to Strings. From what I found, getText() works fine since drawString prints out the String perfectly in Panel1, but nothing is printed out in Panel2.
    Last edited by YellowPainting; 08-23-2009 at 03:51 PM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

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

    Default

    Quote Originally Posted by YellowPainting View Post
    getText() changes JTextFields to Strings. From what I found, getText() works fine since drawString prints out the String perfectly in Panel1, but nothing is printed out in Panel2.
    getText() will extract a String from a JTextField, but you don't declare either of the alleged JTextFields, name or ID, anywhere in your Panel1 class. So without these JTextFields, this code is completely borked and will not compile. If you have code where this is fixed, then please post it.

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

    Default

    I think you're looking to do something similar to this code below. I like to have my Panel1 and Panel2 classes know nothing about each other. Instead they have public methods to allow a main class to do the dirty work:

    Start.java
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Start extends JFrame {
      private static final Dimension PANEL_SIZE = new Dimension(400, 200);
      private Panel1 panel1 = new Panel1();
      private Panel2 panel2 = new Panel2();
      
      public Start() {
        super("Swing");
        panel1.setPreferredSize(PANEL_SIZE);
        panel2.setPreferredSize(PANEL_SIZE);
        panel1.setBorder(BorderFactory.createTitledBorder("panel 1"));
        panel2.setBorder(BorderFactory.createTitledBorder("panel 2"));
        
        add(panel1, BorderLayout.NORTH);
        add(panel2, BorderLayout.SOUTH);
        
        // add actionlistener to button in panel1 object
        // use public methods found in Panel1 and Panel2 classes
        // to get strings from Panel1's textfields and display in 
        // Panel2.
        panel1.addTransferActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // first extract Strings from panel1's textfields
            String name = panel1.getNameText();
            String id = panel1.getIdText();
            String[] info = {name, id}; // place into array
            
            // then pass array to panel2
            panel2.setStrings(info);
          }
        });
      }
    
      // to display our JFrame cleanly
      private static void createAndShowUI() {
        JFrame frame = new Start();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      // start Swing in a thread-safe manner
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }
    Panel1.java
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    class Panel1 extends JPanel  {
      private JTextField name = new JTextField(10);
      private JTextField id = new JTextField(10);
      private JButton transferInfoBtn = new JButton("Transfer Info");
      
      public Panel1() {
        add(name);
        add(id);
        add(transferInfoBtn);
      }
      
      public String getNameText() {
        return name.getText();
      }
      
      public String getIdText() {
        return id.getText();
      }
      
      // allows the main class to add an actionlistener to the transfer info button
      public void addTransferActionListener(ActionListener al) {
        transferInfoBtn.addActionListener(al);
      }
    
    }
    Panel2.java
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Panel2 extends JPanel {
      private String[] info = {"", ""};
      
      public void setStrings(String[] info) {
        // copy String array over
        System.arraycopy(info, 0, this.info, 0, info.length);
        repaint(); // tells JVM to call paintComponent method
      }
    
      public void paintComponent(Graphics g) {
        super.paintComponent(g); // to clear previous data from panel 
        g.setColor(Color.black);
        g.drawString(info[0], 10, 50);
        g.drawString(info[1], 10, 100);
      }
    
    }

  7. #7
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    getText() will extract a String from a JTextField, but you don't declare either of the alleged JTextFields, name or ID, anywhere in your Panel1 class. So without these JTextFields, this code is completely borked and will not compile. If you have code where this is fixed, then please post it.
    Sorry, must have edited them out by accident. If you still need the completed code, here:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    class Panel1 extends JPanel implements MouseListener
    {
        private Image[] images;
        private String[] newInfo;
        private JTextField name;
        private JTextField ID;
        public Panel1(Image[] aimages)
        {
            images = aimages;
            addMouseListener(this);
        }
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            //images
        }
         public void mouseClicked(MouseEvent e) 
        {
            int x = e.getX();
            int y = e.getY();
            if (x > 163 && x < 163 + 153 && y > 445 && y < 445 + 41) //Where the button is placed and the height and width of it.
            {
                //accept button
                String getName = "blah blah blah";
                String getID = "213123141231";
                
                getName = name.getText(); //name and ID are JTextFields
                getID = ID.getText();
    
                newInfo[0] = getName;
                newInfo[1] = getID;
    
                new Panel2(newInfo);
            }
        }
        public void mousePressed (MouseEvent e) {}
        public void mouseReleased (MouseEvent e) {} 
        public void mouseEntered (MouseEvent e){}
        public void mouseExited (MouseEvent e){}
    }

    Also, thanks Fubarable, I'll take a look at it.

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

    Default

    If you look at my code, you should be able to get ideas on how to complete your code. Why not give it a try first, then come back only if it doesn't work.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I appreciate, if all the members can ask the question more clearly in very first post with all relevant details, it's easy to answer as well as easy to find the solution for there question.

  10. #10
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    If you look at my code, you should be able to get ideas on how to complete your code. Why not give it a try first, then come back only if it doesn't work.
    I understand what I did wrong, thanks.

    Quote Originally Posted by Eranga View Post
    I appreciate, if all the members can ask the question more clearly in very first post with all relevant details, it's easy to answer as well as easy to find the solution for there question.
    Sorry I wasn't clear enough, thanks anyway.

Similar Threads

  1. How to add JFrame inside JPanel
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 8
    Last Post: 12-13-2009, 08:41 PM
  2. Multiple Panels in JFrame or JPanel
    By DavidG24 in forum AWT / Swing
    Replies: 5
    Last Post: 05-16-2009, 12:47 PM
  3. Display output in Jframe or JPanel
    By rammurugesan in forum AWT / Swing
    Replies: 12
    Last Post: 04-14-2009, 01:45 PM
  4. problems with JPanel and JFrame
    By v1nsai in forum New To Java
    Replies: 13
    Last Post: 04-08-2009, 07:49 PM
  5. scroll a Jpanel in a JFrame
    By nidhirastogi in forum SWT / JFace
    Replies: 1
    Last Post: 09-07-2008, 03:42 AM

Posting Permissions

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