This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java jbutton  RSS feed

Jeff Barker
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a college student and trying to use layOut to put one jbutton inside another jbutton. Below is the part of the code I am struggling with.
I filled in blank one with < new FlowLayout() >
The second Blank "whole line" < outer.add(inner); >
The third blank I put < outer >
Can someone help me. I don't get it!!

frame = new JFrame("A button in a button");
frame.setSize(300, 200);
outer = new JButton("Outer");
outer.addActionListener(new OuterListener());
inner = new JButton("Inner");
inner.addActionListener(new InnerListener());
frame.getContentPane().add(__________, BorderLayout.CENTER);
message = new JLabel("Click a button...");
frame.getContentPane().add(message, BorderLayout.SOUTH);
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can add a button to a button.

it wouldn't be too hard to make a jpg image of your 'inner' button,
then add that to your 'outer' button as the icon.

its also not that hard to separate the actions of the two.
Vasu Reddy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi you can add Buttons to ToggleButton, below is the sample code.


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

public class Button1 extends JFrame {

public static void main(String args[]) {
new Button1();

Button1() {//constructor
JToggleButton power = new JToggleButton();//container
power.setLayout(new FlowLayout());
JButton am = new JButton("AM");//another button
JButton fm = new JButton("FM");//another button

power.add(am);//Put button in container button
power.add(new JLabel("Power"));//Put label in button
power.add(fm);//Put button in container button

getContentPane().add(power);//pub button in frame

String plafClassName = //set look and feel

}catch(Exception ex){

getContentPane().setLayout(new FlowLayout());
setTitle("Buttons in Buttons");

// Inner WindowListener class to terminate program
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);}});//end WindowListener

}//end constructor

}//end class
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone needs to ask the ovbious question here: why? Why do you want to put one button inside another? How do you want it to behave?
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a good guestion. What is the application?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!