dcsimg
Painting problem with JDesktopPane and JFileChooser
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Craig_Anderson
Posted On:   Wednesday, December 5, 2001 02:39 PM

I'm experiencing a paint problem related to JFileChooser when using JDesktopPane. My app is an MDI. I'm using JDK 1.3 on a Windows 2000 platform. The problem I'm having is twofold: I need to have a toolbar, since that's the common thing to do in a app. To have a toolbar you have to place it in one of the border layout areas, usually North, which I do. To get an MDI interface, I have added a JDesktopPane to the center of the border layout (but south would do just as well). Now call JFileChooser.showOpenDialog() or showCloseDialog(). Close the dialog after it opens. If you now stretch the window, the new area of the window is not shown in the same color as the rest of the client area of the window. In other words, it doesn't paint properly as it    More>>

I'm experiencing a paint problem related to JFileChooser when using JDesktopPane.

My app is an MDI. I'm using JDK 1.3 on a Windows 2000 platform. The problem I'm having is twofold:
I need to have a toolbar, since that's the common thing to do in a app. To have a toolbar you have to place it in one of the border layout areas, usually North, which I do. To get an MDI interface, I have added a JDesktopPane to the center of the border layout (but south would do just as well). Now call JFileChooser.showOpenDialog() or showCloseDialog(). Close the dialog after it opens. If you now stretch the window, the new area of the window is not shown in the same color as the rest of the client area of the window. In other words, it doesn't paint properly as it should.

The other aspect of this problem is that an alternate approach is to set the content pane of the JFrame to a new JDesktopPane. This works great, and after you call the JFileChooser, and stretch the window, there is no paint problem at all! This would be the solution except that if you do this, then you can't have a toolbar, since JDesktopPane doesn't have a layout. If you set a layout in the JDesktopPane to a BorderLayout, then you can have a toolbar, but the JInternalFrame windows that you add to the desktop will exhibit strange sizing behavior, not at all what one would expect to see them do.

To sum, if you want a toolbar, then it seems to have to add the JDesktopPane to the center of the content pane. But if you call JFileChooser, then you have paint problems. If instead, you set the content pane to JDesktopPane, then you can't have a toolbar. It seems to be a catch-22. Can you help?

Below is the Java Tutorial code for using JDesktopPane that has been modified to include the ability to call the open dialog to illustrate the problem. Compile it, run it, open the dialog, close it, then stretch the window to see the problem.



--- file: InternalFrameDemo.java --
import javax.swing.JInternalFrame;

import javax.swing.JDesktopPane;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.JMenuBar;

import javax.swing.JFrame;

import javax.swing.JFileChooser;

import java.awt.*;



import java.awt.event.*;

import java.awt.*;



public class InternalFrameDemo extends JFrame {

JDesktopPane desktop;



public InternalFrameDemo() {

super("InternalFrameDemo");



//Make the big window be indented 50 pixels from each edge

//of the screen.

int inset = 50;

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

setBounds(inset, inset,
screenSize.width - inset*2,
screenSize.height-inset*2);



//Quit this app when the big window closes.

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});



//Set up the GUI.

desktop = new JDesktopPane(); //a specialized layered pane

createFrame(); //Create first window

getContentPane().add (desktop, BorderLayout.CENTER);

// setContentPane(desktop);

setJMenuBar(createMenuBar());



//Make dragging faster:

desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

// desktop.putClientProperty("JDesktopPane.dragMode", "outline");

}



protected JMenuBar createMenuBar() {

JMenuBar menuBar = new JMenuBar();



JMenu menu = new JMenu("Document");

menu.setMnemonic(KeyEvent.VK_D);

JMenuItem menuItem = new JMenuItem("New");

menuItem.setMnemonic(KeyEvent.VK_N);

menuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

createFrame();

}

});

menu.add(menuItem);

menuBar.add(menu);



menu = new JMenu("Open");

menu.setMnemonic(KeyEvent.VK_O);

menuItem = new JMenuItem("New");

menuItem.setMnemonic(KeyEvent.VK_W);

menuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

openFileDialog();

}

});

menu.add(menuItem);

menuBar.add(menu);



return menuBar;

}



protected void createFrame() {

MyInternalFrame frame = new MyInternalFrame();

frame.setVisible(true); //necessary as of kestrel

desktop.add(frame);

try {

frame.setSelected(true);

} catch (java.beans.PropertyVetoException e) {}

}





public void openFileDialog()

{

JFileChooser openChooser = new JFileChooser();

getContentPane().add (openChooser);



int returnValue = openChooser.showOpenDialog (this);

}



public static void main(String[] args) {

InternalFrameDemo frame = new InternalFrameDemo();

frame.setVisible(true);

}

}





class GPFTextFilter extends javax.swing.filechooser.FileFilter
{

public boolean accept(java.io.File f)

{

boolean accept = f.isDirectory();



if (!accept)

{

String suffix = getSuffix (f);



if (suffix != null)

{

accept = suffix.equals ("gpf");

}

}



return (accept);

}



public String getDescription()

{

return ("GPF Files (*.gpf)");

}



private String getSuffix(java.io.File f)

{

String s = f.getPath(),
suffix = null;



int i = s.lastIndexOf (".");



if (i > 0 && i < s.length() - 1)

{

suffix = s.substring (i + 1).toLowerCase();

}



return (suffix);

}

}

--- end of InternalFrameDemo.java ---
--- File: MyInternalFrame.java ---


import javax.swing.JInternalFrame;



import java.awt.event.*;

import java.awt.*;



public class MyInternalFrame extends JInternalFrame {

static int openFrameCount = 0;

static final int xOffset = 30, yOffset = 30;



public MyInternalFrame() {

super("Document #" + (++openFrameCount),

true, //resizable

true, //closable

true, //maximizable

true);//iconifiable



//...Create the GUI and put it in the window...



//...Then set the window size or call pack...

setSize(300,300);



//Set the window's location.

setLocation(xOffset*openFrameCount, yOffset*openFrameCount);

}

}



--- end of MyInternalFrame.java ---

   <<Less
About | Sitemap | Contact