Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi All, I do have class, which i

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi All,

I do have class, which is given below; Presently the size of the dialog box is hard coded(setSize(734, 600));

Could anyone let me know how I get the size dynamic(1/4 of the entire frame).

=============================================
import java.awt.Container;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class FinalValidationDialog extends JDialog
{
private JTextArea myText = new JTextArea();

private JScrollPane myScrollPane;

private Container myParentContainer;

public FinalValidationDialog( Container aParent, String aText )
{
super( (JFrame) null, true );

myText.setText( aText );

myText.setEditable( false );

myText.setLineWrap( true );

myText.setWrapStyleWord( true );

init();

this.setLocationRelativeTo( aParent );
}

private void init()
{

// determine the size of the dialog
Window win = SwingUtilities.windowForComponent(myParentContainer);

System.out.println("SwingUtilities.windowForComponent returned "+
win.getClass().getName());

myScrollPane = new JScrollPane(myText);

this.setTitle("Final Validation");

this.getContentPane().add(myScrollPane, "North");

JPanel p = new JPanel();
JButton ok = new JButton("Ok");
p.add(ok);

getContentPane().add(p, "South");


ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
setVisible(false);
}
});

setSize(734, 600);

}
}
 
Create a variable for the parent frame. JFrame myParentFrame,
OR have you done this already with Container myParentContainer; If your parent frame is a JFrame you should pass JFrame into the constructor not Container.
Pass the parent frame as argument to the constuctor. Which I think that you have done already. From here on I will consider your parent frame is a JFrame.
What you need to do is take your constructor and inside set variable myParentFrame reference it to the object comming in throught the constructor. that is;

public FinalValidationDialog( JFrame aParent, String aText )

myParentContainer = aParent;

now in your init method you can access the parent frame
...init()
Dimension d = myParentContainer.getSize();
setSize(d.width/4, d.height/4);
This will set it to 1/4 of the size.

Have a look in the API for setSize in component and getSize and then Dimension object.

Hope I helped

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top