Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,341 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,614 people online right now. Registration is fast and FREE... Join Now!




JButton Manipulation?

 

JButton Manipulation?

Synth

9 Jan, 2008 - 11:11 AM
Post #1

New D.I.C Head
*

Joined: 29 Aug, 2007
Posts: 44



Thanked: 1 times
My Contributions
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

CODE

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();
    }
}


User is offlineProfile CardPM
+Quote Post


Martyr2

RE: JButton Manipulation?

9 Jan, 2008 - 12:15 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,243



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

My Contributions
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. smile.gif
User is offlineProfile CardPM
+Quote Post

William_Wilson

RE: JButton Manipulation?

9 Jan, 2008 - 02:12 PM
Post #3

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,631



Thanked: 88 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
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/javase/6/docs/api/java...ng/JButton.html
User is online!Profile CardPM
+Quote Post

dontKnowJava

RE: JButton Manipulation?

9 Jan, 2008 - 03:30 PM
Post #4

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 215


My Contributions
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

CODE

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);

    }
}

User is offlineProfile CardPM
+Quote Post

Martyr2

RE: JButton Manipulation?

9 Jan, 2008 - 05:00 PM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,243



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

My Contributions
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. smile.gif
User is offlineProfile CardPM
+Quote Post

jowharshamshiri

RE: JButton Manipulation?

9 Jan, 2008 - 05:08 PM
Post #6

D.I.C Head
**

Joined: 24 Dec, 2007
Posts: 63


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

CODE

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. biggrin.gif

This post has been edited by jowharshamshiri: 9 Jan, 2008 - 05:12 PM
User is offlineProfile CardPM
+Quote Post

dontKnowJava

RE: JButton Manipulation?

9 Jan, 2008 - 05:35 PM
Post #7

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 215


My Contributions
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
User is offlineProfile CardPM
+Quote Post

jowharshamshiri

RE: JButton Manipulation?

9 Jan, 2008 - 05:47 PM
Post #8

D.I.C Head
**

Joined: 24 Dec, 2007
Posts: 63


My Contributions
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: 9 Jan, 2008 - 05:49 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 05:35PM

Live Java Help!

Be Social

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

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month