1

I am creating a GUI in java. Currently i have an empty JFrame and am trying to add a JPanel to it. The JPanel contains buttons, text etc. However none of this is being displayed. My code is as follows:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class memoDisplayUI { 


JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea jTextBox = new JTextArea();
JScrollPane scroll = new JScrollPane();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                memoDisplayUI frame = new memoDisplayUI();
                frame.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public memoDisplayUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    
    frame.getContentPane().setBackground(new Color(255, 255, 255));
    frame.getContentPane().setLayout(null);
    frame.setBounds(100, 100, 270, 400);
    frame.setUndecorated(true); //REMOVES MENU BAR
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JLabel lblMemos = new JLabel("MEMOS");
    lblMemos.setForeground(new Color(100, 149, 237));
    lblMemos.setFont(new Font("Moire", Font.BOLD, 30));
    lblMemos.setBounds(16, 16, 234, 37);
    panel.add(lblMemos);
    
    JButton button = new JButton("");
    button.setBackground(new Color(100, 149, 237));
    button.setBounds(7, 350, 40, 40);
    panel.add(button);
    button.setIcon(new ImageIcon("back.png"));
    
    JButton button_1 = new JButton("");
    button_1.setBackground(new Color(100, 149, 237));
    button_1.setBounds(113, 350, 40, 40);
    panel.add(button_1);
    button_1.setIcon(new ImageIcon("Edit.png"));
    
    JButton button_2 = new JButton("");
    button_2.setBackground(new Color(100, 149, 237));
    button_2.setBounds(220, 350, 40, 40);
    panel.add(button_2);
    button_2.setIcon(new ImageIcon("memo.png"));
    
    JButton btnExit = new JButton("");
    btnExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    });
    btnExit.setBorder(null);
    btnExit.setIcon(new ImageIcon("Exit.jpg"));
    btnExit.setBounds(216, 19, 40, 40);
    panel.add(btnExit);
    
    jTextBox = new JTextArea();
    scroll.setViewportView(jTextBox); // add scroll panel
    jTextBox.setTabSize(4);
    jTextBox.setLineWrap(true);
    jTextBox.setBackground(new Color(192, 192, 192));
    jTextBox.setBounds(8, 60, 255, 286);
    panel.add(jTextBox);
    
    frame.getContentPane().add(panel);
}
}

Could someone please advise as to why this is?

Thanks very much :)

Edit

From a few tweaks to the code, it appears this is the desired layout (in a non-resizable GUI).

Memos GUI

Community
  • 1
  • 1
user3120023
  • 197
  • 3
  • 6
  • 16
  • Hint: Are your components being added to the frame or the **panel**? What's going on with your frame, that should rather be going on with the panel? – Paul Samsotha Apr 04 '14 at 10:05
  • 2
    That being said, you should really look into using a [LayoutManager](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) and avoiding null layouts. Let the layouts do the sizing and placing for you ;) – Paul Samsotha Apr 04 '14 at 10:06
  • I think you used null to get a "place it wherever fits"? Then use a FlowLayout :) – Dylan Meeus Apr 04 '14 at 10:11
  • 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 04 '14 at 10:28
  • BTW - I edited an image into the question. Is that the basic layout you are trying to achieve? If so, a better question would be "How to layout this GUI?" (so long as you provide an attempt). – Andrew Thompson Apr 04 '14 at 10:48

6 Answers6

2

I think you used null to get a "place it wherever fits"? Then use a FlowLayout

frame.setLayout(new FlowLayout());

That should fix it :)

Dylan Meeus
  • 5,696
  • 2
  • 30
  • 49
2

Could someone please advise as to why this is?

Using null layouts and not calling pack().

The image I edited into the question was obtained as a screenshot of the GUI after I had commented out the call to setUndecorated(true) and dragged it a little bigger. Doing so causes the JRE to validate the component structure (what pack() would do) and thereby make the components appear.


As I mentioned in a comment:

..a better question would be "How to layout this GUI?" (so long as you provide an attempt)

And that leads me to my first comment. (Now in longer form)

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 them1, along with layout padding & borders for white space2.




So coming back to:

(so long as you provide an attempt)

Look over those two examples to see how they work, then attempt to combine some layouts and padding to create a frame that can then be packed to reduce to the natural size.

And a tip the the JTextArea. Suggest a size in columns x rows combined with the Font size.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks very much for the comprehensive answer, however I'm still struggling on how to implement this. Thanks :) – user3120023 Apr 04 '14 at 11:13
  • If that is your question ..ask a **new** question. This question, specifically, "Could someone please advise as to why this is?" has been answered IMO. – Andrew Thompson Apr 04 '14 at 11:16
1

Just remove/comment this line from the above code at line number 46.

// frame.getContentPane().setLayout(null);

It should work fine..

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Akash Goswami
  • 306
  • 3
  • 14
1

Replace

frame.getContentPane().setLayout(null);

with

frame.getContentPane().setLayout(new BorderLayout());

Good luck.

Edit: For future reference, to decide which LayoutManager should be used in your case, you should refer to this Visual Guide to LayoutManagers.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
1

1: You should never call setLayout(null). 2: Try frame.validate() to layout the components with your layout.

Shadow3097
  • 28
  • 7
0

Maybe you shoul replace :

frame.getContentPane().add(panel);

by

frame.setContentPane(panel);

Hope it helped

BaptisteL
  • 490
  • 6
  • 15