Results 1 to 6 of 6

Thread: JPanel reseting

  1. #1
    simon77 is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default JPanel reseting

    hello,
    I've been working on an app these days and I got stuck with one situation.
    I wrote for you basic code that illustrates my problem.
    After pressing the "add text" button, in JPanel 2 should appear a new message "hi" but it doesn't work.
    I've tried many things, searched many sites, but I couldn't find the solution for my problem.
    If you can see it, please let me know.. thanx :)

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.TitledBorder;
    
    /**
     *
     * @author simon
     */
    public class What {
    
        private JPanel p1;
        private JPanel p2;
        private Container pane;
        
        public What(){
            JFrame frame = new JFrame("app");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            start(frame.getContentPane());
            frame.setPreferredSize(new Dimension(300, 300));
            frame.pack();
            frame.setVisible(true); 
        }
        
        public void start(Container pane){
            this.pane = pane;
            pane.setLayout(new BorderLayout());
            
            p1 = new JPanel();
            p2 = new JPanel();
            
            p1.setSize(new Dimension(200, 100));
            p1.setBorder(new CompoundBorder(new TitledBorder("JPanel 1"), new EmptyBorder(3,3,3,3)));
            p1.setLayout(new BorderLayout());
            JButton ok = new JButton("add text");
            ok.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    addText();
                }
            });
            
            p1.add(ok);
            
            p2.setSize(new Dimension(300, 200));
            p2.setBorder(new CompoundBorder(new TitledBorder("JPanel 2"), new EmptyBorder(3,3,3,3)));
            p2.add(new JLabel("hello"), BorderLayout.NORTH);
           
            pane.add(p1, BorderLayout.NORTH);
            pane.add(p2, BorderLayout.CENTER);
            
             }
        
        public void addText(){
            p2.add(new JLabel("hi"), BorderLayout.SOUTH);
            p2.repaint();
        }
        
        public static void main(String[] args) {
            new What();
        }
        
    }

  2. #2
    simon77 is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default JPanel reseting

    hello,
    I've been working on an app these days and I got stuck with one situation.
    I wrote for you basic code that illustrates my problem.
    After pressing the "add text" button, in JPanel 2 should appear a new message "hi" but it doesn't work.
    I've tried many things, searched many sites, but I couldn't find the solution for my problem.
    If you can see it, please let me know.. thanx :)


    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.TitledBorder;
    
    /**
     *
     * @author simon
     */
    public class What {
    
        private JPanel p1;
        private JPanel p2;
        private Container pane;
        
        public What(){
            JFrame frame = new JFrame("app");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            start(frame.getContentPane());
            frame.setPreferredSize(new Dimension(300, 300));
            frame.pack();
            frame.setVisible(true); 
        }
        
        public void start(Container pane){
            this.pane = pane;
            pane.setLayout(new BorderLayout());
            
            p1 = new JPanel();
            p2 = new JPanel();
            
            p1.setSize(new Dimension(200, 100));
            p1.setBorder(new CompoundBorder(new TitledBorder("JPanel 1"), new EmptyBorder(3,3,3,3)));
            p1.setLayout(new BorderLayout());
            JButton ok = new JButton("add text");
            ok.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    addText();
                }
            });
            
            p1.add(ok);
            
            p2.setSize(new Dimension(300, 200));
            p2.setBorder(new CompoundBorder(new TitledBorder("JPanel 2"), new EmptyBorder(3,3,3,3)));
            p2.add(new JLabel("hello"), BorderLayout.NORTH);
           
            pane.add(p1, BorderLayout.NORTH);
            pane.add(p2, BorderLayout.CENTER);
            
             }
        
        public void addText(){
            p2.add(new JLabel("hi"), BorderLayout.SOUTH);
            p2.repaint();
        }
        
        public static void main(String[] args) {
            new What();
        }
        
    }

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JPanel reseting

    By adding a new JLabel you are effectively changing the layout. So you need to
    replace you repaint call with

    Java Code:
    p2.revalidate();
    A different approach would be to add a label ahead of time and then simply change
    the text via the setText() method.

    Regards,
    Jim
    Last edited by jim829; 08-13-2014 at 12:54 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: JPanel reseting

    call the revalidate() method instead of repaint() and try
    Ramya:cool:

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: JPanel reseting

    Please don't post the same question in multiple sub forums.
    I've merged them together.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    simon77 is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

Similar Threads

  1. The variables are not reseting even when I specify
    By matrixellox in forum New To Java
    Replies: 9
    Last Post: 03-22-2011, 08:07 PM
  2. clearing/reseting text fields in GUI
    By reis3k in forum New To Java
    Replies: 1
    Last Post: 12-02-2010, 06:12 PM
  3. reseting text fields; .setText("") best option?
    By Unsub in forum Java Applets
    Replies: 2
    Last Post: 02-23-2010, 02:48 AM
  4. [SOLVED] reseting a value
    By s34nn4 in forum Java Applets
    Replies: 5
    Last Post: 11-22-2008, 01:31 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
  •