1 Replies - 12711 Views - Last Post: 04 January 2011 - 04:48 AM Rate Topic: -----

#1 darek9576   User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,731
  • Joined: 13-March 10

Adding a JPanel to an existing JFrame [solved]

Posted 04 January 2011 - 04:35 AM

I have a JTextField wher the user types in the names. After enter has been presssed, i want to add the user input to two JPanel objects and then add the JPanel to the SAME EXISTING frame - so basically the setVisible(true) method has been called before. Another user entry would add it underneath the previous one and so on.

I have no idea how to apprach it.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Demo implements ActionListener
{

	private JFrame frame;
	private JTextField tf;
	
	public Demo()
	{
		
	}
	
	public void createFrame()
	{
		frame = new JFrame("Title");
		Container contentPane = frame.getContentPane();
		tf = new JTextField(20);
		tf.addActionListener(this);
		
		JPanel panel = new JPanel();
		panel.add(tf);
		
		contentPane.add(panel, BorderLayout.NORTH);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(350,350);
		frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent event)
	{
		String name = null;
		name = tf.getText();
		
		JPanel panel = new JPanel();
		JPanel otherPanel = new JPanel();
		panel.add(new JLabel(name));
		otherPanel.add(panel);
		
		System.out.println(name);
		tf.selectAll();
	}
	
	public static void main(String[] args)
	{
		Demo d = new Demo();
		d.createFrame();
	}
}



Thanks for help.

This post has been edited by pbl: 04 January 2011 - 12:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding a JPanel to an existing JFrame [solved]

#2 darek9576   User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,731
  • Joined: 13-March 10

Re: Adding a JPanel to an existing JFrame [solved]

Posted 04 January 2011 - 04:48 AM

Got it! I have no idea how to mark the post as soleved but if anyone knows then feel free.

Solution: validate() method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1