School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,029 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,094 people online right now. Registration is fast and FREE... Join Now!



JButton Manipulation?

JButton Manipulation? Rate Topic: -----

#1 Synth  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 44
  • Joined: 29-August 07


Dream Kudos: 0

Post icon  Posted 09 January 2008 - 11:11 AM

how would you manipulate JButton? such as changing the buttons to specific locations or resizing the buttons?
I've looked around but sites have just said how to use it. plz and ty

import java.awt.*;
import javax.swing.*;

public class calcprgrm1 extends JFrame
{
	JTextField field = new JTextField(25);
	JButton button1 = new JButton("C");//buttons wanted to manipulate
	JButton button2 = new JButton("CE");
	JButton button3 = new JButton("");
	
	public calcprgrm1()
	{
		super("JFrame with Panels");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel pane = new JPanel();
		JPanel pane2 = new JPanel();
		Container con = getContentPane();
		con.setLayout(new FlowLayout());
		con.add(pane);
		con.add(pane2);
		pane.add(field);
		pane2.add(button1);
		pane2.add(button2);
		pane2.add(button3)
		setSize(300, 250);
		setVisible(true);
	}

	public static void main(String[] args)
	{
		calcprgrm1 panel = new calcprgrm1();
	}
}


Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 7,676
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Posted 09 January 2008 - 12:15 PM

Take a look at the methods "setLocation" and "setBounds" inherited from Component... which a JButton is part of. These work when you have set no layout manager (aka setLayout(null)). Otherwise some components are determined by the type of layout you have. You will have to check out the documentation on the type of layout manager you are using to see what you can do about size. One great method you could also look at is "setPreferredSize" which works with some layouts like box layout etc.

Hope this helps. :)
Was This Post Helpful? 0
  • +
  • -

#3 William_Wilson  Icon User is offline

  • lost in compilation
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 4,692
  • Joined: 23-December 05


Dream Kudos: 3275

Expert In: Java, C, Javascript

Posted 09 January 2008 - 02:12 PM

yes, when dealing with more complicated layouts, using setSize and setPreferredsize are ideal. It is often a good idea to use setpreferredsize anyway, in case there is a resizing issue or alignment problem, java will do it's best to accommodate those settings.

Sun.com Documentation on JButton:
http://java.sun.com/...ng/JButton.html
Was This Post Helpful? 0
  • +
  • -

#4 dontKnowJava  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 215
  • Joined: 29-September 07


Dream Kudos: 0

Posted 09 January 2008 - 03:30 PM

hey i was just playing with setLocation() just to see how that works. i usually used layout manager to position but im not getting the result im looking for. the button doesnt even appear. the question is if i have no layout where does the button go when you add it? heres what i got

class Test2 extends JFrame
{
	JButton but = new JButton("Button");
	JPanel pan = new JPanel();

	public Test2()
	{
		setLayout(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(200, 200);
		pan.add(but);
		but.setLocation(10, 10);
	}


public static void main(String args[])
	{
		Test2 t = new Test2();
		t.setVisible(true);

	}
}


Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 7,676
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Posted 09 January 2008 - 05:00 PM

Well for one you are not adding the panel to the form. I will go into this in more depth when I get home later tonight. :)
Was This Post Helpful? 0
  • +
  • -

#6 jowharshamshiri  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 65
  • Joined: 24-December 07


Dream Kudos: 0

Posted 09 January 2008 - 05:08 PM

i dont see why you use a jpanel. just set the layout to null as the other guy said and youre good to go.

import javax.swing.*;
public class myClass extends JFrame{
	JButton but = new JButton("Button");
	public myClass(){
		setLayout(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(200, 200);
		this.add(but);
		but.setLocation(100, 10);
		but.setSize(50, 50);
	}
	public static void main(String args[]){
		myClass t = new myClass();
		t.setVisible(true);
	}
}



ps: martyr2 is right. in your code you dont add the jpanel to your form.
ps #2: thanks for answering martyr2 but you keep surfing the internet while youre at work and youre gonna get fired. take care. :D

This post has been edited by jowharshamshiri: 09 January 2008 - 05:12 PM

Was This Post Helpful? 0
  • +
  • -

#7 dontKnowJava  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 215
  • Joined: 29-September 07


Dream Kudos: 0

Posted 09 January 2008 - 05:35 PM

ah, ok. i was told in class that components go on a panel and then the panel gets added to the frame. guess it works just as well without the panel. thanks
Was This Post Helpful? 0
  • +
  • -

#8 jowharshamshiri  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 65
  • Joined: 24-December 07


Dream Kudos: 0

Posted 09 January 2008 - 05:47 PM

keep in mind if you want your code to be backward compatible you shouldnt add stuff directly to your form. the guys at the class youre attending are right. this feature is available since j2se 5. so my code is not going to work with compilers before that.

ps: follow the routine of adding to jpanel first. thats going to help you alot when you work with many components.

This post has been edited by jowharshamshiri: 09 January 2008 - 05:49 PM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month