6 Replies - 410 Views - Last Post: 14 May 2012 - 05:17 PM Rate Topic: -----

#1 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

How do I add another button (2 in total) to this JFrame?...

Posted 14 May 2012 - 03:13 PM

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonFrame1 extends JFrame {
	
	
	private static final int FRAME_WIDTH = 100;
	private static final int FRAME_HEIGHT = 60;
	
	public ButtonFrame1()
	{
		createComponents();
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
	}
	
	private void createComponents() // Event source
	{
		 JButton button = new JButton("Click me!");
		 JPanel panel = new JPanel();
		 panel.add(button);
		 add(panel);
		 
		 ActionListener listener = new ClickListener();
		 button.addActionListener(listener);
		 
	}
	
}



import javax.swing.JFrame;

public class ButtonViewer {


	public static void main(String[] args) {
		JFrame frame = new ButtonFrame1();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ClickListener implements ActionListener {

		public void actionPerformed(ActionEvent event) {
			System.out.println("I was clicked");
		}
		
}


Hi. I have one button working already, but am having problems adding another. I've tried adding another JButton (JButton button2 = new JButton("No, click me!");) and then adding it to the panel (panel.add(button2)) to the createComponents method, but it doesn't seem to be working. Do I need to create an entirely different class for a new button?

Is This A Good Question/Topic? 0
  • +

Replies To: How do I add another button (2 in total) to this JFrame?...

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10825
  • View blog
  • Posts: 40,363
  • Joined: 27-December 08

Re: How do I add another button (2 in total) to this JFrame?...

Posted 14 May 2012 - 03:26 PM

By default, JFrame uses BorderLayout. So adding a Component to the JFrame will add it to the CENTER constraint, unless you specify another constraint in the add() method. You can do a couple things:
-Change the LayoutManager of the JFrame
-Create a JPanel and use that as your main JPanel. Set its LayoutManager appropriately. Add all your Components to the JPanel, then add that to the JFrame.
Was This Post Helpful? 1
  • +
  • -

#3 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 49
  • View blog
  • Posts: 273
  • Joined: 02-February 09

Re: How do I add another button (2 in total) to this JFrame?...

Posted 14 May 2012 - 03:28 PM

Tried out your code. Creating a new button and adding it to the panel in the createComponents method worked like a charm.:) I don't see your issue
Was This Post Helpful? 1
  • +
  • -

#4 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: How do I add another button (2 in total) to this JFrame?...

Posted 14 May 2012 - 03:56 PM

View Postmacosxnerd101, on 14 May 2012 - 03:26 PM, said:

By default, JFrame uses BorderLayout. So adding a Component to the JFrame will add it to the CENTER constraint, unless you specify another constraint in the add() method. You can do a couple things:
-Change the LayoutManager of the JFrame
-Create a JPanel and use that as your main JPanel. Set its LayoutManager appropriately. Add all your Components to the JPanel, then add that to the JFrame.


Thank you Sir. I will try this.

View Postoha055, on 14 May 2012 - 03:28 PM, said:

Tried out your code. Creating a new button and adding it to the panel in the createComponents method worked like a charm.:) I don't see your issue


Really? How? I just tried creating a new object of JButton, but it just replaced the button above it.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: How do I add another button (2 in total) to this JFrame?...

Posted 14 May 2012 - 04:01 PM

View PostTechSyndrome, on 14 May 2012 - 06:56 PM, said:

Really? How? I just tried creating a new object of JButton, but it just replaced the button above it.

Not a JPanel that has a FlowLayout by default
you can

panel.add(new JButton("Btn1"));
panel.add(new JButton("Btn2"));
panel.add(new JButton("Btn3"));
....
add(panel);

without problem
Was This Post Helpful? 1
  • +
  • -

#6 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: How do I add another button (2 in total) to this JFrame?...

Posted 14 May 2012 - 04:08 PM

View Postpbl, on 14 May 2012 - 04:01 PM, said:

View PostTechSyndrome, on 14 May 2012 - 06:56 PM, said:

Really? How? I just tried creating a new object of JButton, but it just replaced the button above it.

Not a JPanel that has a FlowLayout by default
you can

panel.add(new JButton("Btn1"));
panel.add(new JButton("Btn2"));
panel.add(new JButton("Btn3"));
....
add(panel);

without problem


Wow! It worked! But I don't understand, I thought when I created the JButton object in the createComponents method and I had passed in the parameters "Click me!' that the object would be used up and that I would have to create another one? What is the reason that I can create more buttons below? Also how would I create a different action for this new button? So for example, currently when I click "Click me!" the method is called and returns "I was clicked", however, with my new button "Click me not!" I am unsure how to implement a different method for this...
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: How do I add another button (2 in total) to this JFrame?...

Posted 14 May 2012 - 05:17 PM

Defined them in instance variable then it is easy

       JButton btn1, btn2, btn3;
	
	private void createComponents() // Event source
	{
		 btn1 = new JButton("Click me!");
		 btn2 = new JButton("Bit me!");
		 btn3 = new JButton("Kiss me!");
		 JPanel panel = new JPanel();
		 panel.add(btn1);
		 panel.add(btn2);
		 panel.add(btn3);
		 add(panel);
		 
		 ActionListener listener = new ClickListener();
		 btn1.addActionListener(listener);
		 btn2.addActionListener(listener);
		 btn3.addActionListener(listener);
		 
	}



        public class ClickListener implements ActionListener {
             public void actionPerformed(ActionEvent event) {
                 Object o = e.getSource();
                 if(o == btn1) {
                     ...
                     return;
                 }
                 if(o == btn2) {
                     ...
                     return;
                 }
                 if(o == btn3) {
                     ...
                     return;
                 }


}



Was This Post Helpful? 1
  • +
  • -

Page 1 of 1