1

So I'm making a bulletin board project for my class and it gets all the data from a separate server. I need to make a GUI for it that can submit questions and draw the messages on a Jpanel. I have the panels separated into their own files like so,

private Client() {

        //Set up Main Frame
        JFrame frame = new JFrame("Chat Client");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1000, 1000);

        ConnectPanel connectPanel = new ConnectPanel(st, this.grid);
        PostPanel    postPanel    = new PostPanel();
        GetPanel     getPanel     = new GetPanel();
        PinPanel     pinPanel     = new PinPanel();
        ClearPanel   clearPanel   = new ClearPanel();

        Board boardPanel = new Board(this.grid);

        JTabbedPane controls = new JTabbedPane(JTabbedPane.LEFT);

        controls.add("Connect",connectPanel);
        controls.add("Post",postPanel);
        controls.add("Get",getPanel);
        controls.add("Pin/Unpin",pinPanel);
        controls.add("Clear/Shake",clearPanel);

        //Adding Components to the frame.
        frame.add(controls,BorderLayout.WEST);
        frame.add(boardPanel,BorderLayout.EAST);

        frame.setVisible(true);

    }

I have a main Grid object in the Client Class(main class) to keep track of my messages, but I don't have a way to update the boardPanel when a button in connectPanel is clicked. I tried actionListeners but that doesn't seem to work or a thread that runs in an infinite loop. Any ideas on how I could make this work? Thanks!

Max Rush
  • 41
  • 4
  • 4
    You need to think about ways that you can expose a variable/component in `boardPanel` to the `connectPanel` class. This could be achieved by using static class variables inside the central `Client` class, although that is not recommended, otherwise, you need to pass the component that needs changing to the `connectPanel` on creation, for example `connectPanel = new ConnectPanel(st, this.grid, boardPanel.someComponent);` then inside the `boardPanel` constructor you need to store that passed component so that your action event can change it later. – sorifiend Sep 29 '21 at 23:50
  • 5
    You can use an `Observer` and `Observable` design pattern. Basically the Observable class will notify any Observers when an event occurs. – camickr Sep 29 '21 at 23:51
  • 1
    Some common ways to implement the observer pattern are cited [here](https://stackoverflow.com/a/3072979/230513). – trashgod Sep 30 '21 at 14:31

0 Answers0