Announcement

Collapse
No announcement yet.

JScrollPane einrichten

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • JScrollPane einrichten

    Hallo Leute,

    habe ein Problem, so wie fast jeder hier

    Und zwar programmiere ich ein GUI mit der Eingriff in eine Zug-Simulation genommen kann, d.h. Weichen stellen und Züge notbremsen. Sie nennt sich ZLBEmulator, was aber erstmal nicht so wichtig ist.
    Nun möchte ich meiner GUI eine ScrollPane mit einer vertikalen jScrollBar hinzufügen.
    Allerdings bekomme ich dies nicht in den Griff.
    Meine GUI besteht aus 4 Panels. 2 für Text und 2 für Buttons.
    Die Buttons werden durch eine von mir geschriebene Methode erstellt, da die Anzahl veränderbar ist. Diese wird eingelesen und dementsprechend werden die buttonPanels in ihrer Höhe verlängert.
    Nun möchte ich aber für den Frame eine feste Größe angeben (was auch geht) und ihm die ScrollPane hinzufügen.
    Wenn ich die ScrollPane via getContentPane().add(scrollPane) hinzufüge und alle oben genannten Panels dann der ScrollPane hinzufüge wird meine GUI nicht mehr angezeigt. D.h. die Größe des Fensters bleibt erhalten, allerdings ist alles grau und die Komponenten werden nicht mehr angezeigt. So als würde noch etwas davor hängen. Die setOpaque(true) Methode benutze ich aber auch schon.

    Ich poste hier im Anschluss am besten noch den betreffenden Ausschnitt von meinem Source-Code.
    Dieser wurde mit Jigloo erstellt und dann um die "dynamische Buttonerzeugung" von mir erweitert.
    Bei Fragen zum Code, einfach auf den Post antworten

    Code:
    /*
     * ZLB-Emulator-GUI
     * 
     */
    
    	/**
    	 * Inits the GUI.
    	 */
    	private void initGUI() {
    
    		try {
    			getContentPane().setLayout(null);
    			this.setIconImage(new ImageIcon(getClass().getClassLoader()
    					.getResource("vwr/world/zlbemulator/ipslogo.gif"))
    					.getImage());
    			this.setResizable(false);
    			this.setTitle("ZLB Emulator GUI");
    			this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    			
    			{
    				switchIDPanel = new JPanel();
    				FlowLayout switchIDPanelLayout = new FlowLayout(
    						FlowLayout.LEFT, 10, 17);
    				switchIDPanel.setLayout(switchIDPanelLayout);
    				getContentPane().add(switchIDPanel);
    				switchIDPanel.setBounds(0, 31, 98, getGUIheight());
    				{
    					createSwitchIDLabel(getSwitchButtonCount());
    				}
    			}
    			{
    				brakePanel = new JPanel();
    				FlowLayout brakePanelLayout = new FlowLayout(FlowLayout.CENTER,
    						0, 18);
    				getContentPane().add(brakePanel);
    				brakePanel.setBounds(287, 35, 133, getGUIheight());
    				brakePanel.setLayout(brakePanelLayout);
    				createBrakeButtons(getBrakeButtonCount());
    			}
    			{
    				textPanel = new JPanel();
    				getContentPane().add(textPanel);
    				textPanel.setBounds(0, 0, 420, 35);
    				textPanel.setLayout(null);
    				{
    					switchLabel = new JLabel();
    					textPanel.add(switchLabel);
    					switchLabel.setText("Weichenstellungen ändern");
    					switchLabel.setBounds(28, 7, 182, 28);
    				}
    				{
    					brakeLabel = new JLabel();
    					textPanel.add(brakeLabel);
    					brakeLabel.setText("Zwangsbremsungen einleiten");
    					brakeLabel.setBounds(245, 7, 175, 28);
    					brakeLabel.setHorizontalTextPosition(SwingConstants.CENTER);
    					brakeLabel.setHorizontalAlignment(SwingConstants.CENTER);
    				}
    			}
    			
    			{
    				jScrollPane1 = new JScrollPane();
    				
    				//jScrollPane1.getVerticalScrollBar().setPreferredSize(new java.awt.Dimension(17,50));				
    				jScrollPane1.setBounds(420, 0, 21, getGUIheight() - 35);
    				jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    				jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    				jScrollPane1.getHorizontalScrollBar().setOpaque(false);
    				jScrollPane1.getVerticalScrollBar().setOpaque(true);
    				jScrollPane1.getVerticalScrollBar().setEnabled(true);
    				jScrollPane1.getHorizontalScrollBar().setEnabled(false);
    				getContentPane().add(jScrollPane1);
    			}
    			{
    				switchPanel = new JPanel();
    				FlowLayout switchPanelLayout = new FlowLayout();
    				getContentPane().add(switchPanel);
    				switchPanel.setBounds(98, 35, 189, getGUIheight());
    				switchPanel.setLayout(switchPanelLayout);
    				createSwitchButtons(getSwitchButtonCount());
    			}
    			pack();
    			//this.setSize(652, 454);
    			this.setSize(449, 570);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * Creates the switch ID labels containing the switch IDs.
    	 * 
    	 * @param switchButtonCount
    	 *            the switch button count
    	 */
    	private void createSwitchIDLabel(int switchButtonCount) {
    		for (int i = 0; i < switchButtonCount; i++) {
    			JLabel switchLabel = new JLabel("Weichen ID " + i);
    			switchLabel.setFont(new java.awt.Font("Arial", 0, 12));
    			switchIDPanel.add(switchLabel);
    		}
    
    	}
    
    	/**
    	 * Calculates the GUi height. Depending on the maximum count of buttons in
    	 * one of the both ButtonPanels.
    	 * 
    	 * @return the GUI height
    	 */
    	private int getGUIheight() {
    		int height = 0;
    		if (getSwitchButtonCount() < getBrakeButtonCount()) {
    			for (int i = 0; i < getBrakeButtonCount(); i++) {
    				height += 43;
    			}
    			return height;
    		} else {
    			for (int i = 0; i < getSwitchButtonCount(); i++) {
    				height += 39;
    			}
    			return height;
    		}
    
    	}
    
    	/**
    	 * Creates the brake buttons in the brakePanel and adds the brkListener to
    	 * them.
    	 * 
    	 * @param count
    	 *            the count
    	 */
    	private void createBrakeButtons(int count) {
    
    		BrakeButtonListener brkListener = new BrakeButtonListener();
    		FlowLayout brkButtonLayout = new FlowLayout(FlowLayout.CENTER, 50, 10);
    		brakePanel.setLayout(brkButtonLayout);
    
    		for (int i = 0; i < count; i++) {
    			JToggleButton brakeButton = new JToggleButton("PZB ID  " + i, false);
    			brakeButton.setName("PZB ID  " + i);
    			brakeButton.setDoubleBuffered(true);
    			brakeButton.setBackground(new java.awt.Color(0, 255, 0));
    			brakeButton.addActionListener(brkListener);
    			brakePanel.add(brakeButton);
    			brakeButton.setDoubleBuffered(true);
    			pzbID.put(brakeButton, i);
    		}
    	}
    
    	/**
    	 * Creates the switch buttons and adds an ActionListener and the ButtonGroup
    	 * to them.
    	 * 
    	 * @param count
    	 *            the count
    	 */
    	private void createSwitchButtons(int count) {
    		SwitchAdjustmentListener switchListener = new SwitchAdjustmentListener();
    		FlowLayout swtButtonLayout = new FlowLayout(FlowLayout.LEFT, 10, 10);
    		switchPanel.setLayout(swtButtonLayout);
    		for (int i = 0; i < getSwitchButtonCount(); i++) {
    			ButtonGroup switchGroup = new ButtonGroup();
    			JRadioButton straightButton = new JRadioButton("straight", true);
    			JRadioButton extraButton = new JRadioButton("extra");
    			straightButton.setName("straightID" + i);
    			extraButton.setName("extraID" + i);
    			switchGroup.add(straightButton);
    			switchGroup.add(extraButton);
    			switchButtonID.put(straightButton, i);
    			switchButtonID.put(extraButton, i);
    			switchPanel.add(straightButton);
    			switchPanel.add(extraButton);
    			straightButton.addActionListener(switchListener);
    			extraButton.addActionListener(switchListener);
    
    		}
    	}
    
    }
    Zuletzt editiert von NitroX; 20.06.2007, 15:08.

  • #2
    Hier noch ein Bild der GUI. Vielleicht hilft das ja bei der Fehlersuche..
    Attached Files

    Comment


    • #3
      Hallo!

      Wenn ich es auf die Schnelle richtig sehe, hast Du die ScrollPane in die Content-Pane des Frames hinzugefügt, aber Deine Panels auch! Mit anderen Worten: die anderen Panels liegen auch auf der Content-Pane und nicht in der ScrollPane. Und in die ScrollPane hast Du gar nichts gelegt.

      In der Hoffnung geholfen zu haben,
      Stefan

      Comment


      • #4
        also wenn ich dich dann richtig verstanden habe muss ich das ganze wie anhängen?

        contentPane -> scrollpane -> textPanels,buttonPanels ?

        schonmal vielen dank!

        Comment


        • #5
          k, also hab das mal eben so probiert, dann ist aber der komplette frame grau, so als würde er überdeckt werden. muss ich irgendwie noch einen befehl hinzufügen?
          hier der code mit der änderung:

          Code:
          private void initGUI() {
          
          		try {
          			getContentPane().setLayout(null);
          			this.setIconImage(new ImageIcon(getClass().getClassLoader()
          					.getResource("vwr/world/zlbemulator/ipslogo.gif"))
          					.getImage());
          			this.setResizable(true);
          			this.setTitle("ZLB Emulator GUI");
          			this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
          			{
          				jScrollPane = new JScrollPane();
          				jScrollPane.setViewportView(getContentPane());
          				// jScrollPane1.getVerticalScrollBar().setPreferredSize(new
          				// java.awt.Dimension(17,50));
          				jScrollPane.setBounds(420, 0, 21, getGUIheight() - 35);
          				jScrollPane
          						.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          				jScrollPane
          						.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
          				jScrollPane.setOpaque(true);
          				jScrollPane.getVerticalScrollBar().setEnabled(true);
          				jScrollPane.getHorizontalScrollBar().setEnabled(false);
          				getContentPane().add(jScrollPane);
          			}
          
          			{
          				switchIDPanel = new JPanel();
          				FlowLayout switchIDPanelLayout = new FlowLayout(
          						FlowLayout.LEFT, 10, 17);
          				switchIDPanel.setLayout(switchIDPanelLayout);
          				jScrollPane.add(switchIDPanel);
          				switchIDPanel.setBounds(0, 31, 98, getGUIheight());
          				{
          					createSwitchIDLabel(getSwitchButtonCount());
          				}
          			}
          			{
          				brakePanel = new JPanel();
          				FlowLayout brakePanelLayout = new FlowLayout(FlowLayout.CENTER,
          						0, 18);
          				jScrollPane.add(brakePanel);
          				brakePanel.setBounds(287, 35, 133, getGUIheight());
          				brakePanel.setLayout(brakePanelLayout);
          				createBrakeButtons(getBrakeButtonCount());
          			}
          			{
          				textPanel = new JPanel();
          				jScrollPane.add(textPanel);
          				textPanel.setBounds(0, 0, 420, 35);
          				textPanel.setLayout(null);
          				{
          					switchLabel = new JLabel();
          					textPanel.add(switchLabel);
          					switchLabel.setText("Weichenstellungen ändern");
          					switchLabel.setBounds(28, 7, 182, 28);
          				}
          				{
          					brakeLabel = new JLabel();
          					textPanel.add(brakeLabel);
          					brakeLabel.setText("Zwangsbremsungen einleiten");
          					brakeLabel.setBounds(245, 7, 175, 28);
          					brakeLabel.setHorizontalTextPosition(SwingConstants.CENTER);
          					brakeLabel.setHorizontalAlignment(SwingConstants.CENTER);
          				}
          			}
          
          
          			{
          				switchPanel = new JPanel();
          				FlowLayout switchPanelLayout = new FlowLayout();
          				jScrollPane.add(switchPanel);
          				switchPanel.setBounds(98, 35, 189, getGUIheight());
          				switchPanel.setLayout(switchPanelLayout);
          				createSwitchButtons(getSwitchButtonCount());
          			}
          
          			pack();
          			// this.setSize(652, 454);
          			this.setSize(449, 570);
          		} catch (Exception e) {
          			e.printStackTrace();
          		}
          	}

          Comment


          • #6
            habe es hinbekommen. wer interesse an der lösung des problems hat, kann mir mailen.. /tobeclosed

            Comment

            Working...
            X