Manchmal werden die Inhalte des JFrames angezeigt, machmal nicht...

Ravebaby

Erfahrenes Mitglied
Hallo zusammen,

ich habe einen MainView der von JFrame erbt.
Um ihn nach dem Befüllen anzuzeigen rufe ich "this.setVisible(true);" auf.
Nun habe ich das Problem, dass er manchmal die Inhalte sofort anzeigt und manchmal erst, wenn ich die Fenstergröße verändere.
Wie kann ich das verhindern?
Ich habe versucht mittels "try{Thread.sleep(1000);}catch(Exception e){}" abzuwarten bis alles initialisiert ist und dann erst "this.setVisible(true);" aufzurufen. Aber das hat leider auch keine Wirkung.

Vielen Dank schon einmal für jede Hilfe.
Grüße Rave
 
Moin!
Mir fallen da sopntan 2 Sachen ein:

1. Du fügst Komponenten dem JFrame hinzu, nachdem this.visible(true) aufgerufen wurde
2. Du hast die paint()-Methode überschrieben, rufst aber nicht super.paint() - auf

sollte beides nicht zutreffen, poste doch mal dienen Code hier ;)
 
Es trifft leider beides nicht zu.
Mein Code:

Code:
package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.jmi.xmi.MalformedXMIException;
import javax.jmi.xmi.XmiReader;
import javax.jmi.xmi.XmiWriter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;

import model.GlobalModel;

import org.moflon.xmi.XMIReaderImpl;
import org.moflon.xmi.XMIWriterImpl;

import controller.GlobalController;

/**
 * The Main view. This handles the events fired by the Main view and creates all subviews 
 * @author ahaug31
 * @modified 2007-01-17 ahaug: setGlobalModel added to set a new model after loading a DXL file 
 * @modified 2007-01-17 ahaug: mew startup mechanism added 
 */

public class MainView extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;
	
	//create the tabs
	private	JTabbedPane tabbedPane;
	private	TypesView typesPanel;
	private	AttributesView attributesPanel;
	private	ViewsView viewsPanel;
	private	ModuleView modulePanel;
	
	private GlobalModel model;
	private GlobalController controller;

	// set the main panel properties
	public MainView(GlobalController globalController)
	{
		controller = globalController;
		controller.setMainView(this);
		// get the model created during application startup from the controller
		model = controller.getModel();
		
		setTitle("DOORS Module Manager");
		setSize(1000, 700);
		setBackground(Color.gray);
		
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new BorderLayout());
		getContentPane().add(topPanel);
		
		addMenuBar();
		
		//create the tabs
		typesPanel = new gui.TypesView(model);
		attributesPanel = new gui.AttributesView(model);
		viewsPanel = new gui.ViewsView(model);
		modulePanel = new gui.ModuleView(model);
		
		// Create a tabbed pane
		tabbedPane = new JTabbedPane();
		tabbedPane.addTab("Modules", modulePanel);
		tabbedPane.addTab("AttributeTypes", typesPanel);
		tabbedPane.addTab("Attributes",attributesPanel);
		tabbedPane.addTab("Views", viewsPanel);
		topPanel.add(tabbedPane, BorderLayout.CENTER);
	}
	
	
	/**
   * if a new DXL file was opened there is a need to set the newly created GlobalModel 
   * @param globalModel the newly created GlobalModel
   */
	public void setNewGlobalModel(GlobalModel globalModel){
		model = globalModel;
		typesPanel.setGlobalModel(model);
		attributesPanel.setGlobalModel(model);
		viewsPanel.setGlobalModel(model);
		modulePanel.setGlobalModel(model);
	}
	
	
	private void addMenuBar(){
		// create and set the menu bar
		JMenuBar menuBar = new JMenuBar();
    menuBar.setOpaque(true);
    menuBar.setPreferredSize(new Dimension(200, 20));
		
    // add the file menu
		setJMenuBar(menuBar);
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);
    
    // add the new menu item
    JMenuItem newMenuItem = new JMenuItem("New",KeyEvent.VK_N);
    newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    // add the action handling
    newMenuItem.addActionListener(this);
    newMenuItem.setActionCommand("New");
    fileMenu.add(newMenuItem);
    
    // add the load menu item
    JMenuItem loadMenuItem = new JMenuItem("Open",KeyEvent.VK_O);
    loadMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    // add the action handling
    loadMenuItem.addActionListener(this);
    loadMenuItem.setActionCommand("Open");
    fileMenu.add(loadMenuItem);
    
    // add the save menu item
    JMenuItem saveMenuItem = new JMenuItem("Save",KeyEvent.VK_S);
    saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
    // add the action handling
    saveMenuItem.addActionListener(this);
    saveMenuItem.setActionCommand("Save");
    fileMenu.add(saveMenuItem);
    fileMenu.addSeparator();
    
    // add the sync menu item
    JMenuItem syncMenuItem = new JMenuItem("Sync",KeyEvent.VK_Y);
    syncMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
    // add the action handling
    syncMenuItem.addActionListener(this);
    syncMenuItem.setActionCommand("Sync");
    fileMenu.add(syncMenuItem);
    fileMenu.addSeparator();
    
    // add the exit menu item
    JMenuItem exitMenuItem = new JMenuItem("Exit",KeyEvent.VK_X);
    exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
    //  add the action handling
    exitMenuItem.addActionListener(this);
    exitMenuItem.setActionCommand("Exit");
    fileMenu.add(exitMenuItem);
    
    // add the help menu
    setJMenuBar(menuBar);
    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic(KeyEvent.VK_H);
    menuBar.add(helpMenu);
    
    // add the about menu item
    JMenuItem aboutMenuItem = new JMenuItem("About",KeyEvent.VK_A);
    aboutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
    helpMenu.add(aboutMenuItem);
    
    //set this view visible
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    try{Thread.sleep(1000);}catch(Exception e){} //wait shortly, enabling all objects being initialized (asynchronous events)
		this.setVisible(true);
	}
	
	
	private void openXMI(String filename){
		//Test-Code
		XmiReader xmiReader = new XMIReaderImpl();
		try {
			// if a new DXL file was opened there is a need to create a new model to display
			controller.createNewGlobalModel();
			ArrayList list = (ArrayList)xmiReader.read(filename, model);
			attributesPanel.initializeViewAfterFileLoading();
			typesPanel.initializeViewAfterFileLoading();
			//controller.setNewGlobalModel(model);
		} catch (MalformedXMIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	
	private void writeXMI(String filename){
		XmiWriter xmiWriter = new XMIWriterImpl();
		try {
			xmiWriter.write(new FileOutputStream(filename), model,
					"2.1");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void actionPerformed(ActionEvent e){
		String buttonPressed = e.getActionCommand();
		
		// Create a file chooser
    JFileChooser fc = new JFileChooser();
    
    if (buttonPressed == "New"){
			// set the folder to start in
			System.out.println("Create New Module");
		}
    
		if (buttonPressed == "Open"){
			// set the folder to start in
			File currFile = new File("C:\\AHaug\\XML Lager\\");
			fc.setCurrentDirectory(currFile);
			// open the "file open" dialog
			int returnVal = fc.showOpenDialog(MainView.this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        //This is where a real application would open the file.
        openXMI(file.getPath());
			} else {
        System.out.println("Open command cancelled by user." + "\n");
			}
		}
		
		if (buttonPressed == "Save"){
			//set the folder to start in
			File currFile = new File("C:\\AHaug\\XML Lager\\");
			fc.setCurrentDirectory(currFile);
			int returnVal = fc.showSaveDialog(MainView.this);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
          File file = fc.getSelectedFile();
          writeXMI(file.getPath());	
      } else {
      	System.out.println("Save command cancelled by user." + "\n");
      }		
		}
		
		if (buttonPressed == "Sync"){
			
		}
		if (buttonPressed == "Exit"){
			// exit application
			this.dispose();
		}
	}
}
 
Also...
1. wäre richtig gewesen ;)
Du fügst Komponenten dem JFrame hinzu, nachdem this.visible(true) aufgerufen wurde.
Wo wird this.setVisible() aufgerufen? In der Methode addMenuBar().
Danach fügst du aber im Konstruktor noch Tabs/TabbedPane hinzu. Pack das setVisible() hinter die letzte anweisung im Konstruktor.
 
Zurück