0

I'm creating a GUI in Java using a JFrame, JPanel, JBUtton, and two JLists. When I create the GUI the JFrame appears, along with the JPanel and JButton, but the two JLists don't.

I'm using an absolute layout manager (null) which i think is part of the problem, however I kind of need to use an absolute layout manager.

Here is parts of the code

public class Display extends JFrame {

    protected JPanel mPanel;
    protected JList mGradingList;
    protected JList mCompletedList;
    protected JButton mButton;

    public Display() {
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setSize(640, 320);
        this.setResizable(false);
        this.setLocationRelativeTo(null);

        mPanel = new JPanel(null);
        this.add(mPanel);

        //<more code>

        mCompletedList = new JList();
        mCompletedList.setBorder(new CompoundBorder(
                BorderFactory.createLineBorder(new Color(122, 138, 152)),
                BorderFactory.createEmptyBorder(8, 8, 8, 8)
        ));
        mCompletedList.setBackground(new Color(254, 254, 255));
        mCompletedList.setBounds(mPanel.getWidth() - 210, 10, 200, mPanel.getHeight() - 20);
        mPanel.add(mCompletedList);

        this.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ndavis319
  • 11
  • 4
  • `mPanel.getWidth()` return 0 .so you are adding to minus position – Madhawa Priyashantha Mar 20 '16 at 15:03
  • 2
    Look at the [questions tagged as `null-layout-manager`](http://stackoverflow.com/questions/tagged/null-layout-manager). Look through the ones with accepted answers. See how many of those accepted answers basically boil down to 'use X layout'. I bet it would be 19 out of 20 (if not more). - Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) .. – Andrew Thompson Mar 20 '16 at 15:05
  • 1
    .. along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Mar 20 '16 at 15:05
  • *"..I kind of need to use an absolute layout manager."* ..Why? Not 'kind of' why, but 'specifically' why? – Andrew Thompson Mar 20 '16 at 15:09
  • @FastSnail Why does mPanel.getWidth() return 0? – ndavis319 Mar 20 '16 at 15:10
  • @AndrewThompson The controls on my GUI need to be able to move around pixel by pixel – ndavis319 Mar 20 '16 at 15:11
  • 1
    i don't know but as thopson said you shoudn't use null layout – Madhawa Priyashantha Mar 20 '16 at 15:11
  • 2
    *"The controls on my GUI need to be able to move around"* Under what circumstances? This is a good time for a few more words, rather than less. I am not in the mood for playing '20 questions'. BTW - Layouts are good at achieving that effect when the GUI is resized. – Andrew Thompson Mar 20 '16 at 15:13
  • @AndrewThompson You're getting worked up over trying to have me change how the controls are to be laid out, which I said I am not allowed to do. If you don't know how to fix the issue I describe, without going against the condition I specified, then there is no need to continue this banter. – ndavis319 Mar 20 '16 at 16:25
  • 2
    *"which I said I am not allowed to do."* That's the first time you mentioned 'not allowed'. Be sure to triple the cost to the client, since it will take that much extra time to get this mess working. Oh, and good luck with it. I have better things to do than try and make null layouts work. – Andrew Thompson Mar 20 '16 at 17:22
  • 3
    `Why does mPanel.getWidth() return 0? ` - because components don't have a size until the frame has been made visible. Another reason you should be using Swing the way it was designed which is to use layout managers. Then when the layout managers are invoked all sizes will be resolved. `The controls on my GUI need to be able to move around pixel by pixel` - ridiculous requirement. Swing components like text fields, buttons, lists do just move around by a pixel. – camickr Mar 20 '16 at 18:15
  • 1
    @ndavis319"Why does mPanel.getWidth() return 0?"* - Because it's not be laid out yet but it's parent container, until the container is added to a realised container and validated, none of the components will have a defined sze – MadProgrammer Mar 20 '16 at 21:20

0 Answers0