warum ist die Höhe meines JPanels 0?

Status
Nicht offen für weitere Antworten.

OldSchool

Mitglied
Hi all 🙂

Hab folgendes Problem. Ich hab ein JPanel pCenter und eine splitpane und möchte dass der Balken der splitpane genau in der Mitte des JPanels liegt. Siehe dazu Code einkommentierter Teil.
Jedoch wird die splitpane ganz oben und nicht in der Mitte ausgelegt. Ich weiß auch warum. Wenn ich die Höhe von pCenter ausgeben, ist sie 0.0. Warum das?
Versteh ich nicht!


Code:
public class CourseCourseAnalysis extends JPanel implements GuiInterface {

  private JPanel pCenter;

  private JSplitPane splitPane;

  CourseAnalysisGUI courseAnalysisTop;

  CourseAnalysisGUI courseAnalysisBottom;

  public CourseCourseAnalysis(Mainmenu mainMenu, int rid1, int rid2,
				Connection con, ResourceBundle rb) {
    this.mainMenu = mainMenu;
    this.rb = rb;

    createComponents(rid1, rid2, con);
    initView();
  } // StudentStudentAnalysis()

  private void createComponents(int rid1, int rid2, Connection con) {
    pCenter = new JPanel();

    splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

    courseAnalysisTop = new CourseAnalysisGUI(mainMenu, rid1, con, rb);
    courseAnalysisBottom = new CourseAnalysisGUI(mainMenu, rid2, con, rb);
  } // createComponents()

    
  private void initView() {

    //hole die CenterPanels von den CourseAnalysisGUI-Objekten
    JPanel panelTop = courseAnalysisTop.getCenterPanel();
    JPanel panelBottom = courseAnalysisBottom.getCenterPanel();

    //entferne die BackButtons
    removeBackPanel(panelTop);
    removeBackPanel(panelBottom);

    splitPane.setTopComponent(panelTop);
    splitPane.setBottomComponent(panelBottom);

    pCenter.setLayout(new BorderLayout());
    pCenter.add(splitPane, BorderLayout.CENTER);
    pCenter.add(pBack, BorderLayout.SOUTH);

    //----------------------------------------------    
    //DAS IST WICHTIG
    Dimension dim = pCenter.getSize();
    splitPane.setDividerLocation(dim.getHeight() / 2);

    System.out.println(dim.getHeight());
    //SYSTEM.OUT.PRINTLN LIEFERT 0.0! WARUM????
    //----------------------------------------------
    

    setVisible(true);
  } // initView()

} // CourseCourseAnalysis()
[/b]
 
Ich sehe niergends, dass du das pCenter irgendwo hinzufügst. Aber dann gilt das JPanel als "nicht benutzt", und daher wird auch nie irgendeine Methode aufgerufen, welche die Grösse verändert.

Also musst du das wohl von Hand machen: ruf mal ein "pCenter.validate" auf, und wenn das nichts hilft ein "pCenter.setSize( pCenter.getPreferredSize() );"
 
Aso, dass JPanel pCenter wird aufgefangen und in einem JFrame dargestellt. Hab nämlich alle unwichtigen Teile aus den Code gestrichen. Die Gui geht ja an und für sich.

Also hier zwei pics:

So siehts aus:
pic1.JPG

Der Trennbalken der Splitpane ist ganz oben, weil die Höhe 0.0 ist.

So solls aussehen:
pic2.JPG



Leider geht weder validate noch das andere.

Hab so und so probiert:

Code:
    ...    
    ...
    pCenter.setLayout(new BorderLayout());
    pCenter.add(splitPane, BorderLayout.CENTER);
    pCenter.add(pBack, BorderLayout.SOUTH);

    pCenter.validate();

    Dimension dim = pCenter.getSize();
    splitPane.setDividerLocation(dim.getHeight() / 2);
    ...
    ...

Code:
    ...
    ...    
    pCenter.setLayout(new BorderLayout());
    pCenter.add(splitPane, BorderLayout.CENTER);
    pCenter.add(pBack, BorderLayout.SOUTH);

    pCenter.setSize( pCenter.getPreferredSize() );

    Dimension dim = pCenter.getSize();
    splitPane.setDividerLocation(dim.getHeight() / 2);
    ...
    ...
Beim zweiten krieg ich überhaupt eine Exception
 
Die kommt:
In der Zeile 139, die er anzeigt steht splitPane.setDividerLocation(dim.getHeight() / 2);
Er hat da keinen gültigen Wert oder so


Code:
java.lang.IllegalArgumentException: proportional location must be between 0.0 and 1.0.
	at javax.swing.JSplitPane.setDividerLocation(JSplitPane.java:756)
	at aorta.analysis.compare.CourseCourseAnalysis.initView(CourseCourseAnalysis.java:139)
	at aorta.analysis.compare.CourseCourseAnalysis.<init>(CourseCourseAnalysis.java:86)
	at aorta.util.mainmenu.Mainmenu.changeCenterPanel(Mainmenu.java:366)
	at aorta.util.mainmenu.Mainmenu.chooseNextWindow(Mainmenu.java:249)
	at aorta.util.mainmenu.Menubar.actionPerformed(Menubar.java:263)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764)
	at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
	at javax.swing.AbstractButton.doClick(AbstractButton.java:289)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1113)
	at javax.swing.plaf.basic.BasicMenuItemUI$MouseInputHandler.mouseReleased(BasicMenuItemUI.java:943)
	at java.awt.Component.processMouseEvent(Component.java:5134)
	at java.awt.Component.processEvent(Component.java:4931)
	at java.awt.Container.processEvent(Container.java:1566)
	at java.awt.Component.dispatchEventImpl(Component.java:3639)
	at java.awt.Container.dispatchEventImpl(Container.java:1623)
	at java.awt.Component.dispatchEvent(Component.java:3480)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
	at java.awt.Container.dispatchEventImpl(Container.java:1609)
	at java.awt.Window.dispatchEventImpl(Window.java:1590)
	at java.awt.Component.dispatchEvent(Component.java:3480)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
 
proportional location must be between 0.0 and 1.0.
dim.getHeight() / 2
Offenbar liegt der Wert den du hier einträgst nicht zwischen 0 und 1 (dim.getHeight gibt seltsamerweise ein double zurück, aber bei setDividerLocation wird ein double als verhältnis interpretiert).

Übergieb doch einfach mal 0.5 als Verhältnis.
 
Haut auch nicht hin!

Er interpretiert 0.5 nicht als Verhältnis, sondern schiebt die splitpane einfach um 0.5 runter.
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben