tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
1053
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Registriert seit
    Mar 2005
    Beiträge
    367
    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 :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    
    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.
     

  2. #2
    Avatar von zeja
    zeja zeja ist offline Mitglied Diamant
    tutorials.de Premium-User
    Registriert seit
    Sep 2006
    Beiträge
    2.962
    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.

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    
    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);
        }
    }
     

  3. #3
    Registriert seit
    Mar 2005
    Beiträge
    367
    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
     

Ähnliche Themen

  1. jscrollpane mit Bildern (swing)
    Von hitomie im Forum Java Grundlagen
    Antworten: 7
    Letzter Beitrag: 12.06.10, 00:55
  2. JScrollPane innherhalt einer JList
    Von fishskin im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 0
    Letzter Beitrag: 23.04.07, 14:31
  3. Swing JPanel verschwindet wenn Fenster minimiert wird
    Von illaX im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 3
    Letzter Beitrag: 26.08.05, 07:58
  4. JList in JScrollPane -- an bestimmte Position springen
    Von swagner im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 2
    Letzter Beitrag: 03.03.05, 12:31
  5. Scrollbuttons für JList (nicht JScrollpane)
    Von Daisho im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 1
    Letzter Beitrag: 06.04.04, 16:25

Stichworte