5

How to use a JInternalFrame within a JFrame?

I want to create a client/server app. I know that JInternalFrame should be used with JDeskTopPane but I want a chat window which can be moved within the app (just like chatting in Facebook).

  1. How to use JInternalFrame for this?
  2. Do you suggest any other Swing component for this?
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Roronoa
  • 281
  • 3
  • 10
  • 1
    *"`JDeskTopPane` but I want a chat window which can be moved within the app (just like chatting in Facebook)."* Put a `JInternalFrame` into a `JDesktopPane` and the user can move it at will. E.G. as seen in [this code](http://stackoverflow.com/questions/13859258/java-swing-internal-frame-as-dialog/13859951#13859951) after the internal dialog is closed. – Andrew Thompson Dec 23 '12 at 07:38
  • 1
    As said use a JDesktopPane with your JInternalFrame. here is a nice example too http://stackoverflow.com/a/13815721/1133011 – David Kroukamp Dec 23 '12 at 09:41

1 Answers1

4

enter image description here

Here is the short working example of what you are looking for. Code:

public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {
    initComponents();
}

@SuppressWarnings("unchecked")
    private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jInternalFrame1 = new javax.swing.JInternalFrame();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jInternalFrame1.setBackground(new java.awt.Color(0, 0, 204));
    jInternalFrame1.setBorder(javax.swing.BorderFactory.createMatteBorder(7, 7, 7, 7, new java.awt.Color(0, 0, 0)));
    jInternalFrame1.setTitle("This is JInternal Frame");
    jInternalFrame1.setVisible(true);

    javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
    jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
    jInternalFrame1Layout.setHorizontalGroup(
        jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 386, Short.MAX_VALUE)
    );
    jInternalFrame1Layout.setVerticalGroup(
        jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 304, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jInternalFrame1)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addComponent(jInternalFrame1)
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    );

    pack();
}
   public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
 }


private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JPanel jPanel1;

}

See How to Use Internal Frames for more.

EDIT:

P.S - I have taken help of JPanel. If you wish to hide the component anytime, use setVisible(false) insted of foo.setVisible(false).

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • 1
    +1 for [sscce](http://sscce.org/). Suggestions: 1) Please don't cite old (1.4.2?!) documentation unless required. 2) From the tutorial cited, "Usually, you add internal frames to a desktop pane." 3) Using a GUI designer needlessly complicates your example code. 4) See also how to take a [screenshot](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post). – trashgod Dec 23 '12 at 13:44