activate and deactivate ComboBox - java

How can I make the comboBox available when the checkBox was uncheck (vice versa)
Why the comboBox is still disable after I unChecked the checkBox?
choice [] = {"A","B","C"};
JComboBox a = new JComboBox(choice);
JCheckBox chk = new JCheckBox("choice");
...
a.addActionListener(this);
chk.addActionListener(this);
...
public void actionPerformed(ActionEvent e) {
//disable the a comboBox when the checkBox chk was checked
if(e.getSource()==chk)
a.setEnabled(false);
//enable the a comboBox when the checkBox chk was unchecked
else if(e.getSource()!=chk)
a.setEnabled(true);
}

If I understand you correctly I think all that you need to do is to change the enabled state of the combo box based on the current value of the checkbox:
public void actionPerformed(ActionEvent e) {
if (e.getSource()==chk) {
a.setEnabled(chk.isSelected());
}
}

I have a similar set up, and I use an Item Listener, like so:
CheckBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==ItemEvent.SELECTED){
ComboBox.setEnabled(true);
}else if(e.getStateChange()==ItemEvent.DESELECTED){
ComboBox.setSelectedIndex(-1);
ComboBox.setEnabled(false);
}
}
});
This way the behaviour is different when selected and deselected.

I treid this and worked..
public class JF extends JFrame implements ActionListener {
String choice [] = {"A","B","C"};
JComboBox a = new JComboBox(choice);
JCheckBox chk = new JCheckBox("choice");
JF()
{
this.add(a, BorderLayout.NORTH);
this.add(chk, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
a.addActionListener(this);
chk.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//NOTE THE FOLLOWING LINE!!!!
if(e.getSource()==chk)
a.setEnabled(chk.isSelected());
}
public static void main(String[] args) {
new JF().setVisible(true);
}
}
Your old code didn't work because, even unchecking a checkbox triggers the event. The source of the trigger is the checkbox.. so both while checking and unchecking the event source was chk

if (e.getSource() == chckbxModificar) {
if (chckbxModificar.isSelected()) {
cbxImpuesto.setEnabled(true);
cbxMoneda.setEnabled(true);
txtPorcentaje.setEditable(true);
txtSimbolo.setEditable(true);
} else {
cbxImpuesto.setEnabled(false);
cbxMoneda.setEnabled(false);
txtPorcentaje.setEditable(false);
txtSimbolo.setEditable(false);
}
}

Related

How do Display Custom PopupMenu in JComboBox

JComboBox displays a List on click. Instead of the list, I want to display a JPopupMenu.
In the following code the event is triggered but the popup doesnt show up. Why?
JComboBox box = new JComboBox();
box.addPopupMenuListener(new PopupMenuListener() {
#Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
popupMenu.show(box, 0, box.getHeight());
}
...
});
Alternatively one can use a mouseListener. Due to a JDK-bug
https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4144505
one has to add the mouseListener to all Descendants like that:
MouseAdapter comboPopupAdapter = new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
popupMenu.show(box, 0, box.getHeight());
}
};
box.addMouseListener(comboPopupAdapter);
for (Component c : box.getComponents()) {
c.addMouseListener(comboPopupAdapter);
}

Java how to assign id to button and retrieve them?

I'm getting stuck while building a forum like application which has a vote button.
I have vote up and vote down button for each content which are automatically generated. I want this button to only display the up and down arrow but not any text or label.. how can i find out which button is pressed?
Automated content..
ImageIcon upvote = new ImageIcon(getClass().getResource("vote_up.png"));
ImageIcon downvote = new ImageIcon(getClass().getResource("vote_down.png"));
JButton vote_up = new JButton(upvote);
JButton vote_down = new JButton(downvote);
vote_up.addActionListener(voting);
vote_down.addActionListener(voting);
Action voting = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e){
//What to do here to find out which button is pressed?
}
};
any help is appreciated.
public void a(){
int crt_cnt = 0;
for(ClassA temp : listofClassA)
{
b(crt_cnt);
crt_cnt++;
}
}
public void b(crt_cnt){
//draw button
}
As from above, I have multiple vote_up and vote_down button created by the b function, how can i differentiate which crt_cnt is the button from?
There are multiple ways you might achieve this
You could...
Simply use the source of the ActionEvent
Action voting = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == vote_up) {
//...
} else if (...) {
//...
}
}
};
This might be okay if you have a reference to the original buttons
You could...
Assign a actionCommand to each button
JButton vote_up = new JButton(upvote);
vote_up.setActionCommand("vote.up");
JButton vote_down = new JButton(downvote);
vote_down .setActionCommand("vote.down");
//...
Action voting = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e){
if ("vote.up".equals(e.getActionCommand())) {
//...
} else if (...) {
//...
}
}
};
You could...
Take full advantage of the Action API and make indiviual, self contained actions for each button...
public class VoteUpAction extends AbstractAction {
public VoteUpAction() {
putValue(SMALL_ICON, new ImageIcon(getClass().getResource("vote_up.png")));
}
#Override
public void actionPerformed(ActionEvent e) {
// Specific action for up vote
}
}
Then you could simply use
JButton vote_up = new JButton(new VoteUpAction());
//...
Which will configure the button according to the properties of the Action and will trigger it's actionPerformed method when the button is triggered. This way, you know 100% what you should/need to do when the actionPerformed method is called, without any doubts.
Have a closer look at How to Use Actions for more details
You can detect by using the method getSource() of your EventAction
Action voting = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == vote_up ) {
// vote up clicked
} else if (e.getSource() == vote_down){
// vote down clicked
}
}
};
hey thanks for all the help and assistance! I've finally got it! I solved it by
assigning a text on the button, +/- for vote up or down, followed by the content id which i required, then change the font size to 0
vote.setText("+"+thistopic.content.get(crt_cnt).get_id());
vote.setFont(heading.getFont().deriveFont(0.0f));
after that i could easily trace which button is pressed by comparing to the
actionEvent.getActionCommand()
which return the text on the button!
I would wrap the JButton similar to this:
JButton createMyButton(final JPanel panel, final String text,
final boolean upOrDown, final int gridx, final int gridy) {
final JButton button = new JButton();
button.setPreferredSize(new Dimension(80, 50));
final GridBagConstraints gbc = Factories.createGridBagConstraints(gridx,
gridy);
panel.add(button, gbc);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(final ActionEvent e) {
myActionPerformed(text, upOrDown);
}
});
return button;
}
You could use an int instead of the text, if more convenient.

JCheckBox in Java to take of tick in all checkbox pressing the button

I want to take off the tick from all checkbox what I have in GUI, when I pressed on button - dynamically. Is it possible?
JButton clean = new JButton("Clean");
clean.addActionListener(new MyCleanListener());
buttonBox.add(clean);
public class MyCleanListener implements ActionListener{
public void actionPerformed(ActionEvent a){
if(jCheckBox.isSelected())
c.setSelected(false);
}
}
Thank everyone for your help.
public class MyCleanListener implements ActionListener{
public void actionPerformed(ActionEvent a){
for (int i = 0; i < 256; i++){
c = checkboxList.get(i);
c.setSelected(false);
}
}
}
here my decision.
panel.add(box1);
panel.add(box2);
panel.add(clean);
clean.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Component[] components = panel.getComponents();
for (Component component : components) {
if (component instanceof JCheckBox) {
JCheckBox checkBox = (JCheckBox) component;
if(checkBox.isSelected()) {
checkBox.setSelected(false);
}
}
}
}
});
Get all JCheckBox components from the panel and remove selection.
Memorize all your checkboxes that you need unticked in some list or other data structure, then you can iterate through it in your clean listener's action performed method and untick them all...

How to add a JComboBox on an IToolBarManager

I am developing an Eclipse plug in and try to put a JComboBox on an IToolBarManager and add ActionListener to it, so I can handle the JComboBox selection.
Can anyone please help me with that?
There may be a better solution, but I have used the following method:
IToolBarManager mgr = this.getViewSite().getActionBars().getToolBarManager();
IContributionItem comboCI = new ControlContribution("test") {
protected Control createControl(Composite parent) {
final Combo c = new Combo(parent, SWT.READ_ONLY);
c.add("one");
c.add("two");
c.add("three");
c.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
c.add("four");
}
});
return c;
}
};
mgr.add(comboCI);
}

How can I enable/disable my JTextField depending of the state of a JCheckBox?

I have a Java check box next to a text field.
When the check box gets selected, I want the text box to be enabled and when it isn't, I don't want it to be selected. I tried an if statement with the isSelected() method, but it didn't do anything.
How can I react to state changes of the JCheckBox?
Suggestion:
Read the How to Use Check Boxes tutorial
Register an ItemListener for the JCheckBox instance
Compare state change (i.e. getStateChange()) to either ItemEvent.SELECTED, or ItemEvent.DESELECTED, and then appropriately invoke foo.setEnabled, where foo is the JTextBox instance.
Here's an SSCCE:
public final class JCheckBoxDemo {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame("JCheckBox Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(JCheckAndTextPane.newInstance());
frame.setSize(new Dimension(250, 75)); // for demonstration purposes only
//frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static final class JCheckAndTextPane extends JPanel{
private JCheckAndTextPane(){
super();
// Create components
final JTextField textField = new JTextField("Enabled");
final JCheckBox checkBox = new JCheckBox("Enable", true);
checkBox.addItemListener(new ItemListener(){
#Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
textField.setEnabled(true);
textField.setText("Enabled");
}
else if(e.getStateChange() == ItemEvent.DESELECTED){
textField.setEnabled(false);
textField.setText("Disabled");
}
validate();
repaint();
}
});
add(checkBox);
add(textField);
}
public static final JCheckAndTextPane newInstance(){
return new JCheckAndTextPane();
}
}
}
Use the isSelected method.
You then use an ItemListener so you'll be notified when it's checked or unchecked.
And depending on the state of the isSelected method, then you can enable or disable the JTextBox.
// >Click checkbox > Add Event handler > Item > ItemStateChange > Type the code
// chckBox1 is my Variable name
chckBox2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(chckBox2.isSelected()) {
txtAddCandles.setEnabled(true);
}
}
});

Categories

Resources