Solved

increase internal frame hieght

Posted on 2004-08-14
4
453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
throw exception 21 75
maven module vs maven project 3 103
ejb wildfly example 2 104
Java Restore security prompts not working 10 236
In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

739 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