Solved

increase internal frame hieght

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

770 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