Results 1 to 11 of 11
  1. #1
    AlphaScorpii is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default problems with program - cards show up only when I minimize/resize window

    I'm making 1 program with poker cards. I got 2 problems:

    1. When I click JButton "generate" nothing happens but when I click minimize of the program window or just click on the edge of the window cards show up.

    2. When I click "generate" again program doesn't generate new cards.

    I've been using same code for ActionListener like I used with another program when I was making simple calculator and there was everything fine. Also around words of "generate" button there is rectangle and on calculator buttons there was no rectangle.

    Here is my main class ("generate" button in code is "Generiraj random karte"):

    Java Code:
    package glavni;
    
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.Spring;
    import javax.swing.SpringLayout;
    
    public class Main extends JFrame {
    
    	private static final long serialVersionUID = 1L;
    
    	public static void main(String[] args) {
    		
    		final JFrame okvir = new JFrame("Evaluacija poker ruku");
    		
    		okvir.setLayout(new SpringLayout());
    		okvir.setPreferredSize(new Dimension(780, 652));
    		
    		final JButton generiraj = new JButton();
    		generiraj.setText("Generiraj random karte");
    		
    		ImageIcon back = new ImageIcon ("Karte/back.gif");
    		
    		
    		final JButton prva = new JButton();
    		prva.setIcon(back);
    		final JButton druga = new JButton();
    		druga.setIcon(back);
    		final JButton treca = new JButton();
    		treca.setIcon(back);
    		final JButton cetvrta = new JButton();
    		cetvrta.setIcon(back);
    		final JButton peta = new JButton();
    		peta.setIcon(back);
    		
    		okvir.add (new JLabel());
    		okvir.add (generiraj, new SpringLayout.Constraints(Spring.constant(20), Spring.constant(20), Spring.constant(200), Spring.constant(40)));
    		okvir.add (prva, new SpringLayout.Constraints(Spring.constant(10), Spring.constant(80), Spring.constant(140), Spring.constant(196)));
    		okvir.add (druga, new SpringLayout.Constraints(Spring.constant(160), Spring.constant(80), Spring.constant(140), Spring.constant(196)));
    		okvir.add (treca, new SpringLayout.Constraints(Spring.constant(310), Spring.constant(80), Spring.constant(140), Spring.constant(196)));
    		okvir.add (cetvrta, new SpringLayout.Constraints(Spring.constant(460), Spring.constant(80), Spring.constant(140), Spring.constant(196)));
    		okvir.add (peta, new SpringLayout.Constraints(Spring.constant(610), Spring.constant(80), Spring.constant(140), Spring.constant(196)));
    		
    		okvir.pack();
    		okvir.setVisible(true);
    		
    		
    		class GenerirajListener implements ActionListener {
    			public void actionPerformed (ActionEvent e){
    			
    				final List<Karta> spil = new ArrayList<Karta>();
    				
    				String snagaTemp = null;
    				String bojaTemp = null;
    				String k = null;
    				
    				for (int i = 1; i<5; i++){
    					
    					for (int j = 2; j<14; j++){
    							
    						Karta karta = new Karta(j, i, null);
    						
    						switch( i ) {
    						  case 1: bojaTemp = "s"; break;
    						  case 2: bojaTemp = "d"; break;
    						  case 3: bojaTemp = "h"; break;
    						  case 4: bojaTemp = "c"; break;
    						}
    						
    						switch( j ) {
    						  case 2: snagaTemp = "2"; break;
    						  case 3: snagaTemp = "3"; break;
    						  case 4: snagaTemp = "4"; break;
    						  case 5: snagaTemp = "5"; break;
    						  case 6: snagaTemp = "6"; break;
    						  case 7: snagaTemp = "7"; break;
    						  case 8: snagaTemp = "8"; break;
    						  case 9: snagaTemp = "9"; break;
    						  case 10: snagaTemp = "T"; break;
    						  case 11: snagaTemp = "J"; break;
    						  case 12: snagaTemp = "Q"; break;
    						  case 13: snagaTemp = "K"; break;
    						  case 14: snagaTemp = "A"; break;
    						}
    						
    						k = "Karte/"+snagaTemp+bojaTemp+".gif";
    						karta.setImg(k);
    						
    						spil.add(karta);
    						
    					}
    								
    				}
    				
    				Collections.shuffle(spil);
    				
    				ImageIcon generirana1Icon = new ImageIcon (spil.get(0).getImg());
    				ImageIcon generirana2Icon = new ImageIcon (spil.get(1).getImg());
    				ImageIcon generirana3Icon = new ImageIcon (spil.get(2).getImg());
    				ImageIcon generirana4Icon = new ImageIcon (spil.get(3).getImg());
    				ImageIcon generirana5Icon = new ImageIcon (spil.get(4).getImg());
    				
    				final JButton prvaGenerirana = new JButton();
    				prvaGenerirana.setIcon(generirana1Icon);
    				final JButton drugaGenerirana = new JButton();
    				drugaGenerirana.setIcon(generirana2Icon);
    				final JButton trecaGenerirana = new JButton();
    				trecaGenerirana.setIcon(generirana3Icon);
    				final JButton cetvrtaGenerirana = new JButton();
    				cetvrtaGenerirana.setIcon(generirana4Icon);
    				final JButton petaGenerirana = new JButton();
    				petaGenerirana.setIcon(generirana5Icon);
    				
    				okvir.remove(prva);
    				okvir.remove(druga);
    				okvir.remove(treca);
    				okvir.remove(cetvrta);
    				okvir.remove(peta); 
    			
    				okvir.add (prvaGenerirana, new SpringLayout.Constraints(Spring.constant(10), Spring.constant(80), Spring.constant(140), Spring.constant(196)));
    				okvir.add (drugaGenerirana, new SpringLayout.Constraints(Spring.constant(160), Spring.constant(80), Spring.constant(140), Spring.constant(196)));
    				okvir.add (trecaGenerirana, new SpringLayout.Constraints(Spring.constant(310), Spring.constant(80), Spring.constant(140), Spring.constant(196)));
    				okvir.add (cetvrtaGenerirana, new SpringLayout.Constraints(Spring.constant(460), Spring.constant(80), Spring.constant(140), Spring.constant(196)));
    				okvir.add (petaGenerirana, new SpringLayout.Constraints(Spring.constant(610), Spring.constant(80), Spring.constant(140), Spring.constant(196)));	
    				
    			}
    		}
    		
    		GenerirajListener gl = new GenerirajListener();
    		generiraj.addActionListener(gl);
    			
    	}
    
    }
    Last edited by Fubarable; 07-02-2011 at 07:31 PM. Reason: Moderator Edit: code tags added

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

    Default

    Any time you add/remove components from a container you should call revalidate() on the container (if it derives from JComponent) and then repaint(). The first call tells the layout managers to layout the components present and the second tells the container to repaint itself and its children (and borders). Since you're adding to the JFrame, you're actually adding to its contentPane which is a Container variable but is almost always a JPanel. So to fix this, call

    Java Code:
    // after adding new components onto okvir...
    
    ((JPanel)okvir.getContentPane()).revalidate(); // cast it to a JPanel so we can call revalidate
    okvir.repaint();
    Also, I added code tags to your post to help the code retain its formatting. To see how to do this yourself for your next posts, please see the link in my signature below.
    Last edited by Fubarable; 07-02-2011 at 08:21 PM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    Um,okvir is a JFrame.

    @Revalidate the JFrame's contentPane.

    db

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

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

    Default

    Quote Originally Posted by DarrylBurke View Post
    Um,okvir is a JFrame.

    @Revalidate the JFrame's contentPane.

    db
    Oops. Edited. Thanks

  6. #6
    AlphaScorpii is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default ...

    Tried

    Java Code:
    ((JFrame)okvir.getContentPane()).revalidate();
    but Xbulb says

    "The method revalidate is undefined for the type JFrame."

    I got option to validate and invalidate. I tried to validate when I made first components and then revalidate after second but got same message.

    Also when saving/running I'm getting message:

    "Save All Failed
    java.lang.NullPointerException"
    Last edited by AlphaScorpii; 07-02-2011 at 09:53 PM.

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

    Default

    Quote Originally Posted by AlphaScorpii View Post
    Tried

    Java Code:
    ((JFrame)okvir.getContentPane()).revalidate();
    but Xbulb says
    I don't see anywhere in our replies where we suggest that you cast the contentPane as a JFrame. Rather we said JPanel. Please check if your code is correct first.


    Also when saving/running I'm getting message:

    "Save All Failed
    java.lang.NullPointerException"
    So you've got another unrelated error. Best find out which line is throwing this exception and then checking to see which variable is null and why. You've got some debugging to do.

  8. #8
    AlphaScorpii is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I don't see anywhere in our replies where we suggest that you cast the contentPane as a JFrame. Rather we said JPanel. Please check if your code is correct first.
    Sry, from your messaging I wrongly understood that you wrote JPanel wrong and that it should go JFrame... but anyway I tried both but for JPanel I get message: "JPanel cannot be resolved to a type".

    Quote Originally Posted by Fubarable View Post
    So you've got another unrelated error. Best find out which line is throwing this exception and then checking to see which variable is null and why. You've got some debugging to do.
    Problem is that I don't get information which line makes problem. Althought I get this message work is saved...

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

    Default

    Quote Originally Posted by AlphaScorpii View Post
    Sry, from your messaging I wrongly understood that you wrote JPanel wrong and that it should go JFrame... but anyway I tried both but for JPanel I get message: "JPanel cannot be resolved to a type".
    Have you imported the JPanel class? You should probably read up on importing classes and other Java basics before trying to tackle Swing as you may be trying to run before you can walk. You can find out more on it here: Creating and Using Packages


    Problem is that I don't get information which line makes problem. Althought I get this message work is saved...
    Are you sure about this? That would be a very unusual NullPointerException message if it didn't give you the line numbers that are involved. If still having a problem, post the actual exception message lines here.

  10. #10
    AlphaScorpii is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Have you imported the JPanel class? You should probably read up on importing classes and other Java basics before trying to tackle Swing as you may be trying to run before you can walk. You can find out more on it here: Creating and Using Packages
    I didn't import JPanel... I don't have much time for learning, I have to be done soon and practicly everything is working except this 2 problems.

    Quote Originally Posted by Fubarable View Post
    Are you sure about this? That would be a very unusual NullPointerException message if it didn't give you the line numbers that are involved. If still having a problem, post the actual exception message lines here.
    Fixed this. Problem was in class where I had comparator and made wrong compare...


    btw. I tried with repaint only but only change when I click "generate" is that now back of the cards is removed but I still need to click on the edge of the window for cards to show up.

  11. #11
    AlphaScorpii is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    EDIT:

    Ok everything is working fine for now
    Last edited by AlphaScorpii; 07-03-2011 at 04:17 PM.

Similar Threads

  1. FrameView doesn't show new JPanel until I resize
    By metazone in forum SWT / JFace
    Replies: 3
    Last Post: 01-05-2012, 03:52 PM
  2. Replies: 6
    Last Post: 11-22-2010, 11:55 AM
  3. Resize shell on show/hide controls.
    By spacetoha in forum SWT / JFace
    Replies: 1
    Last Post: 03-24-2009, 12:20 PM
  4. Window resize icon...
    By pele in forum SWT / JFace
    Replies: 3
    Last Post: 06-09-2008, 08:31 AM
  5. Replies: 1
    Last Post: 02-16-2008, 11:43 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
  •