[Swing] JList verschwindet wenn man sie in ein JScrollPane verpackt

Prophet05

Erfahrenes Mitglied
Hallo,

ich habe ein JPanel in das ich verschiedene Komponenten mit einem GridBagLayout verpacke. Unter anderem auch eine JList. Wenn ich die JList nun so in das GridBagLayout einfüge ist sie wie gewollt sichtbar und nimmt so viel platz ein wie sei bekommen kann. Wenn ich die JList jetzt aber in ein JScrollPane verpacke verschwindet sie gänzlich und ich sehe sie gar nicht. Ich verstehe nicht warum. Hier des entscheidende Ausschnitt aus meinem Quelltext:
Code:
public OceanControlPanel() {
        
        this.uiOceanObjectList    = new JList();
        this.uiRemoveObjectButton = new JButton("Remove");
        this.uiAddObjectButton    = new JButton("Add");
        this.uiStartStopSimButton = new JButton("Start");
        this.uiOceanPanel         = new OceanPanel();
        
        this.initLayout();
        
    }
    
    /**
     * Initializes the layout of the components on the panel.
     */
    private void initLayout() {
        
        this.setLayout(new BorderLayout());
        
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false);
        
        splitPane.setLeftComponent(new JScrollPane(this.uiOceanPanel));
        
        // Setup the panel with the control items of the ocean.
        JPanel controlPanel = new JPanel();
        controlPanel.add(this.uiStartStopSimButton);
        controlPanel.add(this.uiAddObjectButton);
        controlPanel.add(this.uiRemoveObjectButton);
        controlPanel.add(this.uiOceanObjectList);
        
        // Set the layout of the control panel.
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        
        controlPanel.setLayout(gbl);
        
        // Basic settings
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridx = 0;
        constraints.insets = new Insets(3,3,3,3);
        constraints.weightx = 1.0;
        constraints.weighty = 0.0;
        constraints.gridheight = 1;
        constraints.gridwidth  = 1;
        
        constraints.gridy = 0;
        gbl.addLayoutComponent(this.uiStartStopSimButton, constraints);
        
        constraints.gridy = 1;
        gbl.addLayoutComponent(this.uiAddObjectButton, constraints);
        
        constraints.gridy = 2;
        gbl.addLayoutComponent(this.uiRemoveObjectButton, constraints);
        
        constraints.gridy = 3;
        constraints.weighty = 1.0;
        constraints.fill = GridBagConstraints.BOTH;
        // So verschwindet sie plötzlich:
        //gbl.addLayoutComponent(new JScrollPane(this.uiOceanObjectList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS), constraints);
        // So wird sie richtig angezeigt, das bring mit aber nichts weil ich sie ja gerne scrollen würde...
        gbl.addLayoutComponent(this.uiOceanObjectList, constraints);
        
        splitPane.setRightComponent(controlPanel);
        
        // Insert the split pane.
        this.add(splitPane, BorderLayout.CENTER);
        
    }

Die besagte Liste ist this.uiOceanObjectList.
 
Eigentlich fügt man die Komponente nicht dem Layout hinzu, sondern dem Panel. Dann klappts auch mit dem Scrolling.

Poste beim nächsten Mal bitte ein vollständig lauffähiges Beispiel. Ich mußte noch einiges drumherum bauen ums bei mir nachzuvollziehen.

Java:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public class OceanControlPanel extends JFrame {

	private JList uiOceanObjectList;
	private JButton uiRemoveObjectButton;
	private JButton uiAddObjectButton;
	private JButton uiStartStopSimButton;

	public OceanControlPanel() {
		Vector<String> test = new Vector<String>(Arrays.asList("1", "2"));
		this.uiOceanObjectList = new JList(test);
		this.uiRemoveObjectButton = new JButton("Remove");
		this.uiAddObjectButton = new JButton("Add");
		this.uiStartStopSimButton = new JButton("Start");

		this.initLayout( );

	}

	/**
	 * Initializes the layout of the components on the panel.
	 */
	private void initLayout() {

		this.setLayout(new BorderLayout( ));

		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				false);

		splitPane.setLeftComponent(new JScrollPane( ));

		// Setup the panel with the control items of the ocean.
		JPanel controlPanel = new JPanel( );
		controlPanel.add(this.uiStartStopSimButton);
		controlPanel.add(this.uiAddObjectButton);
		controlPanel.add(this.uiRemoveObjectButton);
		controlPanel.add(this.uiOceanObjectList);

		// Set the layout of the control panel.
		GridBagLayout gbl = new GridBagLayout( );
		GridBagConstraints constraints = new GridBagConstraints( );

		controlPanel.setLayout(gbl);

		// Basic settings
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 0;
		constraints.insets = new Insets(3, 3, 3, 3);
		constraints.weightx = 1.0;
		constraints.weighty = 0.0;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;

		constraints.gridy = 0;
		controlPanel.add(this.uiStartStopSimButton, constraints);

		constraints.gridy = 1;
		controlPanel.add(this.uiAddObjectButton, constraints);

		constraints.gridy = 2;
		controlPanel.add(this.uiRemoveObjectButton, constraints);

		constraints.gridy = 3;
		constraints.weighty = 1.0;
		constraints.fill = GridBagConstraints.BOTH;
		// So verschwindet sie plötzlich:
		controlPanel.add(new JScrollPane(this.uiOceanObjectList,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS), constraints);
		// So wird sie richtig angezeigt, das bring mit aber nichts
		// weil ich sie ja gerne scrollen würde...
		// gbl.addLayoutComponent(this.uiOceanObjectList,
		// constraints);

		splitPane.setRightComponent(controlPanel);

		// Insert the split pane.
		this.add(splitPane, BorderLayout.CENTER);

	}

	public static void main(String[] args) {
		OceanControlPanel p = new OceanControlPanel( );
		p.setSize(300, 300);
		p.setVisible(true);
	}
}
 
Danke für deine Hilfe, so geht es. Also ich habe das bis jetzt immer so gemacht und bisher nie Probleme damit gehabt. Kannst du mir erklären wo der unterschied zwischen den beiden Varianten ist? Wenn ich das richtig verstanden habe machen doch beide das selbe? "add" ruft ja im Hintergrund auch einfach nur "addLayoutComponent" auf oder?

Gruß, Prophet
 

Neue Beiträge

Zurück