Samooramad
asked on
desktop hieght
Hi experts,
I have a desktop with internal frames, a menu bar and a toolbar. The internal frame hieght is incremented every time a button is pressed and a label added to it but I want it to stop growing in hieght once it reaches the limits of the desktop. How is this done?
thanks
I have a desktop with internal frames, a menu bar and a toolbar. The internal frame hieght is incremented every time a button is pressed and a label added to it but I want it to stop growing in hieght once it reaches the limits of the desktop. How is this done?
thanks
Can you not use the getHeight() method?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
gethieght of what?
ASKER
oh ok.. I will try that
ASKER
no it didnt work. It keeps going down and disapears behind the toolbar and keeps going.
could you include a working example?
could you include a working example?
Can you post some sampel code (a small compilable example) so I can try that and see what's going on?
can u post ur code??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sorry my reply comes late,I was having PC problems before..sure I will post it
ASKER
Im still having problems.. I will post the code.
Javatm, your code was great but I still dont know how to apply it to the desktop boundry
Javatm, your code was great but I still dont know how to apply it to the desktop boundry
ASKER
package hospital;
import java.util.ArrayList;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.*;
public class m extends JFrame {
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel bottom = new JPanel();
JScrollPane sp = new JScrollPane();
JButton d =new JButton("Open List");
JPanel contentPane;
JMenu File = new JMenu();
JMenuItem FileExit = new JMenuItem("Exit");
Border border1;
Border border2;
//layouts
BorderLayout borderLayout1 = new BorderLayout();
BorderLayout borderLayout2 = new BorderLayout();
BorderLayout borderLayout3 = new BorderLayout();
JDesktopPane desktop = new JDesktopPane();
JMenuBar jMenuBar1 = new JMenuBar();
JToolBar jToolBar1 = new JToolBar();
//toolBar buttons
JButton help=new JButton("Help");
//panels
JPanel eastPanel = new JPanel();
JPanel centerPanel = new JPanel();
//layouts
BorderLayout docborder = new BorderLayout();
//Interface
private JButton addName;
private JPanel bigpanel;
private JPanel southPanel;
private JPanel leftpanel;
private JScrollPane theScrollPane;
private JTextField theTextField;
doctor[] allDoctors;//original
int nrOfDsAdded = 0;//original
public boolean frameOpened = false;
////////////////////////// ////////// ////////// /
///Constructor
public m() {
enableEvents(AWTEvent.WIND OW_EVENT_M ASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
////////////////////////// ////////// ////////// /////////
///Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
border1 = BorderFactory.createLineBo rder(Color .gray,2);
border2 = BorderFactory.createLineBo rder(Color .black,2);
this.setSize(new Dimension(500, 400));
this.setDefaultCloseOperat ion(3);
////////////////////////// ////////// ////////// /
allDoctors = new doctor[100];
////////////////////////// ////////// ////////// /
//set up menu items and their actions
JPopupMenu.setDefaultLight WeightPopu pEnabled(f alse);
addWindowListener(new java.awt.event.WindowAdapt er() {
public void windowClosing(java.awt.eve nt.WindowE vent evt) {
exitForm(evt);
}
});
File.setText("File");
File.setMnemonic(KeyEvent. VK_F);
FileExit.setAccelerator(Ke yStroke.ge tKeyStroke (KeyEvent. VK_E, ActionEvent.CTRL_MASK));
FileExit.addActionListener (new ActionListener() {
public void actionPerformed(ActionEven t e) {
FileExit_actionPerformed(e );
}
});
////////////////////////// ////////// //////
//toolbar buttons
d.addActionListener( new ActionListener()
{ public void actionPerformed(ActionEven t e) {
d_actionPerformed(e);
}
});
////////////////////////// ///////
contentPane.setLayout(bord erLayout1) ;
desktop.setLayout(borderLa yout2);
//add components of menu, desktop and toolbar
File.add(FileExit);
jMenuBar1.add(File);
jToolBar1.add(d);
setJMenuBar(jMenuBar1);
desktop.add(eastPanel, BorderLayout.EAST);
desktop.add(centerPanel, BorderLayout.CENTER);
contentPane.add(desktop, BorderLayout.CENTER);
contentPane.add(jToolBar1, BorderLayout.SOUTH);
}//jb init
////////////////////////// ////////// ///////
private void exitForm(WindowEvent evt)
{
System.exit(0);
}//end
////////////////////////// ////////// /////////
//File | Exit action performed
public void FileExit_actionPerformed(A ctionEvent e)
{
System.exit(0);
}//end
////////////////////////// ////////// /
public void d_actionPerformed(ActionEv ent e)
{
JInternalFrame dFrame = new JInternalFrame(" My List",false,false,true,tru e);
if (frameOpened == false) {
centerPanel.add(dFrame, BorderLayout.CENTER);
dFrame.setBackground(Color .white);
dFrame.setVisible(true);
addFrameLayout(dFrame);
dFrame.pack();
frameOpened = true;
}//if
else
dFrame.moveToFront();
}
////////////////////////// //////////
protected void processWindowEvent(WindowE vent e) {
super.processWindowEvent(e );
if (e.getID() == WindowEvent.WINDOW_CLOSING ) {
FileExit_actionPerformed(n ull);
}
}
////////////////////////// ////////
private void addFrameLayout(final JInternalFrame f)
{
theScrollPane = new JScrollPane();
bigpanel = new JPanel();//carrier
leftpanel = new JPanel();//for names
southPanel = new JPanel();//for button
theTextField = new JTextField();
addName = new JButton();
leftpanel.setLayout(new GridBagLayout());
bigpanel.setLayout(new FlowLayout(FlowLayout.LEFT ));
bigpanel.add(leftpanel);
theScrollPane.setViewportV iew(bigpan el);
f.getContentPane().add(the ScrollPane ,BorderLay out.CENTER );
theTextField.setPreferredS ize(new Dimension(150, 20));
theTextField.addActionList ener(new ActionListener()
{
public void actionPerformed(ActionEven t evt) {
addDActionPerformed(evt);
if(f.getHeight() < (desktop.getHeight()-jTool Bar1.getHe ight()-jMe nuBar1.get Height()-2 00))////th is is what I tried but did not work!!!!!
{
f.setSize(f.getWidth(), f.getHeight() + 12);
}
}
});
addName.setText("Add name");
addName.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEven t evt) {
addDActionPerformed(evt);
int h =desktop.getHeight();
System.out.println("deskto p hieght"+h);
int t = jToolBar1.getHeight();
System.out.println("toolba r hieght"+t);
int m = jMenuBar1.getHeight();
System.out.println("menuba r hieght"+m);
f.setSize(f.getWidth(),f.g etHeight() + 12);//it should be applied here too!!!!
}
});
southPanel.add(theTextFiel d);
southPanel.add(addName);
f.setFrameIcon(null);
f.getContentPane().add(sou thPanel,Bo rderLayout .SOUTH);
}
////////////////////////// ////////// ////////// ////////// ////
private void addDActionPerformed(Action Event evt)
{
JLabel label = new JLabel(theTextField.getTex t());
GridBagConstraints gridBagConstraints =new GridBagConstraints();
allDoctors[nrOfDsAdded] = new doctor();
allDoctors[nrOfDsAdded].se tName(theT extField.g etText());
label.setText(theTextField .getText() );
theTextField.setText("");
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = nrOfDsAdded;
gridBagConstraints.anchor = GridBagConstraints.WEST;
nrOfDsAdded++;
leftpanel.add(label, gridBagConstraints);
leftpanel.revalidate();
leftpanel.repaint();
}
////////////////////////// ////////// ////////// /////////
public void main( String[] args )
{
m frame = new m() ;
frame.show() ;
}
////////////////////////// ////////// ////////// ////////
}//hframe
import java.util.ArrayList;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.*;
public class m extends JFrame {
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel bottom = new JPanel();
JScrollPane sp = new JScrollPane();
JButton d =new JButton("Open List");
JPanel contentPane;
JMenu File = new JMenu();
JMenuItem FileExit = new JMenuItem("Exit");
Border border1;
Border border2;
//layouts
BorderLayout borderLayout1 = new BorderLayout();
BorderLayout borderLayout2 = new BorderLayout();
BorderLayout borderLayout3 = new BorderLayout();
JDesktopPane desktop = new JDesktopPane();
JMenuBar jMenuBar1 = new JMenuBar();
JToolBar jToolBar1 = new JToolBar();
//toolBar buttons
JButton help=new JButton("Help");
//panels
JPanel eastPanel = new JPanel();
JPanel centerPanel = new JPanel();
//layouts
BorderLayout docborder = new BorderLayout();
//Interface
private JButton addName;
private JPanel bigpanel;
private JPanel southPanel;
private JPanel leftpanel;
private JScrollPane theScrollPane;
private JTextField theTextField;
doctor[] allDoctors;//original
int nrOfDsAdded = 0;//original
public boolean frameOpened = false;
//////////////////////////
///Constructor
public m() {
enableEvents(AWTEvent.WIND
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//////////////////////////
///Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
border1 = BorderFactory.createLineBo
border2 = BorderFactory.createLineBo
this.setSize(new Dimension(500, 400));
this.setDefaultCloseOperat
//////////////////////////
allDoctors = new doctor[100];
//////////////////////////
//set up menu items and their actions
JPopupMenu.setDefaultLight
addWindowListener(new java.awt.event.WindowAdapt
public void windowClosing(java.awt.eve
exitForm(evt);
}
});
File.setText("File");
File.setMnemonic(KeyEvent.
FileExit.setAccelerator(Ke
FileExit.addActionListener
public void actionPerformed(ActionEven
FileExit_actionPerformed(e
}
});
//////////////////////////
//toolbar buttons
d.addActionListener( new ActionListener()
{ public void actionPerformed(ActionEven
d_actionPerformed(e);
}
});
//////////////////////////
contentPane.setLayout(bord
desktop.setLayout(borderLa
//add components of menu, desktop and toolbar
File.add(FileExit);
jMenuBar1.add(File);
jToolBar1.add(d);
setJMenuBar(jMenuBar1);
desktop.add(eastPanel, BorderLayout.EAST);
desktop.add(centerPanel, BorderLayout.CENTER);
contentPane.add(desktop, BorderLayout.CENTER);
contentPane.add(jToolBar1,
}//jb init
//////////////////////////
private void exitForm(WindowEvent evt)
{
System.exit(0);
}//end
//////////////////////////
//File | Exit action performed
public void FileExit_actionPerformed(A
{
System.exit(0);
}//end
//////////////////////////
public void d_actionPerformed(ActionEv
{
JInternalFrame dFrame = new JInternalFrame(" My List",false,false,true,tru
if (frameOpened == false) {
centerPanel.add(dFrame, BorderLayout.CENTER);
dFrame.setBackground(Color
dFrame.setVisible(true);
addFrameLayout(dFrame);
dFrame.pack();
frameOpened = true;
}//if
else
dFrame.moveToFront();
}
//////////////////////////
protected void processWindowEvent(WindowE
super.processWindowEvent(e
if (e.getID() == WindowEvent.WINDOW_CLOSING
FileExit_actionPerformed(n
}
}
//////////////////////////
private void addFrameLayout(final JInternalFrame f)
{
theScrollPane = new JScrollPane();
bigpanel = new JPanel();//carrier
leftpanel = new JPanel();//for names
southPanel = new JPanel();//for button
theTextField = new JTextField();
addName = new JButton();
leftpanel.setLayout(new GridBagLayout());
bigpanel.setLayout(new FlowLayout(FlowLayout.LEFT
bigpanel.add(leftpanel);
theScrollPane.setViewportV
f.getContentPane().add(the
theTextField.setPreferredS
theTextField.addActionList
{
public void actionPerformed(ActionEven
addDActionPerformed(evt);
if(f.getHeight() < (desktop.getHeight()-jTool
{
f.setSize(f.getWidth(), f.getHeight() + 12);
}
}
});
addName.setText("Add name");
addName.addActionListener(
{
public void actionPerformed(ActionEven
addDActionPerformed(evt);
int h =desktop.getHeight();
System.out.println("deskto
int t = jToolBar1.getHeight();
System.out.println("toolba
int m = jMenuBar1.getHeight();
System.out.println("menuba
f.setSize(f.getWidth(),f.g
}
});
southPanel.add(theTextFiel
southPanel.add(addName);
f.setFrameIcon(null);
f.getContentPane().add(sou
}
//////////////////////////
private void addDActionPerformed(Action
{
JLabel label = new JLabel(theTextField.getTex
GridBagConstraints gridBagConstraints =new GridBagConstraints();
allDoctors[nrOfDsAdded] = new doctor();
allDoctors[nrOfDsAdded].se
label.setText(theTextField
theTextField.setText("");
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = nrOfDsAdded;
gridBagConstraints.anchor = GridBagConstraints.WEST;
nrOfDsAdded++;
leftpanel.add(label, gridBagConstraints);
leftpanel.revalidate();
leftpanel.repaint();
}
//////////////////////////
public void main( String[] args )
{
m frame = new m() ;
frame.show() ;
}
//////////////////////////
}//hframe
ASKER
there might be some mistakes due to cut and paste but you get the idea from this code
thank you
thank you
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that worked.
Thank you
Thank you
:)