JDialog: Repaint does not work as wanted when called a second time

Hi,

I have created a JDialog with a More/Less Details button which, when invoked, shows or not shows a JScrollPane with a JEditorPane and resizes the dialog window to make space for that extra component or to take away that space.

It works fine thefirst time I invoke the JDialog (see main method). But if I invoke it a second time and want to see the details the details JScrollPane is not fully shown.

I have experimented with repaint and revalidate on different panels but to no avail.

I am quite new to Swing, what am I misunderstanding? Is my Close-button not tidying up properly?

import javax.swing.*;

import javax.swing.border.*;

import java.awt.event.*;

import javax.swing.event.*;

import java.awt.*;

class Testerextends JDialog{

privatestaticint totalWidth = 200;

privatestaticint totalHeight = 100;

privatestaticint detailsPanelHeight = 200;

public Tester(){

super();

}

publicstaticint createAndShowGUI(){

Tester td =new Tester();

return td.doGUI();

}

publicint doGUI(){

//Create and set up the window.

setPreferredSize(new Dimension(totalWidth, totalHeight+detailsPanelHeight));

setModal(true);

setResizable(false);

setTitle("Tester");

setAlwaysOnTop(true);

final Container mainPane = getContentPane();

mainPane.setLayout(new FlowLayout());

// *** Panel for details

final JEditorPane detailsText =new JEditorPane("text/html","<HTML>Some <i>html</i>.</HTML>");

final JScrollPane detailsPanel =new JScrollPane(detailsText);

detailsPanel.setBorder(new TitledBorder("Details"));

detailsText.setEditable(false);

detailsPanel.setPreferredSize(new Dimension(totalWidth-100, detailsPanelHeight));

detailsPanel.setVisible(false);

// *** Panel for Info

final JToggleButton detailsBtn =new JToggleButton("More Details");

detailsBtn.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

if (detailsBtn.isSelected()){

detailsBtn.setText("Less Details");

detailsBtn.repaint();

detailsPanel.setVisible(true);

setBounds(getX(), getY(),

getWidth(), getHeight()+detailsPanelHeight);

// Also tried detailsPanel.revalidate(), mainPane.revalidate/repaint()

// to no avail

detailsPanel.repaint();

}

else{

detailsBtn.setText("More Details");

detailsBtn.repaint();

detailsPanel.setVisible(false);

setBounds(getX(), getY(),

getWidth(), getHeight()-detailsPanelHeight);

detailsPanel.repaint();

}

}

});

// *** Panel for buttons

JPanel btnPanel =new JPanel();

mainPane.add(btnPanel);

mainPane.add(detailsPanel);

// Allow Button

JButton allowBtn =new JButton("Close");

allowBtn.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

setVisible(false);

dispose();

}

});

btnPanel.add(allowBtn);

btnPanel.add(detailsBtn);

//Display the window.

pack();

setBounds(getX(), getY(), totalWidth, totalHeight);

setVisible(true);

return 1;

}

publicstaticvoid main(String[] args){

javax.swing.SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

// Run once and you can see details in nice window

System.err.println(Tester.createAndShowGUI());

// Run twice and switching on details shows an empty space

System.err.println(Tester.createAndShowGUI());

}

});

}

}

[6951 byte] By [almhe03a] at [2007-10-3 8:26:24]
# 1

Replace your action preformed with this:

public void actionPerformed(ActionEvent e) {

if (detailsBtn.isSelected()) {

detailsBtn.setText("Less Details");

setSize(new Dimension(

getWidth(), getHeight()+detailsPanelHeight));

detailsPanel.setVisible(true);

}

else {

detailsBtn.setText("More Details");

detailsPanel.setVisible(false);

setSize(new Dimension(

getWidth(), getHeight()-detailsPanelHeight));

}

validate();

}

zadoka at 2007-7-15 3:32:47 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks a million.The small validate() at the end did it.
almhe03a at 2007-7-15 3:32:47 > top of Java-index,Desktop,Core GUI APIs...