?
Solved

increase internal frame hieght

Posted on 2004-08-14
4
Medium Priority
?
469 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 1000 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.
Suggested Courses

649 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