Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Displaying small buttons

  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Displaying small buttons

    Hello.
    What did you mean by (1,2)?
    You will be have to mention the width and height respectively. By doing so you are telling the JVM to set the width to 1 pixel and height to 2 pixels.
    Thats a meaningless size.

    Syed.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Need help editing the size of a JButton!?!?

    Quote Originally Posted by syedbhai View Post
    You will be have to mention the width and height respectively. By doing so you are telling the JVM to set the width to 1 pixel and height to 2 pixels.
    Thats a meaningless size.

    Syed.
    1 px wide and 2 px high are valid sizes for a component. (0, 0) is a valid size. The method does not deal with size in pixels, it takes a Dimension as the only parameter.

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Need help editing the size of a JButton!?!?

    jps,
    I just experimented with what you have said. Its incorrect.
    I created one JButton object and added it to a JFrame object.
    When I invoked setPreferredSize(new Dimension(1,2)) the button component didn't get displayed.
    When I tried new Dimension(40,20) it got displayed.
    Let me know if I am still wrong.

    Syed.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Need help editing the size of a JButton!?!?

    /**TestButton.java */
     
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
     
    public class TestButton extends JPanel implements ActionListener {
     
    	private static final long	serialVersionUID	= 1L;
     
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				TestButton tb = new TestButton();
    				tb.createAndShowGUI();
    			}
    		});
    	}
     
    	public TestButton() {
    		this.setPreferredSize(new Dimension(40, 40));
    		String commandTesting = "testing";
    		JButton button = new JButton(commandTesting);
    		button.setPreferredSize(new Dimension(1,2));
    		button.addActionListener(this);
    		this.add(button);
    	}
     
    	public void createAndShowGUI() {
    		JFrame frame = new JFrame("TestButton");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(this);
    		frame.pack();
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent ae) {
    		System.out.println(ae.getActionCommand());
    	}
     
    }
    As small as 1 pixel by two pixels may be, it is still drawn, still present, and still functional.

    --- Update ---

    Off topic discussion moved to a new thread

Similar Threads

  1. Small problem, help please?
    By Jonathansafc in forum What's Wrong With My Code?
    Replies: 33
    Last Post: September 3rd, 2011, 06:46 PM
  2. Small help needed
    By javabeg in forum Java Theory & Questions
    Replies: 4
    Last Post: March 15th, 2011, 09:50 AM
  3. keylistener small problem
    By matecno in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2010, 07:51 PM
  4. Small Project
    By 3XiLED in forum Paid Java Projects
    Replies: 7
    Last Post: March 1st, 2010, 07:35 AM
  5. small problem ... I need help
    By Ashar in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2009, 10:26 AM