Can we enable a jcomboBox according to a click on an item of another jcomboBox after the execution of our jFrame? is it possible ?
JComboBox comboBox_1 = new JComboBox();
comboBox_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ind = comboBox_1.getSelectedIndex();
System.out.println(ind);
if(ind==1) {
comboBox_4.setEnabled(true);
}
} catch (Exception e1) {
}
});
Related
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);
}
}
I have a JCombobox in which when I select any one from drop down list of JCombobox ,the selected item is opening but when I click on "Custom" among one of the drop down list I have to open a daiolg ,here daiolg is opening but drop down list is not closing I want to hide the drop down when I click on Custom. here is my sample code....
private PropertyChangeSupport pcs;///here Iam using ActionListener and PopupMenuListener
public void actionPerformed(ActionEvent ae){
if(ae.getSource() instanceof ComboBox )
{
ComboBox comboBox = (ComboBox)ae.getSource();
Object selectedItem = comboBox.getSelectedItem();
if(selectedItem != null && (!selectedItem.equals("(Custom..)")))
{
pcs.firePropertyChange("ITEM_SELECTED",getCaption(),null);
}}}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
{
ComboBox comboBox = (ComboBox)e.getSource();
Object repeatedSelectedItem = comboBox.getSelectedItem();
if(repeatedSelectedItem != null && repeatedSelectedItem.equals("(Custom..)"))
{
invokeCustomFilterDialog(repeatedSelectedItem, comboBox);
}}
private void invokeCustomFilterDialog(Object repeatedSelectedItem, ComboBox comboBox)
{
customFilterDialog.showDialog(); //here Iam opening dailog...
if(customFilterDialog.isCustomFilterAppliedFlag() == true)
{
pcs.firePropertyChange("ITEM_SELECTED",getCaption(),null);
}
else
{comboBox.setSelectedItem(lastSelectedItem);}}
public void popupMenuCanceled(PopupMenuEvent e)
{ }
public void popupMenuWillBecomeVisible(PopupMenuEvent e)
{
ComboBox comboBox = (ComboBox)e.getSource();
this.lastSelectedItem = comboBox.getSelectedItem();
}
combobox.getUI().setPopupVisible(combobox, false);
You can use SwingUtilities.invokeLater.
For example
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
final JComboBox comboBox = (JComboBox) e.getSource();
final Object repeatedSelectedItem = comboBox.getSelectedItem();
if (repeatedSelectedItem != null
&& repeatedSelectedItem.equals("(Custom..)")) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
invokeCustomFilterDialog(repeatedSelectedItem, comboBox);
}
});
}
}
I have the following piece of code to create a JList:
rightPanel.add(Box.createRigidArea(new Dimension(20, 20)));
final JList list = new JList(nameData);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(5);
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(250, 80));
listScroller.setAlignmentX(LEFT_ALIGNMENT);
rightPanel.add(listScroller);
And I have this code for a listSelectionListener() to disable a button, when an item in the list isn't selected:
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() == -1) {
//No selection.
deleteConfirmButton.setEnabled(false);
} else {
//Selection.
deleteConfirmButton.setEnabled(true);
index = list.getSelectedIndex();
}
}
}
});
But whether an item in the list is selected or not, the state for the button doesn't change. What should I do?
Your deleteConfirmButton disable-enable logic is not correct. At first you need to disable. At the selection of list you should enable and at the click on deleteConfirmButton button you shoudl again disable. Read the comment of following code.
final JButton deleteConfirmButton = new JButton("Kustuta");
deleteConfirmButton.setEnabled(false); //Disable here
deleteConfirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteConfirmButton.setEnabled(false);//Disable here
}
});
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
deleteConfirmButton.setEnabled(true);//Enable here
}
});
Assuming deleteConfirmButton is inside a JPanel named rightPanel, call the repaint and revalidate methods to paint the button again.
if (list.getSelectedIndex() == -1) {
//No selection.
deleteConfirmButton.setEnabled(false);
} else {
//Selection.
deleteConfirmButton.setEnabled(true);
index = list.getSelectedIndex();
}
rightPanel.repaint();
rightPanel.revalidate();
another problem. I wanted to make doubleclick on JTable which open new window with form. So finally i made it this way:
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event){
int viewRow = table.getSelectedRow();
if(viewRow < 0)
System.out.println("LOL");
else{
final int modelRow = table.convertRowIndexToModel(viewRow);
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 2)
try {
new BookForm();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
}
});
It works, but not perfect. First time when i doubleclick on JTable it opens 2 windows (why not one?), next time it opens 4 windows, next another 6 windows, etc. Any ideas? Maybe i should have to use different method? Thanks for help!
Take a second to look over your code...
Each time the selection changes, you add a new MouseListener
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event){
int viewRow = table.getSelectedRow();
if(viewRow < 0)
System.out.println("LOL");
else{
// You add a new mouse listener...
final int modelRow = table.convertRowIndexToModel(viewRow);
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 2)
try {
new BookForm();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
}
});
So, when you "finally" double click a row, you will have 1-n MouseListeners registered against the table...
You could just get rid of the selection listener and simple add the MouseListener directly to the table...
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 2)
int selectedRow = table.getSelectedRow();
if (selectedRow > -1) {
int modelRow = table.convertRowIndexToModel(selectedRow);
try {
new BookForm();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
Also take a look at The Use of Multiple JFrames: Good or Bad Practice? before you bomb bard your user with lots of new windows...
How can I update the state (enable/disable) of my button when the user changes the selected item in a combo box.?
The button has a reference to the combo box, but the combo box does not know anything about the button.
If the button has a reference to the combo box, then the button can register an action listener at the combo box in which you can change the state of your button.
final JButton button = new JButton();
final JComboBox comboBox = new JComboBox();
comboBox.addActionListener( new ActionListener() {
#Override
public void actionPerformed( final ActionEvent event ) {
// Your logic to determine when to enable/disable:
final boolean enabled = comboBox.getSelectedIndex() == 0;
button.setEnabled( enabled );
}
} );
The combobox is not required to know about the button. You need to add a listener to the combobox events like this:
public class ComboBoxDemo ... implements ActionListener {
. . .
petList.addActionListener(this) {
. . .
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String petName = (String)cb.getSelectedItem();
yourButton.setEnabled(true/false);
}
. . .
}
The JButton could simply add its own ActionListener on the JComboBox and in the ActionListener you could then change the state of the JButton according to the selected item of the combo box.
I have previously written code where enabling or disabling a button depends on filling a textfield and selecting an item of a combobox together. It may be helpful here.
jComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jComboBoxActionPerformed(e);
}
});
...
jTextField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
jTextFieldDocumentListener(e);
}
public void removeUpdate(DocumentEvent e) {
jTextFieldDocumentListener(e);
}
public void changedUpdate(DocumentEvent e) {
jTextFieldDocumentListener(e);
}
});
jTextField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextFieldActionPerformed(e);
}
});
...
private void jComboBoxActionPerformed(ActionEvent e){
if(jComboBox.getSelectedIndex() == -1)
jButton.setEnabled(false);
else if(!jTextField.getText().equals(""))
jButton.setEnabled(true);
}
private void jTextFieldDocumentListener(DocumentEvent e){
if(jTextField.getText().equals("") || jComboBox.getSelectedIndex() == -1){
jButton.setEnabled(false);
}
else{
jButton.setEnabled(true);
}
}
private void jTextFieldActionPerformed(ActionEvent e){
if(jTextField.getText().equals("")){
jButton.setEnabled(false);
}
if(!(jTextField.getText().equals(""))){
jButton.setEnabled(true);
}
}
If the combobox is selected and the textfield is filled, then the button will be enabled. Otherwise it will not be enabled.