Solved

desktop hieght

Posted on 2004-08-20
15
305 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:Samooramad
15 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11854034
Can you not use the getHeight() method?
0
 
LVL 35

Accepted Solution

by:
girionis earned 100 total points
ID: 11854044
Everytime it is incremented use the getHeight() of the desktop and compare the height with that of the internal frame.
0
 

Author Comment

by:Samooramad
ID: 11854375
gethieght of what?
0
 

Author Comment

by:Samooramad
ID: 11854400
oh ok.. I will try that
0
 

Author Comment

by:Samooramad
ID: 11856149
no it didnt work. It keeps going down and disapears behind the toolbar and keeps going.
could you include a working example?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11856163
Can you post some sampel code (a small compilable example) so I can try that and see what's going on?
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11856285
can u post ur code??

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 200 total points
ID: 11857605
Hi Friend;

I made an example same as I gave you before but this sample explanes the limitation part. It actually refresh the size on
every text entered but it limits it to up to 350 by 250 size, which you can use on your part. Just know the size of your
desired size and walla the limit will cut it down.

/**
 * Sample Class to Gradualy Refresh the Size of
 * JInternalFrame w/ Limitations in a
 * JTextField Component
 *
 * Made By : Javatm
 * Date      : 08/21/2004
 * Time     : 11:46 AM PH
 *
 **/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;

public class Sample extends JFrame {

     private Document doc;

     private int HightX = 300;
     private int WidthY = 200;
     private int LimitX = 350;
     private int LimitY = 250;

     private JInternalFrame f1;
     private JDesktopPane d1;
     private JTextField t1;
     private JLabel l1;
     private JPanel p1;
     private JPanel p2;
     private String s1;

     public Sample() {

     super("Sample . . .");

     d1 = new JDesktopPane();
     f1 = new JInternalFrame("", true, true, true, true);

     p1 = new JPanel();
     p2 = new JPanel();

     l1 = new JLabel("Enter Text Here :     ");
     t1 = new JTextField(10);
     doc = t1.getDocument();
     
     p1.setLayout(new GridLayout(3,1));
     p1.add(new JPanel());
     p1.add(p2);
     p1.add(new JPanel());

     p2.add(l1); p2.add(t1);

     f1.setSize(HightX, WidthY);
     f1.setVisible(true);
     f1.getContentPane().add(p1);

     d1.add(f1);

     this.getContentPane().add(d1, BorderLayout.CENTER);

     setSize(400, 350);
     setVisible(true);

     doc.addDocumentListener(new DocumentListener()
     {
        public void changedUpdate(DocumentEvent e)
        {
        f1.setTitle(t1.getText());
        f1.setSize(HightX, WidthY);

        // Limitation Part . . .

        if (LimitX == HightX) {

        System.out.println("Limitation Started !");
        f1.setSize(LimitX, LimitY);
        }
        else {
        HightX++; WidthY++;
        }
        }
                     
        public void insertUpdate(DocumentEvent e)
        {
        f1.setTitle(t1.getText());  
        f1.setSize(HightX, WidthY);

        // Limitation Part . . .

        if (LimitX == HightX) {

        System.out.println("Limitation Started !");
        f1.setSize(LimitX, LimitY);
        }
        else {
        HightX++; WidthY++;
        }
        }

        public void removeUpdate(DocumentEvent e)
        {
        f1.setTitle(t1.getText());  
        f1.setSize(HightX, WidthY);

        HightX--; WidthY--;
        }
        });
     }

     public static void main(String args[]) {

     Sample x = new Sample();
     x.setDefaultCloseOperation(EXIT_ON_CLOSE);
     }
}

Hope that helps . . .
Javatm
0
 

Author Comment

by:Samooramad
ID: 11858154
sorry my reply comes late,I was having PC problems before..sure I will post it
0
 

Author Comment

by:Samooramad
ID: 11859061
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
0
 

Author Comment

by:Samooramad
ID: 11859216
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.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
///////////////////////////////////////////////////////
///Component initialization
private void jbInit() throws Exception  {
      contentPane = (JPanel) this.getContentPane();
    border1 = BorderFactory.createLineBorder(Color.gray,2);
    border2 = BorderFactory.createLineBorder(Color.black,2);
    this.setSize(new Dimension(500, 400));
    this.setDefaultCloseOperation(3);
///////////////////////////////////////////////
    allDoctors = new doctor[100];
  ///////////////////////////////////////////////
//set up menu items and their actions

    JPopupMenu.setDefaultLightWeightPopupEnabled(false);

    addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

    File.setText("File");
    File.setMnemonic(KeyEvent.VK_F);

   FileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
    FileExit.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        FileExit_actionPerformed(e);
      }
     });

//////////////////////////////////////////
//toolbar buttons

  d.addActionListener( new ActionListener()
 { public void actionPerformed(ActionEvent e) {
       d_actionPerformed(e);
     }
 });


/////////////////////////////////

contentPane.setLayout(borderLayout1);
desktop.setLayout(borderLayout2);

//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(ActionEvent e)
{
  System.exit(0);
}//end

/////////////////////////////////////

  public void d_actionPerformed(ActionEvent e)
  {
  JInternalFrame dFrame = new JInternalFrame(" My List",false,false,true,true);


 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(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      FileExit_actionPerformed(null);
    }
  }

//////////////////////////////////
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.setViewportView(bigpanel);

    f.getContentPane().add(theScrollPane,BorderLayout.CENTER);
    theTextField.setPreferredSize(new Dimension(150, 20));

     theTextField.addActionListener(new ActionListener()
     {
       public void actionPerformed(ActionEvent evt) {
       addDActionPerformed(evt);

       if(f.getHeight() < (desktop.getHeight()-jToolBar1.getHeight()-jMenuBar1.getHeight()-200))////this 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(ActionEvent evt) {

        addDActionPerformed(evt);
        int h =desktop.getHeight();
        System.out.println("desktop hieght"+h);
        int t = jToolBar1.getHeight();
        System.out.println("toolbar hieght"+t);
        int m = jMenuBar1.getHeight();
        System.out.println("menubar hieght"+m);

       f.setSize(f.getWidth(),f.getHeight()+ 12);//it should be applied here too!!!!
     }
     });

    southPanel.add(theTextField);
    southPanel.add(addName);

    f.setFrameIcon(null);
    f.getContentPane().add(southPanel,BorderLayout.SOUTH);
  }

////////////////////////////////////////////////////////////
    private void addDActionPerformed(ActionEvent evt)
   {

     JLabel label = new JLabel(theTextField.getText());
     GridBagConstraints gridBagConstraints =new GridBagConstraints();

     allDoctors[nrOfDsAdded] = new doctor();
     allDoctors[nrOfDsAdded].setName(theTextField.getText());

     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

0
 

Author Comment

by:Samooramad
ID: 11859226
there might be some mistakes due to cut and paste but you get the idea from this code
thank you
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 200 total points
ID: 11869028
this could be of some help.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class Sample extends JFrame {

     private int HightX = 0;
     private int WidthY = 0;

     private JInternalFrame f1;
     private JDesktopPane d1;
     private JPanel p1;
     private String s1;
       private JButton button;
       int count = 0;

     public Sample() {

     super("Sample . . .");

     d1 = new JDesktopPane();
     f1 = new JInternalFrame("", true, true, true, true);

     p1 = new JPanel();

       button = new JButton("Click");
       p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));


     f1.setVisible(true);
     f1.getContentPane().add(p1);

     d1.add(f1);


     this.getContentPane().add(d1, BorderLayout.CENTER);
     this.getContentPane().add(button, BorderLayout.NORTH);

     setSize(400, 400);
     setVisible(true);

      //System.out.println("desktop height : " + d1.getHeight());

      button.addActionListener(new ActionListener()
            {
                  public void actionPerformed(ActionEvent e)
                  {
                        count++;
                        JLabel label;

                        label = new JLabel("Hello ");
                        label.setBorder(new BevelBorder(BevelBorder.RAISED));
                        label.setSize(20,20);
                        int height = 34 + (1 + p1.getComponentCount() )* label.getHeight();

                        if(height < d1.getHeight()){
                              p1.add(label);
                              f1.setSize(400, height);
                              f1.repaint();
                        }
                        //System.out.println("Count : " + p1.getComponentCount());
                        //System.out.println("height : " + height);
                  }
            });

     }

     public static void main(String args[]) {

     Sample x = new Sample();
     x.setDefaultCloseOperation(EXIT_ON_CLOSE);
     }
}


mb...
0
 

Author Comment

by:Samooramad
ID: 11872515
that worked.
Thank you
0
 
LVL 35

Expert Comment

by:girionis
ID: 11872705
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 68
java  and programming certification ? 4 86
eclipse package explorer vs project explorer view 2 74
Error with Java/Cache JDBC Classpath 2 30
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now