Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding components in GUI upon repaint method

I'm not understanding Java GUI's as well as I thought. In my paint method for a frame, I'd like to wipe all of the current buttons, and add new ones. (The overall goal is to have an interface where the user can see characters and click on the buttons to download documents related to the character. Since every character is different, when the user selects a new user from my list, a new set of documents and buttons will be available to them.)

This is a test frame that I just wrote that shows where things go sideways. It has the similar paradigms that I use in my actual program, without too much clutter:

public class GUITest extends JFrame
{

/**
 * @param args
 */
public static void main(String[] args)
{
    Container gui_test = new GUITest();

}

private JComponent content = null;

public GUITest()
{
    super();

    setVisible(true);
}

public void paint(Graphics g)
{
    this.removeAll();

    content = new JPanel();

    JComponent test_button = new JButton("New Button 1");
    JComponent button = new JButton("New Button 2");

    content.add(button);
    content.add(test_button);

    this.add(content);

    super.paint(g);
}

}

Without the call to removeAll(), buttons will continue to be thrown on top of the JPanel, but with the call, nothing shows up. I don't know why this is, as I'm adding the components appropriately, right?

Edit
Got it, let me give you a more detailed breakdown. A client is navigating my program by looking at a list of characters in a game on a west panel. They can select a row from the list which will show char details on the east panel. The details are an image and description. Recently, I added relevant documents for that particular char, which will show on the bottom of the east panel. I created key listener's, so the client can quickly view the document by pressing a num key, but I also want to give them the ability to click on the button to launch a pdf view and see the contents of the document.

Since every char has different related docs and different number of docs, I repainted the buttons every time, to reflect the amount of related docs and the appropriate titles for the docs. This is where the repaint is acting strange. You gave me a good explanation of what's going wrong, but I don't know how to give the client access to the docs now, aside from painting a description of the doc along with the hot key needed to launch it. Does that make sense?

like image 911
Sal Avatar asked Dec 26 '22 21:12

Sal


2 Answers

Never add components to your GUI or remove components in the paint or paintComponent methods. Just don't do it. Ever. Period.

These methods are for drawing only, and need to be as fast as possible, else your program will appear unresponsive. Not only that, you do not have full control over when or even if these methods will be called, so program logic and structure should not go into these methods.

Instead react to user events with event listeners such as ActionListeners, ListSelectionListeners, or with key bindings.

Edit
Regarding

Got it, let me give you a more detailed breakdown. A client is navigating my program by looking at a list of characters in a game on a west panel. They can select a row from the list which will show char details on the east panel. The details are an image and description. Recently, I added relevant documents for that particular char, which will show on the bottom of the east panel. I created key listener's, so the client can quickly view the document by pressing a num key, but I also want to give them the ability to click on the button to launch a pdf view and see the contents of the document.

I'd use a JList to hold the list of selectable information on the left, and would react to it with a ListSelectionListener. In the listener, I'd change the related displayed information. I also avoid using KeyListeners with Swing but instead gravitate towards Key Bindings as they're more flexible and less rigid regarding focus.

Regarding

Since every char has different related docs and different number of docs, I repainted the buttons every time, to reflect the amount of related docs and the appropriate titles for the docs. This is where the repaint is acting strange. You gave me a good explanation of what's going wrong, but I don't know how to give the client access to the docs now, aside from painting a description of the doc along with the hot key needed to launch it. Does that make sense?

I'm not sure what you're doing here or what you're trying to do.

like image 59
Hovercraft Full Of Eels Avatar answered Jan 06 '23 05:01

Hovercraft Full Of Eels


Since every char has different related docs and different number of docs, I repainted the buttons every time, to reflect the amount of related docs and the appropriate titles for the docs. This is where the repaint is acting strange. You gave me a good explanation of what's going wrong, but I don't know how to give the client access to the docs now, aside from painting a description of the doc along with the hot key needed to launch it. Does that make sense?

So rather then "painting" the buttons, why not just change there text (setText(...)).

When a user selects a "char". You are going to need to rebuild portions of your screen. Change the list model (as suggest above) and remove/add any buttons you need on the document container.

like image 30
MadProgrammer Avatar answered Jan 06 '23 04:01

MadProgrammer