14 Replies - 375 Views - Last Post: 06 April 2013 - 07:08 AM Rate Topic: -----

#1 JamesHudson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-March 13

Understanding JFrames in java

Posted 26 March 2013 - 12:52 PM

hi i am working on a dictionary program in java that uses the JFrame class, but i can not work out why my dictionary program is not adding the JLabel to the window. Here's the constructor.

private static JTextField SearchBox;
private static HashMap<String, String> map;
private static JPanel Panel = new JPanel();

public DPGraphics(){

    super("Dictionary");
    Panel.setLayout( new BorderLayout());

    add(Panel);

    SearchBox = new JTextField(20);
    SearchBox.setEditable(false);
    Panel.add(SearchBox, BorderLayout.NORTH);
}


now that the Jframe and panel are added to the window can i just add a component to the panel, like this

Panel.add(new JLabel("Test"), BorderLayout.EAST);

but this has no effect on the window. I've tryed everything that i can think of, but nothing works. I've got some more code that listens for the input from the SearchBox that adds the JLabel to the window, Here's the code.
public void ListemForAction(){

    SearchBox.setEditable(true);
    TheHandler Handle = new TheHandler();
    SearchBox.addActionListener(Handle);
}


this just listens for an action in the SearchBox and updates the window to have the new JLabel that isn't working.

and here's the code for the TheHandler class.
private class TheHandler implements ActionListener{


    public void actionPerformed(ActionEvent event){
        System.out.println("got to the ActionEvent method");

        String EnteredString = event.getActionCommand();

        map = DP.getMap();


        updatedGraphics(EnteredString, map.get(EnteredString));}
lastly the update graphics method.

public void updatedGraphics(String Key, String Value){

    System.out.println("got to the updatedGraphics method");

    if(Key.equals("ZYME")){
        System.out.println("It's just not adding to the srceen " + Key + " " + Value);
    }

    JLabel KeyAndValue = new JLabel(Key);
    Panel.add(KeyAndValue, BorderLayout.CENTER);
}



i know that if you input something in to the Searchbox that it would get to the ListemForAction method but its just not adding it to the window. and i cant work out why, i've been trying to get it to work for a little over 2 weeks now and i cant work out what it is thats i'm doing wrong.

*Edited: please :code:

This post has been edited by pbl: 26 March 2013 - 12:57 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Understanding JFrames in java

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Understanding JFrames in java

Posted 26 March 2013 - 12:58 PM

Don't forget your Posted Image tags. It might be something to do with the variable name 'Panel'. 'Panel' already exists as part of the awt API, it might be referring to that rather than your JPanel. Common convention tells you to write those variable names with a lowerCamelCase.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Understanding JFrames in java

Posted 26 March 2013 - 01:02 PM

Please :code:
Respect Java conviention a variable starts by a lower character so

private static JPanel Panel = new JPanel();

should be

private static JPanel panel = new JPanel();

so later on in your code we know we are talking about a panel object and not the Panel class

What is public DPGraphics(){ to which you add a JPanel() ?
Please include class declaration in your code

Quote

now that the Jframe and panel are added to the window can

what do you call a window ? You can't add a JFrame to anything

Thanks
Was This Post Helpful? 1
  • +
  • -

#4 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Understanding JFrames in java

Posted 26 March 2013 - 01:05 PM

Indeed, as pbl has pointed out you don't even have a contentPane as far as this code is concerned. Use Container c = getContentPane(); and then add your panel with c.add(panel);
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Understanding JFrames in java

Posted 26 March 2013 - 01:10 PM

View PostFlukeshot, on 26 March 2013 - 04:05 PM, said:

Indeed, as pbl has pointed out you don't even have a contentPane as far as this code is concerned. Use Container c = getContentPane(); and then add your panel with c.add(panel);

@Flukeshot:
You don't need a contentPane to add() a component (at least since JRE 1.5)
You will need it thought if you plan to set the JFrame backGround() color

--------------------- cut here -------------------

@OP:
It is never a good idea to add component on the fly as you do.
Set the component visible(false)
add it a construction time
and toggle its visibility in the actionPerformed()

However, if you persist with the bad habit of adding Component en the fly, you will have to call the validate() method on the JFrame

This post has been edited by pbl: 26 March 2013 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Understanding JFrames in java

Posted 26 March 2013 - 01:18 PM

I tend to keep my GUI in it's own class with it's own nested listeners.

Upon construction of the GUI object, the method GUI.setupGUI(); is called.

This method deals with everything from creating a container, setting any layout managers and adding all components to the container, to setting visibility, size etc.

Is this bad practice in your opinion (is this an example of adding components 'on the fly'?) or was your post directed at me only for the first part? :)/>
Was This Post Helpful? 0
  • +
  • -

#7 JamesHudson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-March 13

Re: Understanding JFrames in java

Posted 26 March 2013 - 01:35 PM

View Postpbl, on 26 March 2013 - 01:10 PM, said:

View PostFlukeshot, on 26 March 2013 - 04:05 PM, said:

Indeed, as pbl has pointed out you don't even have a contentPane as far as this code is concerned. Use Container c = getContentPane(); and then add your panel with c.add(panel);

You don't need a contentPane to add() a component (at least since JRE 1.5)
you will need it to set the backGround() color thought

It is never a good idea to add component on the fly as you do.
Set the component visible(false)
add it a construction time
and toggle its visibility in the actionPerformed()

However, if you persist with the bad habit of adding Component en the fly, you will have to call the validate() method on the JFrame


hi thanks, i am still learning and your comment was helpful on bad habits that i pit up, i hadn't try this yet but will do later.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Understanding JFrames in java

Posted 26 March 2013 - 01:39 PM

View PostFlukeshot, on 26 March 2013 - 04:18 PM, said:

Is this bad practice in your opinion (is this an example of adding components 'on the fly'?) or was your post directed at me only for the first part? :)/>/>

Was directed to you only for the first part :)

I'll make it clearer :^:
Was This Post Helpful? 0
  • +
  • -

#9 JamesHudson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-March 13

Re: Understanding JFrames in java

Posted 26 March 2013 - 01:41 PM

thanks for everyone's help, it's great that people would help, i've been getting no feedback on other site's.
Was This Post Helpful? 0
  • +
  • -

#10 JamesHudson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-March 13

Re: Understanding JFrames in java

Posted 26 March 2013 - 01:47 PM

View Postpbl, on 26 March 2013 - 01:39 PM, said:

View PostFlukeshot, on 26 March 2013 - 04:18 PM, said:

Is this bad practice in your opinion (is this an example of adding components 'on the fly'?) or was your post directed at me only for the first part? :)/>/>/>

Was directed to you only for the first part :)/>

I'll make it clearer :^:/>

sorry i didn't see your post can you elaborate on this please.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Understanding JFrames in java

Posted 26 March 2013 - 01:52 PM

It is my post #5

It wasn't clear ? I edited it to separate what I was telling to Flukeshot and what I was telling to you.

The part addressed to you is still not clear ?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Understanding JFrames in java

Posted 26 March 2013 - 02:02 PM

View PostJamesHudson, on 26 March 2013 - 04:41 PM, said:

thanks for everyone's help, it's great that people would help, i've been getting no feedback on other site's.

Glad we could help
Come back whenever you want
Was This Post Helpful? 0
  • +
  • -

#13 JamesHudson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-March 13

Re: Understanding JFrames in java

Posted 27 March 2013 - 08:51 AM

i just try this code

		public void actionPerformed(ActionEvent event){
			
			key = event.getActionCommand();
			value = DP.getMap().get(key);
			
			keyLabel.setVisible(true);
			valueLabel.setVisible(true);
		}



and the JLabels are all way null, here my constructor and variables.

	private static JTextField searchBox;
	private static HashMap<String, String> map;
	private static JPanel panel;
	private static String key;
	private static String value;
	private static JLabel keyLabel;
	private static JLabel valueLabel;
	
	public DPGraphics(){
		
		super("Dictionary");
		
		panel = new JPanel();
		panel.setLayout( new BorderLayout());
		
		add(panel);
		
		searchBox = new JTextField(20);
		searchBox.setEditable(false);
		add(searchBox, BorderLayout.NORTH);
		
		keyLabel = new JLabel(key + "Hello world");
		keyLabel.setVisible(true);
		panel.add(keyLabel, BorderLayout.CENTER);
		
		valueLabel = new JLabel(value + "Hello world");
		valueLabel.setVisible(true);
		panel.add(valueLabel, BorderLayout.EAST);
	}



i cant do it this way, the keyLabel and valueLabel are not updating.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Understanding JFrames in java

Posted 27 March 2013 - 07:36 PM

Your completly missed the concept

first in your constructor, as value is not determine yet, you should just do:

    keyLabel = new JLabel();
    keyLabel.setVisible(false);
    panel.add(keyLabel, BorderLayout.CENTER);



in the actionPerformed()

   keyLabel.setText(value + "....
   keyLabel.setVisible(true);


Was This Post Helpful? 0
  • +
  • -

#15 JamesHudson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-March 13

Re: Understanding JFrames in java

Posted 06 April 2013 - 07:08 AM

View Postpbl, on 27 March 2013 - 07:36 PM, said:

Your completly missed the concept

first in your constructor, as value is not determine yet, you should just do:

    keyLabel = new JLabel();
    keyLabel.setVisible(false);
    panel.add(keyLabel, BorderLayout.CENTER);



in the actionPerformed()

   keyLabel.setText(value + "....
   keyLabel.setVisible(true);



sorry ye i had just forget the put true in side setVisible. thanks for your help tho
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1