Hi all.
Have some issues with Swing, or more specifically, how to control the layout. I want to have a window where one part, at the bottom, has a rather constant size and the rest is taken by list. Only thing is that when I add things to the list it starts pushing down everything bellow it and resizing the window. I want to make so I instead get a scroll bar, or something like that. Not that good with layout managers, to be honest, so it is a real challenge. My code at the moment looks like this:
The method will create a frame and return it. mLstModel is the model for the list and mClose is the action listener for the close button (I made it a variable because I figured it would mean less objects will be created, which may make things easier for the gc... or something, lol, I may be thinking completely wrong). Not asking for you to code for me, but any pointers and help with how to make it work the way I want it to, i.e. making so adding stuff to the list won´t push down the components bellow it and won´t start to resize the window, would be deeply appreciated./** * Creates and returns a window frame */ protected JFrame createFrame() { // Create the window JPanel content = new JPanel( true ); content.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridheight = 1; c.weightx = 1; c.weighty = 1; c.insets = new Insets(1, 1, 1, 1); // Create the list JList l = new JList(); l.setBorder(new EtchedBorder( EtchedBorder.LOWERED )); l.setSize(400, 500); l.setModel( mLstModel ); c.fill = GridBagConstraints.BOTH; c.gridwidth = 3; c.gridx = 0; c.gridy = 0; content.add(l, c); // Add the information panel JPanel info = new JPanel( true ); info.setBorder(new TitledBorder(new EtchedBorder( EtchedBorder.LOWERED ), "Info")); c.fill = GridBagConstraints.HORIZONTAL; c.gridy = 2; content.add(info, c); // Set the constraints that applies to the buttons c.fill = GridBagConstraints.NONE; c.gridwidth = 1; c.weighty = 0; // Add the buttons c.gridy = 4; JButton btn = new JButton( "Close" ); btn.addActionListener( mClose ); c.gridx = 2; c.anchor = GridBagConstraints.LAST_LINE_END; content.add(btn, c); // Return the window JFrame win = new JFrame( "View logs" ); win.add( content ); win.pack(); return win; }
Tell me if you want more code, this method is the only one that actually creates the window and its components. The rest just deals with what is created, so to speak.
Take care,
Kerr.