1

I already have a JInternalFrame with buttons and text fields but now I want to add a JScrollPane to this Internal Frame, and I don´t know how to.

It is possible add a JScrollPane without add all components again to the JInternalFrame? How?

Thanks!!

I have added the buttons and the texts fields with NetBeans, Design view.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
MartinGian
  • 395
  • 5
  • 16
  • need a LOT more information to help. how did you add the buttons and text fields? you'd add the scrollpane in a similar way. – John Gardner Mar 28 '14 at 21:01
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Mar 28 '14 at 21:03
  • 1
    This looks similar to [a question I answered yesterday](http://stackoverflow.com/questions/22698509/scroll-bar-en-jinternalframe). You create a JScrollPane and add a JPanel to its viewport, and then add that to your JInternalFrame. – Hovercraft Full Of Eels Mar 28 '14 at 21:04
  • @HovercraftFullOfEels How??? Can you show me a piece of code?? – MartinGian Mar 28 '14 at 21:06

1 Answers1

5

A very basic example of creating a JScrollPane and add a JPanel to its viewport, and then add that to your JInternalFrame:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class AddStuffToScrollPane {
   public static void main(String[] args) {
      JPanel panel = new JPanel(new BorderLayout());
      panel.add(new JTextArea(20, 30), BorderLayout.CENTER);
      panel.add(new JPanel(new GridLayout(1, 0)) {
         {
            add(new JButton("Foo"));
            add(new JButton("Bar"));
         }
      }, BorderLayout.PAGE_END);
      JScrollPane scrollPane = new JScrollPane(panel);

      JInternalFrame internalFrame = new JInternalFrame("InternalFrame", true,
            true);
      internalFrame.getContentPane().add(scrollPane);
      internalFrame.setPreferredSize(new Dimension(200, 200));
      internalFrame.setSize(internalFrame.getPreferredSize());
      internalFrame.setVisible(true);
      JDesktopPane desktopPane = new JDesktopPane();
      desktopPane.setPreferredSize(new Dimension(400, 400));
      desktopPane.add(internalFrame);
      JOptionPane.showMessageDialog(null, desktopPane);
   }
}

The most important take home message that you should get if you get nothing at all: gear your GUI classes towards creating JPanels, not JFrames, not JInternalFrames. If you can create a decent JPanel, then you can put it anywhere it is needed: inside of a JFrame, or JInternalFrame, or a JScrollPane,... anywhere!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373