Swing OpenOffice IFrame flackert beim ändern der Größe

Tom_

Aktives Mitglied
Hallo! Zusammen,

habe schon einiges hier gelesen über das Flackern nur hat es mich eher verwirrt als mir geholfen.
Die angebotenen Lösungen beziehen sich meist auf Spiele einerseits und andererseite taucht immer wieder der Begriff doubleBuffering auf ( was Swing aber von hausaus machen sollte).

Im grunde habe ich nichts anderes gemacht als einmal ganz einfach nach rezept OpenOffice eingebunden.

Funktioniert auch wunderbar nur wenn man die Fenstergröße mit der Maus ändert, dann flackerts.

Kann mir bitte jemand einen link oder code posten?

Danke!
lg
Tom

Code:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;

import ag.ion.bion.officelayer.NativeView;
import ag.ion.bion.officelayer.application.IApplicationAssistant;
import ag.ion.bion.officelayer.application.ILazyApplicationInfo;
import ag.ion.bion.officelayer.application.IOfficeApplication;
import ag.ion.bion.officelayer.application.OfficeApplicationException;
import ag.ion.bion.officelayer.application.OfficeApplicationRuntime;
import ag.ion.bion.officelayer.desktop.GlobalCommands;
import ag.ion.bion.officelayer.desktop.IFrame;
import ag.ion.bion.officelayer.document.DocumentDescriptor;
import ag.ion.bion.officelayer.document.IDocument;
import ag.ion.bion.officelayer.internal.application.ApplicationAssistant;
import ag.ion.bion.officelayer.text.ITextDocument;

public class SimpleSwingApp extends Frame {

	private static final String  CELL_NAME         = "C1";
	private static final String  SHEET_NAME        = "Tabelle1"; //or Table1

	public static IOfficeApplication   officeApplication = null;
	public static IFrame               officeFrame       = null;
	private ITextDocument		 document          = null;
	private JPanel               noaPanel          = null;

	public SimpleSwingApp() {
		super(SimpleSwingApp.class.getName());
		JPopupMenu.setDefaultLightWeightPopupEnabled(false);
		JPanel content = new JPanel(new GridBagLayout());
		setLayout(new BorderLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		
		JButton buttonNewDoc = new JButton("Open new Document/Close old one");
	
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.1;
		c.gridx = 2;
		c.gridy = 0;
		content.add(buttonNewDoc, c);
		
		buttonNewDoc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {

				officeApplication.getDocumentService().constructNewDocument(officeFrame,
							IDocument.WRITER,
							DocumentDescriptor.DEFAULT);
					}
				catch (Exception exception) {
					exception.printStackTrace();
				}
			}
		});

		noaPanel = new JPanel();
		c.weighty = 1.0;
		c.anchor = GridBagConstraints.PAGE_END;
		c.insets = new Insets(10, 0, 0, 0);
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 6;
		content.add(noaPanel, c);
		setSize(1024, 800);
//		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				try {
					if (officeApplication != null) {
						officeApplication.deactivate();
						officeApplication.dispose();
					}
				} catch (OfficeApplicationException e) {
					e.printStackTrace();
				}
				dispose();
				System.exit(0);
			}
		});
		
		JSplitPane splitPane = new JSplitPane();
		
		
		JPanel treePanel = new JPanel(new BorderLayout());
		
		JTree tree = new JTree();
		treePanel.add(new JScrollPane(tree));
		
		splitPane.setLeftComponent(treePanel);
		splitPane.setRightComponent(content);
		
		splitPane.setDividerLocation(0.4);
		
		add(splitPane, BorderLayout.CENTER);
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent windowEvent) {
				try {
					if (document != null)
						document.close();
					document = null;
					/*if (officeApplication != null) {
            officeApplication.deactivate();
            officeApplication.dispose();
            officeApplication = null;
          }*/
				}
				catch (Exception exception) {
					//do not consume
				}
			}
		});
		
		setVisible(true);
		
		fillNOAPanel();
		
		officeFrame.disableDispatch(GlobalCommands.CLOSE_DOCUMENT);
		officeFrame.disableDispatch(GlobalCommands.QUIT_APPLICATION);
		officeFrame.disableDispatch(GlobalCommands.OPEN_DOCUMENT);
		officeFrame.disableDispatch(GlobalCommands.NEW_DOCUMENT);
		officeFrame.disableDispatch(GlobalCommands.NEW_MENU);
		officeFrame.disableDispatch(GlobalCommands.CLOSE_WINDOW);
	    
		officeFrame.updateDispatches();

		
/*		try {
			officeFrame.getLayoutManager().hideElement("private:resource/menubar/menubar");
		} catch (NOAException e1) {
			e1.printStackTrace();
		}
	*/
	}

	private void fillNOAPanel() {
		if (noaPanel != null) {
			try {
				if (officeApplication == null)
					officeApplication = startOOO();
				officeFrame = constructOOOFrame(officeApplication, noaPanel);
			

				officeApplication.getDocumentService().constructNewDocument(officeFrame,
						IDocument.WRITER,
						DocumentDescriptor.DEFAULT);
				
				noaPanel.setVisible(true);
			}
			catch (Throwable throwable) {
				noaPanel.add(new JLabel("An error occured while creating the NOA panel: " + throwable.getMessage()));
				throwable.printStackTrace();
			}
		}
	}

	private IOfficeApplication startOOO() throws Throwable {
		IApplicationAssistant applicationAssistant = new ApplicationAssistant(new File("lib").getAbsolutePath());
		ILazyApplicationInfo[] appInfos = applicationAssistant.getLocalApplications();
		if (appInfos.length < 1)
			throw new Throwable("No OpenOffice.org Application found.");
		HashMap configuration = new HashMap();
		configuration.put(IOfficeApplication.APPLICATION_HOME_KEY, appInfos[0].getHome());
		configuration.put(IOfficeApplication.APPLICATION_TYPE_KEY, IOfficeApplication.LOCAL_APPLICATION);
		IOfficeApplication officeAplication = OfficeApplicationRuntime.getApplication(configuration);

		officeAplication.setConfiguration(configuration);
		officeAplication.activate();
		return officeAplication;
	}

	private IFrame constructOOOFrame(IOfficeApplication officeApplication, final Container parent)
	throws Throwable {
		final NativeView nativeView = new NativeView(new File("lib").getAbsolutePath());
		parent.add(nativeView);
		parent.addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
				nativeView.setPreferredSize(new Dimension(e.getComponent().getWidth() - 5, e.getComponent().getHeight() - 5));
				parent.getLayout().layoutContainer(parent);
			}
		});
		nativeView.setPreferredSize(new Dimension(parent.getWidth() - 5, parent.getHeight() - 5));
		parent.getLayout().layoutContainer(parent);
		IFrame officeFrame = officeApplication.getDesktopService().constructNewOfficeFrame(nativeView);
		parent.validate();
		return officeFrame;
	}

}
 
Zuletzt bearbeitet:
1. Es gibt etwas, dass nennt sich Edit-Funktion (hat auch diverse Vorteile, wenn man selbst nicht der erste ist, der auf sein Problem antwortet)
2. relevanten Sourcecode
 
Double Buffering und dergleichen wird dir dabei nicht helfen. Denke auch nicht das du das überhaupt vermeiden kannst, schließlich embeddest du eine Standalone Applikation die in einem anderen Prozess läuft und in einer ganz anderen Programmiersprache geschrieben wurde in eine Swing Umgebung, da muss man ein paar Abstriche hinnehmen.
 
Der Code sieht ok aus. Mit dem Flackern wirst du wie bereits gesagt vermutlich einfach leben müssen.
Übrigens: Throwable fängt man nicht, und man wirft es auch nicht!
 

Zurück
Oben