Results 1 to 10 of 10
Like Tree2Likes
  • 2 Post By Fubarable

Thread: JButton after a click

  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default JButton after a click

    Hello everyone,
    I've got a small problem.
    After I click on a JButton he gets bigger and I don't want it to happens.
    I attached 2 pictures that will demonstrate it.

    And here is also the source code, the code is long cause of the action listeners so don't panic :P

    Java Code:
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.*;
    
    
    public class Game
    {
    	JFrame frame1;
    	JPanel panel1;
    	GridBagConstraints gbc;
    	JButton b1;
    	//removed the other 8 buttons
    	boolean flag;
    	
    	public Game()
    	{
    		initialization();
    		addListeners();
    		addItems();
    	}
    	public void initialization()
    	{
    		frame1 =  new JFrame("Tic Tac Toe - Imri Persiado");
    		panel1 = new JPanel(new GridBagLayout());
    		gbc = new GridBagConstraints();
    		b1 = new JButton("");
    		//removed the other 8 buttons
    		flag = false;
    	}
    	public void addListeners()
    	{
    		frame1.addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				System.exit(0);
    			}
    		});
    		b1.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				if(flag)
    				{
    					b1.setText("X");
    					flag = false;
    				}
    				else
    				{
    					b1.setText("O");
    					flag = true;
    				}
    			}
    		});
    		//removed the other 8 buttons
    	}
    	public void addItems()
    	{
    		gbc.fill = GridBagConstraints.HORIZONTAL;
    		gbc.ipady = 170;
    		gbc.ipadx = 170;
    		
    		
    		gbc.gridx = 0;
    		gbc.gridy = 0;
    		panel1.add(b1,gbc);
    		
    		//removed the other 8 buttons
    		
    		frame1.add(panel1);
    		frame1.setLocationRelativeTo(null);
    		frame1.pack();
    		frame1.setVisible(true);
    		
    	}
    	public static void main(String[] args)
    	{
    		Game g = new Game();
    	}
    
    }
    Attached Thumbnails Attached Thumbnails JButton after a click-clean.png   JButton after a click-after.png  
    Last edited by tnrh1; 12-07-2011 at 03:40 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default Re: JButton after a click

    Quote Originally Posted by tnrh1 View Post
    And here is also the source code, the code is long cause of the action listeners so don't panic :P
    For better help sooner, you should post an SSCCE that demonstrates the problem. Your problem is only with one JButton at a time, right? So your code should only contain one JButton. Make it as easy as possible for people to help you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: JButton after a click

    I edited the code, it's more readable now.

    edit: looks like the font size effect those empty spaces which makes that wrost since in the example I attached the text was very small and I'm planning to use a much bigger font.
    Last edited by tnrh1; 12-07-2011 at 03:45 PM.

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

    Default Re: JButton after a click

    I'm curious why you're using a GridBagLayout when a GridLayout would work better to hold the buttons and to force them to be a certain stable size.
    KevinWorkman and tnrh1 like this.

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: JButton after a click

    Because I have never worked with layouts before, and I'm still on a learning process.
    Anyway thanks for clearing this up for me.
    As usual one problem leads me to another, I would like to change the 4th row size.
    Attached Thumbnails Attached Thumbnails JButton after a click-.png  

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: JButton after a click

    I would like to change the 4th row size.
    Again, you have a layout issue. So you need to use a proper combination of layout managers.

    You need one panel that uses the GridLayout with the 9 button. Maybe you add that to the CENTER of your content pane which uses a BorderLayout by default.

    Then you can add a JLabel that displays the text to the SOUTH. Or maybe you use a JPanel with a FlowLayout that displays multiple labels.

    Then you pack() the frame and everything will be displayed at its proper size.

  7. #7
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: JButton after a click

    I never thought about it that way, thanks.
    Anyway I got a panel that contains all the buttons(panel1) and a panel that contains a single label(panel2).
    panel1 layout is gridbaglayout and panel2 I didn't set anything.

    I created a mainPanel that will hold my both panels as I want to(mainPanel) and his layout is gridlayout it looks somthing like that:
    Java Code:
    mainPanel.add(panel1); 
    mainPanel.add(panel2);
    frame1.add(mainPanel);
    frame1.pack();
    frame1.setVisible(true);
    But unfortunately the panel2 size is somthing like ScreenHeight-panel1Height.
    I tried to change it but nothing happend:
    Attached Thumbnails Attached Thumbnails JButton after a click-.png  

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: JButton after a click

    I created a mainPanel that will hold my both panels as I want to(mainPanel) and his layout is gridlayout
    Well, thats not what I suggested now is it? A GridLayout makes all components the same size.

    Read my suggestion again.

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

    Default Re: JButton after a click

    RTF tutorials. Please.

  10. #10
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: JButton after a click

    Ok, some changes:
    Panel1 - GridLayout (to get all the buttons in the same size).
    Panel2 - no layout is set(I got a single label in it, there is nothing to manage in there ..)
    MainPanel - GridBagLayout(To be able to change the sizes of every row).

    MainPanel
    - Panel1 is in the first row and column (0,0)
    - Panel2 is in the second row and first colum (0,1)

    I played alittle bit with the GridBagConstraints instance to change every row hight.
    The first row(panel1) should be somthing like 200 pixels and the second row(panel2, the single label) should be something like 10 pixels.

    In the picture I attached I used a button instead of a label so we can see the component border.
    Every time I change the second row height(the panel2 which contains the button) it changes only the button height and I keep getting empty space.
    I guess it's an empty space between panel1 to panel2:
    Attached Thumbnails Attached Thumbnails JButton after a click-.png  

Similar Threads

  1. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  2. Replies: 2
    Last Post: 06-22-2011, 01:19 PM
  3. JButton click
    By mine0926 in forum New To Java
    Replies: 0
    Last Post: 07-30-2010, 04:50 AM
  4. JButton click problem
    By mine0926 in forum NetBeans
    Replies: 7
    Last Post: 06-11-2010, 03:00 AM
  5. how to click a jbutton and open an url
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:44 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
  •