Results 1 to 4 of 4

Thread: JPanel update.

  1. #1
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation JPanel update.

    Greetings.

    I am having a problem updating the contents of a JPanel inside a JFrame.
    I wanted to update the JPanel by means of re-initializing the JPanel by other class extended by JPanel. (Other means are considered).
    How can I resolve this thread?
    Here's the my code.

    Java Code:
    package sampleproject;
    
    import java.awt.Graphics;
    import java.awt.Dimension;
    import java.awt.BorderLayout;
    import java.awt.Color;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    
    public class Main extends JFrame {
    
        private final String[] buttonNames = {"Sample", "Test"};
        private final JButton[] buttons = new JButton[buttonNames.length];
        private final ButtonHandler listener = new ButtonHandler();
        private final JPanel buttonPanel = new JPanel();
        private JPanel centerPanel = new JPanel();
    
        public Main() {
            super("Sample Frame");
            setLayout(new BorderLayout());
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(new Dimension(800, 600));
            setResizable(false);
            setVisible(true);
    
            for(int i=0; i<buttonNames.length; i++) {
                buttons[i] = new JButton(buttonNames[i]);
                buttons[i].addActionListener(listener);
                buttonPanel.add(buttons[i]);
            }
    
            add(buttonPanel, BorderLayout.NORTH);
            add(centerPanel, BorderLayout.CENTER);
    
        }
    
        private class ButtonHandler implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent event) {
                if (event.getActionCommand().equals("Sample")) {
                    centerPanel = new CenterPanel(event.getActionCommand());
                    centerPanel.revalidate();
                    centerPanel.repaint();
                } else if (event.getActionCommand().equals("Test")) {
                    centerPanel = new NewPanel();
                    centerPanel.revalidate();
                    centerPanel.repaint();
                }
                
            }
        }
    
        private class CenterPanel extends JPanel {
    
            private String message = "";
    
            public CenterPanel(String message) {
                this.message = message;
            }
           
            public void paintComponent(Graphics g) {
                g.drawString(message, 0, 0);            
            }
    
        }
    
        private class NewPanel extends JPanel {
            
            public NewPanel() {
                setBackground(Color.RED);
            }                
            
        }
    
        public static void main(String[] args) {
            new Main();
        }
    
    }
    Thanks,
    Cyril H.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

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

    Default

    If you want quicker help, you should tell us the details of your problem.

    But nevertheless, if you change the JPanel that a variable refers to, it will have absolutely no effect on the JPanel that is being displayed. You need to either change the state of the JPanel being displayed (probably the best solution), or swap JPanels held by the container, possibly by using a CardLayout.

  3. #3
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Actually, my problem is how to change to contents of my JPanel with different views that extends JPanel. The change happens is you click a button. Well instead of using JTabbedPane, I prefer using JButton.

    Swapping the JPanels? How?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Quote Originally Posted by chyrl View Post
    Swapping the JPanels? How?
    You've already been told. Or didn't you read all the way to the last word in Fubarable's response?

    db

Similar Threads

  1. Problem about JLabel not update in JPanel
    By fantasyme in forum Java Applets
    Replies: 11
    Last Post: 10-16-2011, 07:00 AM
  2. Update JPanel
    By collin389 in forum New To Java
    Replies: 3
    Last Post: 11-25-2009, 11:30 PM
  3. JPanel won't update
    By ibanez270dx in forum New To Java
    Replies: 3
    Last Post: 01-06-2009, 08:59 PM
  4. How to update my jdk???
    By low224 in forum New To Java
    Replies: 4
    Last Post: 01-04-2009, 04:51 PM
  5. Using sql:update tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 11:49 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
  •