0

I am trying to make a custom chat dialogue window for my instant messenger program. The dialogue is made of a separate class with a JFrame and a couple extra properties. I can create the JFrame and several components (a send message button, a text field for typing the message, and the scrollpane which is supposed to contain my JTextPane). Unfortunately, the JTextPane won't show up, only a grey box where the scrollpane is (I'm assuming that's what it is).

Here is the code for my custom dialogue window. package instantmessenger;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.text.Document;


public class ChatDialogue extends JComponent{
private BuddyConversation owner;


/** COMPONENTS *****************************/
private JFrame window;
private javax.swing.JButton btnSendMessage;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane textMessages;
private javax.swing.JTextField textNewMessage;
/*******************************************/


public ChatDialogue(String username, BuddyConversation owner){
    this.owner = owner;
    initComponents(username);
}
private void btnSendMessageActionPerformed(java.awt.event.ActionEvent evt){
    owner.addMessage(textNewMessage.getText());
}
public javax.swing.JTextPane getMessageBox(){
    return textMessages;
}
private void initComponents(String toUser){
    window = new JFrame();
    window.setAlwaysOnTop(true);
    window.setBounds(new java.awt.Rectangle(0, 0, 200, 400));
    window.setMaximizedBounds(new java.awt.Rectangle(0, 0, 200, 12312312));
    window.setMaximumSize(new java.awt.Dimension(200, 12312312));
    window.setMinimumSize(new java.awt.Dimension(200, 375));
    window.setTitle("Chat with " + toUser);

    textMessages = new javax.swing.JTextPane();
    jScrollPane1 = new javax.swing.JScrollPane(textMessages);
     jLabel1 = new javax.swing.JLabel();
    textNewMessage = new javax.swing.JTextField();
    btnSendMessage = new javax.swing.JButton();

    javax.swing.GroupLayout windowLayout = new javax.swing.GroupLayout(window.getContentPane());
    window.getContentPane().setLayout(windowLayout);
    windowLayout.setHorizontalGroup(
        windowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(windowLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(windowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1)
                .addComponent(textNewMessage)
                .addGroup(windowLayout.createSequentialGroup()
                    .addGroup(windowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(btnSendMessage))
                    .addGap(0, 29, Short.MAX_VALUE)))
            .addContainerGap())
    );
    windowLayout.setVerticalGroup(
        windowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(windowLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(textNewMessage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(btnSendMessage)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    btnSendMessage.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnSendMessageActionPerformed(evt);
        }
    });
    /** ADD COMPONENTS TO JFRAME ******************************************/
    //window.getContentPane().add(jLabel1);
    //window.getContentPane().add(jScrollPane1);
    //window.getContentPane().add(textNewMessage);
    //window.getContentPane().add(btnSendMessage);
    //window.add(textMessages);
    /**********************************************************************/

    jScrollPane1.setViewportView(textMessages);

    jLabel1.setText("Put your message her and then hit enter or something or press the button");

    btnSendMessage.setText("Send Message");

    jScrollPane1.setVisible(true);
    textMessages.setVisible(true);
    window.invalidate();
    window.repaint();
    window.pack();
    window.revalidate();
    window.setVisible(true);
    }
}

So right now as I said before the only thing that shows up is a grey outline where the text and scrollpane should go. I made the JFrame design in the design editor in my main GUI and then just copy and pasted the autogenerated code into my custom class...

The code at the bottom is just me messing around trying to get it to work. I could have probably switched it to a normal textfield or something and gotten it to work but I can't stand not knowing what I'm doing wrong. Help would be much appreciated.

Tyler Helmuth
  • 129
  • 2
  • 11
  • Is there any reason why you are not importing most of the classes which you use? – Daniel Lerps Apr 20 '14 at 05:39
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). `window.setBounds(new java.awt.Rectangle(0, 0, 200, 400));` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Apr 20 '14 at 05:43
  • See also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Apr 20 '14 at 05:44
  • Yes I've tried adding my text pane straight to the window but nothing changes. I didn't import the classes just because that's how the design editor had them in the autogenerated code. – Tyler Helmuth Apr 20 '14 at 05:46
  • I just added a main method with the call `new ChatDialogue("AAA")` and I see the text pane and it is moving with the scrollpane. – user1803551 Apr 20 '14 at 05:47
  • 1
    Thanks for the help @AndrewThompson I'm new here. And your comment about the setBounds is what fixed it for me. After removing the setBounds and setMaximumBounds calls the textpane showed up fine for me. – Tyler Helmuth Apr 20 '14 at 05:49
  • Fast learner - good to see. :) I upgraded the comment to an answer. – Andrew Thompson Apr 20 '14 at 05:54

2 Answers2

1
window.setBounds(new java.awt.Rectangle(0, 0, 200, 400));
window.setMaximizedBounds(new java.awt.Rectangle(0, 0, 200, 12312312));
window.setMaximumSize(new java.awt.Dimension(200, 12312312));
window.setMinimumSize(new java.awt.Dimension(200, 375));

See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)

So the above should be:

window.pack();
window.setMinimumSize(window.getSize()); //this is a rare exception to the rule

Note that frame will end up at co-ordinate 0,0 (top left) of screen. For frame positioning, you cannot go by setLocationByPlatform(true). See this answer for demo.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Andrew Thompson's pointer about setBounds is what fixed this for me. Removing the calls to setBounds and setMaximumBounds somehow cleared up some platform specific problem my textpane was having. For other people the original code apparently worked fine. Thanks!

Tyler Helmuth
  • 129
  • 2
  • 11
  • If Andrew's suggestion helped solve the problem then you should accept the answer an make the comment on his answer. – camickr Apr 20 '14 at 17:31