Solved

increase internal frame hieght

Posted on 2004-08-14
4
416 Views
Last Modified: 2012-05-05
Hi experts,
I have an internal frame with a text field. Each time the text field enters a word it is dispayed on the internal frame as labels (under each other)
the internal frame has a scrollbar but the frame stays the same size while the words are entered and you need to maximize it to see them all.
I would like it to gradually increase in hieght as each name is entered.
thanks
0
Comment
Question by:Samooramad
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
vilia earned 250 total points
ID: 11802992
Here is an example. The internal frame is 3 rows initially. It increases up to 10 rows. After that there is not any additional height increase (to obey screen limits).

/*
 * Created on Aug 14, 2004
 */
package tests;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

/**
 * @author vilia
 */
public class InternalFrameHeight extends JFrame {
      // min & max number of rows to be displayed in
      // JInternalFrame
      private static final int LIST_MIN_ROWS = 3;
      private static final int LIST_MAX_ROWS = 10;
      //
      private JInternalFrame internalFrame;
      private DefaultListModel listModel = new DefaultListModel();
      private JList list;

      public InternalFrameHeight() {
            getContentPane().setLayout(new BorderLayout());
            internalFrame = new JInternalFrame();
            internalFrame.setVisible(true);
            list = new JList(listModel);
            list.setVisibleRowCount(LIST_MIN_ROWS);
            internalFrame.getContentPane().add(new JScrollPane(list));
            getContentPane().add(internalFrame, BorderLayout.NORTH);
            getContentPane().add(getBottomPanel(), BorderLayout.SOUTH);
            pack();
            show(true);
      }

      private Panel getBottomPanel() {
            Panel p = new Panel();
            p.setLayout(new BorderLayout());
            final JTextField t = new JTextField() {
                  public Dimension getPreferredSize() {
                        return new Dimension(200, 20);
                  }
            };
            p.add(t, BorderLayout.WEST);
            JButton b = new JButton("Add");
            b.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent ae) {
                        listModel.addElement(t.getText());
                        t.setText("");
                        if (LIST_MIN_ROWS <= listModel.getSize()
                              && listModel.getSize() <= LIST_MAX_ROWS) {
                              list.setVisibleRowCount(listModel.getSize());
                        }
                        if (false) {
                              // resize JInternalFrame only
                              internalFrame.getParent().doLayout();
                        } else {
                              // resize JFrame (main)
                              Component parent = internalFrame;
                              while (!((parent = parent.getParent())
                                    instanceof InternalFrameHeight));
                              ((JFrame) parent).pack();
                        }
                  };
            });

            p.add(b, BorderLayout.EAST);
            return p;
      }

      public static void main(String[] argv) {
            new InternalFrameHeight();
      }
}
0
 
LVL 15

Expert Comment

by:Javatm
ID: 11803004

I dont know if this is what you want but this example extends the size of a JInternalFrame every time
you add a letter to the label of a JInternalFrame. Hope yo like it.

/**
 * Sample Class to Gradualy Refresh the Size of a
 * JInternalFrame in Every Text Entered on a
 * JTextField Component
 *
 * Made By : Javatm
 * Date       : 08/15/2004
 * Time      : 11:59 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 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.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);

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

      HightX++; WidthY++;
        }
        public void removeUpdate(DocumentEvent e) {}
        });
      }

      public static void main(String args[]) {

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

Hope that helps . . .
Javatm
0
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 250 total points
ID: 11803023

This other demo increase's and decrease the size of JInternalFrame if you add or delete a
character in a JTextField, hope you like it.

/**
 * Sample Class to Gradualy Refresh the Size of a
 * JInternalFrame in Every Text Entered on a
 * JTextField Component
 *
 * Made By : Javatm
 * Date       : 08/15/2004
 * Time      : 11:59 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 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.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);

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

        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);
      }

}
0
 

Author Comment

by:Samooramad
ID: 11804680
both worked great
thanks guys
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 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

17 Experts available now in Live!

Get 1:1 Help Now