Results 1 to 8 of 8
  1. #1
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Text areas and fields not showing up

    I have made sort of a chat room that works on the console. Obviously not all users are going to be able to use the console, so I'm now creating a gui on the client end with a text area to view all of the chatting and a text field at the top for input. Right now it's being stubborn though and all I see is the 'main' panel background. Here's the gui 'section' of my code:

    Java Code:
            public Client() throws IOException{
    		scan = new Scanner(System.in);
    		
    		frame = new JFrame();
    		main = new JPanel(new BorderLayout());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setTitle("El mejor Salon de Chat del Mundo");
    		frame.setSize(800, 600);
    		frame.setLocationRelativeTo(null);
    		frame.add(main);
    		
    		frame.setVisible(true);
    		setComponents();
    		
    		//connect();
    	}
    	
    	public void setComponents(){
    		main.setBackground(new Color(100, 150, 240));
    		
    		textField = new JTextField();
    		textField.setBackground(Color.green);
    		
    		chatArea = new JTextArea("Test");
    		chatArea.setBackground(Color.black);
    		
    		main.add(chatArea);
    		main.add(textField, BorderLayout.NORTH);
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Text areas and fields not showing up

    Don't call setVisible(true) until *after* adding all components to the GUI.

  3. #3
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: Text areas and fields not showing up

    *giant facepalm* THANKS haha. I might be back to this thread if I run into more trouble though.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,256
    Rep Power
    19

    Default Re: Text areas and fields not showing up

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default New Problem

    Ok bigger problem I think:

    I'm going for this:
    Text areas and fields not showing up-j.png

    but all I'm getting is the blue background from my JTextArea. I even looked at tutorials for the SpringLayout and did the exact same steps in my 'top' jpanel, but now that panel isn't even showing up.

    Java Code:
    public class Client implements ActionListener{
    	private Socket socket;
    	private Scanner scan;
    	private BufferedReader in;
    	private PrintWriter out;
    	private String ask;
    	
    	private JFrame frame;
    	private JPanel top;
    	private JTextArea chatArea;
    	private JTextField textField;
    	private JButton change;
    	private JLabel label;
    	
    	public Client() throws IOException{
    		scan = new Scanner(System.in);
    		
    		frame = new JFrame();
    		frame.setLayout(new BorderLayout());		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setTitle("El mejor Salon de Chat del Mundo");
    		frame.setSize(600, 500);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    		
    		setComponents();
    		frame.setVisible(true);
    		
    		//connect();
    	}
    	
    	public void setComponents(){
    		SpringLayout layout = new SpringLayout();
    		top = new JPanel(layout);
    		top.setBackground(new Color(55, 55, 55));
    		
    		change = new JButton("Change Look");
    		layout.putConstraint(SpringLayout.WEST, change, 5, SpringLayout.WEST, top);
    		layout.putConstraint(SpringLayout.NORTH, change, 5, SpringLayout.NORTH, top);
    		
    		label = new JLabel("Please enter your name: ");
    		layout.putConstraint(SpringLayout.WEST, label, 15, SpringLayout.EAST, change);
    		layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, top);
    		
    		textField = new JTextField();
    		layout.putConstraint(SpringLayout.WEST, textField, 5, SpringLayout.EAST, label);
    		layout.putConstraint(SpringLayout.NORTH, textField, 5, SpringLayout.NORTH, top);
    		textField.addActionListener(this);
    		
    		chatArea = new JTextArea();
    		chatArea.setBackground(new Color(110, 160, 250));
    		chatArea.setEditable(false);
    		
    		frame.add(new JScrollPane(chatArea), BorderLayout.CENTER);
    		frame.add(top, BorderLayout.NORTH);
    		top.add(change);
    		top.add(label);
    		top.add(textField);
    	}

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Text areas and fields not showing up

    Why jump to one of the more difficult layouts? Why not simply use BorderLayout for the main JPanel (or contentPane), place the JTextArea in a JScrollPane and the JScrollPane added to the GUI BorderLayout.CENTER, and then create a JPanel that uses either FlowLayout or BoxLayout, stuff it with the JButton, JLabel and JTextField, and then add this JPanel to the main JPanel, BorderLayout.PAGE_START?

  8. #8
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: Text areas and fields not showing up

    Edit: Got it! Needed to fix my textfield.
    Last edited by Bestsanchez; 05-24-2012 at 06:41 AM.

Similar Threads

  1. text areas expanding
    By aizen92 in forum New To Java
    Replies: 5
    Last Post: 03-20-2011, 08:51 PM
  2. disapling text fields
    By javanew in forum AWT / Swing
    Replies: 2
    Last Post: 05-06-2010, 03:39 PM
  3. AWT: Painting buttons and text areas in a canvas
    By chappa in forum AWT / Swing
    Replies: 6
    Last Post: 01-09-2010, 02:37 PM
  4. text areas, printstream, and "\n"
    By diggitydoggz in forum New To Java
    Replies: 10
    Last Post: 12-26-2008, 04:03 PM
  5. Demonstration of text fields in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •