Swing Inhalte von Scroll-Panes werden nicht gezeichnet

Skrodde

Aktives Mitglied
Hallo zusammen,
ich bin gerade dabei, mich in Swing einzuarbeiten. Dazu haben ich ein TabbedPane angelegt, mit dem zwei Tabs angezeigt werden sollen. Auf jedem soll ein Scroll Pane zu sehen sein, auf dem dann wiederum ein oder zwei Buttons liegen. Allerdings werden weder das ScrollPane noch die Buttons angezeigt.
Was ist da wohl schiefgelaufen?
Vielen Dank, Gruß, Skrodde

Java:
package optGUI;

import java.awt.FlowLayout;
import java.awt.HeadlessException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import OptController.OptController;

public class OptGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private OptController optController;
	
	/**
	 * @throws HeadlessException
	 */
	public OptGUI(OptController optController) throws HeadlessException {
		//Name the frame "Optimization"
		super("Optimization");
		this.optController = optController;
		
		//Place the frame in the middle of the screen
		setLocationRelativeTo(null);
		
		//End the program via System.exit() when user clicks on (X) of the frame
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//User can not resize the frame
		//this.setResizable( false );
		
		//Create a Tabs-Object
		JTabbedPane tabs = new JTabbedPane();
		this.add(tabs);
		
		//ScrollPane for the Definition of Variables
		//------------------------------------------
		//Scroll Pane can be scrolled vertically, but not horizontally
			JScrollPane pane_1 = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			tabs.addTab("(1) Define Variables", null, pane_1, "Define all necessary variables that are used in the optimization problem.");
		//Continue Button that continues to Scroll Pane 2
			JButton btn_continue1 = new JButton("Continue");
			pane_1.add(btn_continue1);
		
			
		//ScrollPane for the Definition of Constraints
		//--------------------------------------------
		//Scroll Pane can be scrolled vertically, but not horizontally
			JScrollPane pane_2 = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			tabs.addTab("(2) Define Objective Function", null, pane_2, "Define the Type of the Optimization and the objective Function.");
		//Continue Button that continues to Scroll Pane 3
			JButton btn_continue2 = new JButton("Continue");
			pane_2.add(btn_continue2);
		//Backwards Button that goes back to Pane 1
			JButton btn_back2 = new JButton("Back");
			pane_2.add(btn_back2);
			
		JLabel l = new JLabel( "Lebe immer First-Class, sonst tun es deine Erben! Das ist eine alte Weisheit, beachte sie!" );
	    this.add( l );
		
		
		this.pack();
		this.setVisible(true);
		this.setLayout(new FlowLayout());
	}
	
}
 
Bin ich jetzt blöd oder fügst du dein Panel dem JFrame garnicht hinzu?

Java:
add(tabs);

ausserdem setzt man das Layout nicht nachdem man setVisible aufruft.
 
Zuletzt bearbeitet:
Hm, ich habe jetzt die letzten zwei Zeilen meines Codes getauscht, also
[JAVA=68]
this.setLayout(new FlowLayout());
this.setVisible(true);
[/code]
Allerdings sehe ich keine Veränderung, mein Fenster sieht gleich (schlecht) aus :autsch:

EDIT: Ich habe gerade gelesen, dass die add() Methode für den Container JScrollPane nicht geeignet ist, also habe ich mir Panels angelegt, diese in die Tabs eingelegt und dann die ScrollPanes mit
Code:
setViewPortView()
auf die Panels losgelassen. Das ganze sieht nun so aus wie unten im Code, allerdings sehe ich jetzt gar keine Tabs mehr.

Java:
package optGUI;

import java.awt.FlowLayout;
import java.awt.HeadlessException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import OptController.OptController;

public class OptGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private OptController optController;
	
	/**
	 * @throws HeadlessException
	 */
	public OptGUI(OptController optController) throws HeadlessException {
		//Name the frame "Optimization"
		super("Optimization");
		this.optController = optController;
		
		//Place the frame in the middle of the screen
		setLocationRelativeTo(null);
		
		//End the program via System.exit() when user clicks on (X) of the frame
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//User can not resize the frame
		//this.setResizable( false );
		
		//Create a Tabs-Object
		JTabbedPane tabs = new JTabbedPane();
		this.add(tabs);
		
		//ScrollPane for the Definition of Variables
		//------------------------------------------
		//Panel for the content of the first tab
			JPanel panel1 = new JPanel();
			tabs.addTab("(1) Define Variables", null, panel1, "Define all necessary variables that are used in the optimization problem.");
		//Scroll Pane can be scrolled vertically, but not horizontally
			JScrollPane scrollpane1 = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			scrollpane1.setViewportView(panel1);
		//Continue Button that continues to Scroll Pane 2
			JButton btn_continue1 = new JButton("Continue");
			panel1.add(btn_continue1);
		
			
		//ScrollPane for the Definition of Constraints
		//--------------------------------------------
		//Panel for the content of the second tab
			JPanel panel2 = new JPanel();
			tabs.addTab("(2) Define Objective Function", null, panel2, "Define the Type of the Optimization and the objective Function.");
		//Scroll Pane can be scrolled vertically, but not horizontally
			JScrollPane scrollpane2 = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			scrollpane2.setViewportView(panel2);
		//Continue Button that continues to Scroll Pane 3
			JButton btn_continue2 = new JButton("Continue");
			panel2.add(btn_continue2);
		//Backwards Button that goes back to Pane 1
			JButton btn_back2 = new JButton("Back");
			panel2.add(btn_back2);
		
		JLabel l = new JLabel( "Lebe immer First-Class, sonst tun es deine Erben! Das ist eine alte Weisheit, beachte sie!" );
	    this.add( l );
	    
		this.pack();
		this.setLayout(new FlowLayout());
		this.setVisible(true);
	}
	
}
 
Zuletzt bearbeitet:
Also, generell scheinst du dir die Grundlagen von Swing nicht richtig angeschaut zu haben. Dein Programm wird von oben nach unten gelesen. Da macht es wenig Sinn z.B. einen Button zu erzeugen, nachdem du ihn bereits deiner scrollPane übergibst. Des Weiteren, übergibst du den Button falsch.

Ich hab deinen "Horror" Code mal ein wenig strukturiert und dir ein Beispiel implementiert. Danach richtest du dich am besten für die weiteren Anpassungen.

Java:
import java.awt.FlowLayout;
import java.awt.HeadlessException;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
 

 
public class OptGUI extends JFrame {
 
    private static final long serialVersionUID = 1L;

    private JPanel panel1;
    private JTabbedPane tabs;
    private JScrollPane pane_1; 
    private JButton btn_continue1;
    /**
     * @throws HeadlessException
     */
    public OptGUI() throws HeadlessException {
        super("Optimization");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        tabs = new JTabbedPane();
        btn_continue1 = new JButton("Continue");
        pane_1 = new JScrollPane(btn_continue1);
        pane_1.setViewportView(btn_continue1);
        tabs.addTab("(1) Define Variables", null, pane_1, "Define all necessary variables that are used in the optimization problem.");
      
        this.add(tabs);
        this.pack();
        this.setVisible(true);
        this.setLayout(new FlowLayout());
    }
    
    public static void main(String[]rgs){
    	new OptGUI();
    }
}

[EDIT]P.S Ich habe deinen Konstruktor geändert, da ich deine Controllerklasse nicht habe, das musst du natürlich alles wieder ändern. Auch den Inhalt der Tabs etc.[/EDIT]
 
@c_sidi90
warum ein Layout setzen nachdem alles angezeigt wird bzw wenn nur ein element hinzugefuegt wird.

weiterhin ist es kein problem eine Component unfertig zu erzeugen, einem Container zu geben und dann weiter zu initialisieren. Solange noch nix gezeichnet wurde ists das kein Problem.
 
Ich hab deinen "Horror" Code mal ein wenig strukturiert und dir ein Beispiel implementiert. Danach richtest du dich am besten für die weiteren Anpassungen.

Vielen Dank für die Hilfe und entschuldige den "Horror". Ich werde mich wohl doch noch etwas mit Swing beschäftigen :rtfm:, bevor ich hier ernsthafte Versuche mache. Dieses Beispiel klappt nun aber wunderbar mit dem unten genannten Code und dafür vielen Dank an c_sidi90 :toll:

Java:
package optGUI;

import java.awt.FlowLayout;
import java.awt.HeadlessException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import OptController.OptController;

public class OptGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private OptController optController;
	private JPanel panel1,panel2;
    private JTabbedPane tabs;
    private JScrollPane scrollpane1, scrollpane2; 
    private JButton btn_continue1,btn_continue2,btn_back2;
    
	/**
	 * @throws HeadlessException
	 */
	public OptGUI(OptController optController) throws HeadlessException {
		//Name the frame "Optimization"
		super("Optimization");
		this.optController = optController;
		
		//Place the frame in the middle of the screen
		setLocationRelativeTo(null);
		
		//End the program via System.exit() when user clicks on (X) of the frame
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Do not let the user resize the frame
		this.setResizable( false );
		
		tabs = new JTabbedPane();
        
		btn_continue1 = new JButton("Continue");
        scrollpane1 = new JScrollPane();
        panel1 = new JPanel();
        panel1.add(btn_continue1);
        scrollpane1.setViewportView(panel1);
        tabs.addTab("(1) Variables", null, scrollpane1, "Define all necessary variables that are used in the optimization problem.");
      
        btn_continue2 = new JButton("Continue");
        btn_back2 = new JButton("Back");
        scrollpane2 = new JScrollPane();
        panel2 = new JPanel();
        panel2.add(btn_continue2);
        panel2.add(btn_back2);
        scrollpane2.setViewportView(panel2);
        tabs.addTab("(2) Objective Function", null, scrollpane2, "Define Objective Function and Optimization Type");
        
        this.add(tabs);
        this.pack();
        this.setVisible(true);
        this.setLayout(new FlowLayout());
	}
	
}
 
@c_sidi90
warum ein Layout setzen nachdem alles angezeigt wird bzw wenn nur ein element hinzugefuegt wird.

Hab ich vergessen aus seinem Code zu nehmen, das hab ich in meinem Post ja bereits erwähnt das das kein Sinn macht. Das es nichts ausmacht die Komponenten zu übergeben ohne zu initialisieren ist richtig, machen tut es trotzdem niemand, zumindest keiner der seinen Code übersichtlich halten will.
 

Zurück
Oben