Results 1 to 7 of 7
- 09-10-2011, 01:58 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 3
- Rep Power
- 0
How to implement JCheckBox inside JButton?
I was trying some solutions, and every of them failed. Until now, I had thought that anything is possible in Java. One of my solution, that don't makes compilation warrning and runs (but checkbox is missing anyway) is:
thenJava Code:class myCheckBox extends JCheckBox implements Icon { int w = 20; public myCheckBox() { super(); setSelected(true); setFocusPainted(false); setOpaque(false); setToolTipText(" close route "); addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { } } }); } public void paintIcon(Component c, Graphics g, int x, int y) { } public int getIconWidth() { return w; } public int getIconHeight() { return w; } }
Any tryings of creating own button class with own content manager fails as well. Any ideas to solve this?Java Code:myCheckBox CB = new myCheckBox(); button.setIcon(CB);
- 09-10-2011, 05:16 AM #2
Re: How to implement JCheckBox inside JButton?
You have a class that implements Icon but has an empty paintIcon(...) method. Doesn't make sense, that.
Describe the user interaction with your custom composite component. What happens on button click? What determines whether the check box will be selected / unselected?
Without knowing that, I can't make any design suggestion.
db
- 09-10-2011, 06:23 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: How to implement JCheckBox inside JButton?
Not sure I understand the usage of this component either, but maybe Component Border « Java Tips Weblog will work for you.
- 09-10-2011, 06:42 AM #4
Re: How to implement JCheckBox inside JButton?
Here's one interpretation, assuming the checked status is separately set externally.
dbJava Code:import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class CheckButtonDemo { public static void main(String[] args) throws Exception { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new CheckButtonDemo().makeUI(); } }); } public void makeUI() { final CheckButton checkButton = new CheckButton("TSP ROUTE"); JFrame frame = new JFrame(); frame.setLayout(new FlowLayout()); frame.add(checkButton); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); new Timer(750, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { checkButton.setChecked(!checkButton.isChecked()); } }).start(); } } class CheckButton extends JButton { private JCheckBox checkBox = new JCheckBox(); public CheckButton(String text) { super(text); setHorizontalTextPosition(LEFT); setIcon(new Icon() { private Icon checkIcon = UIManager.getIcon("CheckBox.icon"); @Override public void paintIcon(Component c, Graphics g, int x, int y) { checkIcon.paintIcon(checkBox, g, x, y); } @Override public int getIconWidth() { return checkIcon.getIconWidth(); } @Override public int getIconHeight() { return checkIcon.getIconHeight(); } }); } public void setChecked(boolean checked) { checkBox.setSelected(checked); repaint(); } public boolean isChecked() { return checkBox.isSelected(); } }
- 09-10-2011, 08:22 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 3
- Rep Power
- 0
Re: How to implement JCheckBox inside JButton?
DarrylBruke, your answer source is greatly valuable for my experience. In addition, I had received solution unbelivable trivial from other forum...
Thank You guys, advanced handling Icon solution teached me something as well. Good day!Java Code:JCheckBox jcb = new JCheckBox(); button.add(jcb);
- 09-10-2011, 04:32 PM #6
Re: How to implement JCheckBox inside JButton?
You still haven't answered this:
dbDescribe the user interaction with your custom composite component. What happens on button click? What determines whether the check box will be selected / unselected?
- 09-10-2011, 11:44 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 3
- Rep Power
- 0
Re: How to implement JCheckBox inside JButton?
Its about using Travelling Salesman Algorithm etc; there is limited space on toolbar, and checkbox role is, when its unselected, route is not closing at final connection/in kruskal's or prim's version last connection is being made as addition, which in some cases of application needs is not compulsory
sorry for my english if its not perfect
edit:
while ive learned that all components in java are containers, simple adding jchkbox to jbutton makes both accessible by user, and works great. Thanks again for Your inquiryLast edited by s1w; 09-10-2011 at 11:46 PM.
Similar Threads
-
Jbutton inside a jtable wont fire action
By giskard in forum AWT / SwingReplies: 5Last Post: 08-02-2011, 08:07 PM -
Getting text from inside a jeditorpane inside a jtabbedpane
By captain alge in forum New To JavaReplies: 9Last Post: 04-12-2011, 07:26 PM -
How can I add JCheckBox to each row?
By batya in forum AWT / SwingReplies: 1Last Post: 11-04-2009, 09:25 PM -
How To Add A Jbutton Inside A Rectangle
By SANDY_INDIA in forum AWT / SwingReplies: 9Last Post: 07-06-2008, 08:06 AM -
using JCheckBox, JButton and JTextArea with JDBC
By warship in forum JDBCReplies: 1Last Post: 08-08-2007, 01:25 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks