Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Diargg

Thread: Strange problem

  1. #1
    djokovic is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Strange problem

    Hello everyone. I have a really weird problem. I created JFrame window, added JPanel to it. When I run the program. It seems, that it is only gray background, and no elements in it,
    Strange problem-1scree.jpg
    but if i press on the corner of the window, everything pop up.
    Strange problem-2scree.jpg
    What could be wrong?
    if you need my code, here it is.
    Java Code:
    public class mainClass {
    	
    	public static void main(String[] args)  {
    		JFrame f = new JFrame("Words Counter");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setSize(410,410);
    		f.setVisible(true);
    		f.getContentPane().add(new mainPanel());
    	}
    	
    }
    Java Code:
    public class mainPanel extends JPanel {
    
    	// Initializing variables
    	private JPanel statsPanel;
    	private JTextArea textArea;
    	private JLabel words;
    	private JButton showStats, recompute;
    	private int amount;
    	private String wordsLabelText;
    	
    	public mainPanel(){
    		setBackground(Color.GRAY);
    		setLayout(new BorderLayout());
    		amount = 0;
    		start();
    		
    	}
    	
    	public void start(){
    		setStatsPanel();
    		setWordsLabel();
    		setButtons();
    		setTextArea();
    	}
    	public void setStatsPanel(){
    		statsPanel = new JPanel();
    		statsPanel.setBorder(BorderFactory.createTitledBorder("Stats Box"));
    		add(BorderLayout.CENTER, statsPanel);
    	}
    	public void setWordsLabel(){
    		wordsLabelText = "Total words ";
    		words = new JLabel(wordsLabelText+amount);
    		statsPanel.add(words);
    	}
    	public void setButtons(){
    		showStats = new JButton("Show words");
    		recompute = new JButton("Clear all");
    		add(BorderLayout.SOUTH, showStats);
    		add(BorderLayout.EAST, recompute);
    	}
    	public void setTextArea(){
    		textArea = new JTextArea(13,10);
    		add(BorderLayout.NORTH, textArea);
    	}
    }
    Last edited by djokovic; 07-05-2012 at 07:12 PM.

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

    Default Re: Strange problem

    Quote Originally Posted by djokovic View Post
    Hello everyone. I have a really weird problem. I created JFrame window, added JPanel to it. When I run the program. It seems, that it is only gray background, and no elements in it,
    ....
    but if i press on the corner of the window, everything pop up.
    ...
    What could be wrong?
    Nothing weird about this. Check out your code:

    Java Code:
    public class mainClass {
    	
    	public static void main(String[] args)  {
    		JFrame f = new JFrame("Words Counter");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setSize(410,410);
    		f.setVisible(true);
    		f.getContentPane().add(new mainPanel());
    	}
    	
    }
    Note where you add your components to the JFrame relative to where you call setVisible(true) on the JFrame. The order is important, and this should make intuitive sense, now that you think of it, right?

  3. #3
    djokovic is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Strange problem

    Quote Originally Posted by Fubarable View Post
    Nothing weird about this. Check out your code:

    Java Code:
    public class mainClass {
    	
    	public static void main(String[] args)  {
    		JFrame f = new JFrame("Words Counter");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setSize(410,410);
    		f.setVisible(true);
    		f.getContentPane().add(new mainPanel());
    	}
    	
    }
    Note where you add your components to the JFrame relative to where you call setVisible(true) on the JFrame. The order is important, and this should make intuitive sense, now that you think of it, right?
    LOL , I'm stupid not the code, thank you. But I want to ask another question, if you don't mind.
    From screens in my last post, you can see, that I want to get words from the textarea, when people push button show stats.
    I have this listener class:
    Java Code:
    private class showStatsListener implements ActionListener{
    		public void actionPerformed(ActionEvent arg0) {
    			amount = 0;
    			String textString = textArea.getText();
    			Scanner scan = new Scanner(textString);
    			while(scan.hasNext()){
    				amount++;
    				scan.next();
    			}
    			
    		}
    		
    	}
    But problem is, that It doesn't update the label named 'words', even if it update the amount integer. So, what I need to write after while loop to update that label? I tried to rewrite code:
    words = new JLabel(wordsLabelText+amount); But it duplicates the label.
    but then

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Strange problem

    Why would you create a new JLabel when there's already one there? I bet you there's some way of setting the Text of a JLabel...
    Fubarable likes this.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Strange problem

    Moved form New to Java.

    Please go through the Forum Rules -- particularly the third paragraph.

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

  6. #6
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Strange problem

    @Dokovic:

    To solve your problem:
    1. add Action Listener to your JButton showStats.



    To see does it work you could do this:
    1. Set some initial text in your text Area component.
    2. In you showStatsListener print textString on System.out to see if you've get text from textArea;
    3. Also print amount to see does it work.

Similar Threads

  1. Strange font problem
    By BayPhilip in forum Jobs Discussion
    Replies: 0
    Last Post: 03-01-2011, 10:11 PM
  2. strange problem
    By dinosoep in forum New To Java
    Replies: 6
    Last Post: 05-01-2010, 10:28 AM
  3. Strange Problem
    By Aseem in forum JDBC
    Replies: 5
    Last Post: 02-12-2010, 07:46 AM
  4. Strange problem
    By Tamir in forum Eclipse
    Replies: 1
    Last Post: 08-18-2008, 09:44 PM
  5. What is the answer to this strange problem:
    By willemjav in forum New To Java
    Replies: 5
    Last Post: 08-11-2008, 09:31 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
  •