Member Avatar for Illidanek

I have an issue with positioning of a JList in a JPanel, I am using absolute positioning, and I don't want to switch to gridlayout or any of that stuff cuz the whole program is in absolute positioning and it wud mess everything up.

The problem is that once the list is positioned, it will sometimes move to the position it is supposed to be in, but sometimes, for example when I recompile, without changing anything, It will move to the top left of the screen.
Then the list might return to where it is supposed to be after a while.
This all seems very random, and it happens even if I dont change a single character in the code.

Here's the code for the positioning of the list.
This is part of the constructor.

model = new DefaultListModel(); // declares the list model
      list = new JList(model); // adds the list model to the list
			
  for( int i = 0; i < numAudiobooks; i++)
  	{
  	// adds elements to the list here
  			
  	}
  	list.setSelectedIndex(0);
  	scrolling = new JScrollPane(list);
thepanel.add(scrolling);
list.setBounds(500, 500, 200, 200);
thepanel.add( list );

Please help

Is there a layout manager active for thePanel? Are you fighting it?

Member Avatar for Illidanek

Is there a layout manager active for thePanel? Are you fighting it?

No I don't think there is a layout manager active, i do set the layout to null which is what you need to do for absolute positioning.

Hard to tell when you only post a few lines of code.
Do you have a minimal program that demos the problem?

Member Avatar for Illidanek

Hard to tell when you only post a few lines of code.
Do you have a minimal program that demos the problem?

Here's one:

import javax.swing.*;
class ListPositioning
{
	JPanel thepanel = new JPanel();
	JFrame frame = new JFrame();
	DefaultListModel model;
	JList list = new JList();
	private JScrollPane scrolling;	
	public ListPositioning()
	{
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  		frame.getContentPane().add(thepanel); // adds the panel to the frame
  		frame.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
  		thepanel.setLayout(null);
  		model = new DefaultListModel();
  		list = new JList(model);
  		for(int i = 0; i < 3; i++)
  		{
  			model.addElement("Element " + i);
  		}
  		list.setSelectedIndex(0);
  		scrolling = new JScrollPane(list);
  		thepanel.add(scrolling);
  		list.setBounds(415, 100, 600, 500);
  		thepanel.add( list );
  		frame.setVisible(true);
	}
	public static void main(String[] args)
	{
	new ListPositioning();
	}
}

This is just an example, the real one has many buttons, a bucket full of actionlisteners and many other things.
It's just that from time to time, it will move to that top left corner, usually more often on certain computers than others.

Ok, I've got it changing from one execution to another.

More later.

Here's some debugging code added to your code. No idea why yet. Other than you're mixing layout managers up.

import javax.swing.*;

public class ListPositioning
{
	JPanel thepanel = new JPanel();
	JFrame frame = new JFrame();
	DefaultListModel model;
	JList list;
	private JScrollPane scrolling;	

	public ListPositioning()	{
	   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  		frame.getContentPane().add(thepanel); // adds the panel to the frame
  		frame.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());

  		thepanel.setLayout(null);

  		model = new DefaultListModel();
  		list = new JList(model) {
      public void setBounds(int x, int y, int w, int h) {
        super.setBounds(x,y,w,h);
        System.out.println("bnds set to: x=" +x+ ", y= " + y);
        try{throw new Exception("Who called");}catch(Exception ex){ex.printStackTrace();};
      }
      };


  		for(int i = 0; i < 3; i++)
  		{
  			model.addElement("Element " + i);
  		}
  		list.setSelectedIndex(0);

  		scrolling = new JScrollPane(list);
  		thepanel.add(scrolling);

  		list.setBounds(415, 100, 600, 500);
  		thepanel.add( list );

  		frame.setVisible(true);
      System.out.println("pnl @=" +  thepanel.getLocationOnScreen() + ", list @=" + list.getLocationOnScreen()
                         + ", list2@=" + list.getLocation());
      //pnl @=java.awt.Point[x=4,y=26], list @=java.awt.Point[x=419,y=126], list2@=java.awt.Point[x=415,y=100]<< Centered
      //pnl @=java.awt.Point[x=4,y=26], list @=java.awt.Point[x=4,y=26], list2@=java.awt.Point[x=0,y=0]     << upper left corner
	}

   //---------------------------------------------------
	public static void main(String[] args)  	{
	   new ListPositioning();
	}
}

/* Output        when list in upper left corner

Running: "C:\Program Files\Java\j2re1.4.2_08\bin\java.exe" -cp D:\JavaDevelopment;.;acm.jar ListPositioning

bnds set to: x=415, y= 0
java.lang.Exception: Who called
	at ListPositioning$1.setBounds(ListPositioning.java:37)
	at java.awt.Component.move(Unknown Source)
	at java.awt.Component.setLocation(Unknown Source)
	at javax.swing.JViewport.setViewPosition(Unknown Source)
	at javax.swing.plaf.basic.BasicScrollPaneUI$VSBChangeListener.stateChanged(Unknown Source)
	at javax.swing.DefaultBoundedRangeModel.fireStateChanged(Unknown Source)
	at javax.swing.DefaultBoundedRangeModel.setRangeProperties(Unknown Source)
	at javax.swing.JScrollBar.setValues(Unknown Source)
	at javax.swing.plaf.basic.BasicScrollPaneUI.syncScrollPaneWithViewport(Unknown Source)
	at javax.swing.plaf.basic.BasicScrollPaneUI$ViewportChangeHandler.stateChanged(Unknown Source)
	at javax.swing.JViewport.fireStateChanged(Unknown Source)
	at javax.swing.JViewport$ViewListener.componentResized(Unknown Source)
	at java.awt.Component.processComponentEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
bnds set to: x=0, y= 0
java.lang.Exception: Who called
	at ListPositioning$1.setBounds(ListPositioning.java:37)
	at java.awt.Component.move(Unknown Source)
	at java.awt.Component.setLocation(Unknown Source)
	at javax.swing.JViewport.setViewPosition(Unknown Source)
	at javax.swing.plaf.basic.BasicScrollPaneUI$HSBChangeListener.stateChanged(Unknown Source)
	at javax.swing.DefaultBoundedRangeModel.fireStateChanged(Unknown Source)
	at javax.swing.DefaultBoundedRangeModel.setRangeProperties(Unknown Source)
	at javax.swing.JScrollBar.setValues(Unknown Source)
	at javax.swing.plaf.basic.BasicScrollPaneUI.syncScrollPaneWithViewport(Unknown Source)
	at javax.swing.plaf.basic.BasicScrollPaneUI$ViewportChangeHandler.stateChanged(Unknown Source)
	at javax.swing.JViewport.fireStateChanged(Unknown Source)
	at javax.swing.JViewport.setViewPosition(Unknown Source)
	at javax.swing.plaf.basic.BasicScrollPaneUI$VSBChangeListener.stateChanged(Unknown Source)
	at javax.swing.DefaultBoundedRangeModel.fireStateChanged(Unknown Source)
	at javax.swing.DefaultBoundedRangeModel.setRangeProperties(Unknown Source)
	at javax.swing.JScrollBar.setValues(Unknown Source)
	at javax.swing.plaf.basic.BasicScrollPaneUI.syncScrollPaneWithViewport(Unknown Source)
	at javax.swing.plaf.basic.BasicScrollPaneUI$ViewportChangeHandler.stateChanged(Unknown Source)
	at javax.swing.JViewport.fireStateChanged(Unknown Source)
	at javax.swing.JViewport$ViewListener.componentResized(Unknown Source)
	at java.awt.Component.processComponentEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
bnds set to: x=415, y= 100
java.lang.Exception: Who called
	at ListPositioning$1.setBounds(ListPositioning.java:37)
	at ListPositioning.<init>(ListPositioning.java:51)
	at ListPositioning.main(ListPositioning.java:63)
pnl @=java.awt.Point[x=4,y=26], list @=java.awt.Point[x=4,y=26], list2@=java.awt.Point[x=0,y=0]

0 error(s)

>>>>>>>>>>>>>>>>>>>> When centered <<<<<<<<<<<<<<<<<<<<<<<<<
Running: "C:\Program Files\Java\j2re1.4.2_08\bin\java.exe" -cp D:\JavaDevelopment;.;acm.jar ListPositioning

bnds set to: x=415, y= 100
java.lang.Exception: Who called
	at ListPositioning$1.setBounds(ListPositioning.java:37)
	at ListPositioning.<init>(ListPositioning.java:51)
	at ListPositioning.main(ListPositioning.java:63)
pnl @=java.awt.Point[x=4,y=26], list @=java.awt.Point[x=419,y=126], list2@=java.awt.Point[x=415,y=100]

*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.