Solved

increase internal frame hieght

Posted on 2004-08-14
4
445 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
factorial example 4 50
tomcat startup error 5 106
Java: How do I open the default windows program for a given file type 3 41
Is there a simpler dropbox system? 10 35
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 …

828 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