Jcombobox value change while clicking on jbutton - java

I have to search contents of a jtable using values of two relative jcomboboxes.But when i click on the search jbutton, it's not working,although the value in one jcombobox is changed.. how to overcome this problem

I assume you have two jComboBox:
jComboBox1 and jComboBox2
now I store selected two values from jComboBoxes as String and made action event in your jButton like:
String str1=jComboBox1.getSelectedItem().toString();
String str2=jComboBox2.getSelectedItem().toString();
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchValue(sre1,str2);
}
});
public void searchValue(String str1, String str2){
//your code to get connection and so on
try{
/**
*You can make your select query like:
*SELECT * FROM TABLE WHERE someField1='"+str1+"' and someField2='"+str2+"'
*/
}catch(Exception e){
e.printStackTrace();
}
}

Related

How to open another JFrame with selected RadioButton according to data in JTable?

I am trying to send data to JFrame named UpdateCar using a button. Data should say which radio button should be selected.
btnUpdateCar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UpdateCar updatePage = new UpdateCar();
updatePage.setVisible(true);
int selectedRow = table.getSelectedRow();
updatePage.buttonGroup.setSelected(getSelectedButton(model,selectedRow).getModel(),true);
// when I use above line it doesn't work. But instead if I use the thing it will return, it works for that value.
}
});
I wrote a method like this for this purpose:
public JRadioButton getSelectedButton(DefaultTableModel model, int selectedRow) {
String selectedButton = (String) model.getValueAt(selectedRow,3);
UpdateCar updatePage = new UpdateCar();
if(selectedButton.equals("Automatic")) {
return updatePage.rdbtnAutomatic;
}else {
return updatePage.rdbtnManuel;
}
}
Well, apparently both method and btnUpdateCar's action perfrom method should have same reference for target JFrame. I was using two different calls for that JFrame. Now I have global variable for that Jframe and problem solved.

Need my GUI to read from a JTextField and show in a JPanel

I have added an action listener to the text field. When the btnReadString (Button Read String) is pressed the program should read what is on the text field and show on the JPanel. but nothing shows on the panel.
stringTextField.addActionListener(new ActionListener() {
public void stringTextField (java.awt.event.ActionEvent e) {
if(e.getSource()==btnReadString) //when the button is pressed
{
String stringParameter = stringTextField.getText(); //gets the text and puts it on this string called "stringParameter"
textPane.setText(stringParameter);//the JPanel is set to what is on the string.
}
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
The functionality for the ActionListener should go in the actionPerformed method, as nothings calling the stringTextField method...
stringTextField.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnReadString) //when the button is pressed
{
String stringParameter = stringTextField.getText(); //gets the text and puts it on this string called "stringParameter"
textPane.setText(stringParameter);//the JPanel is set to what is on the string.
}
}
});
But, based on the code, the ActionListener should be attached to the btnReadString and not the field, as the above logic will never result in anything been executed (as the source of the event will never be btnReadString)
btnReadString.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String stringParameter = stringTextField.getText(); //gets the text and puts it on this string called "stringParameter"
textPane.setText(stringParameter);//the JPanel is set to what is on the string.
}
});
I would suggest having a closer look at How to Write an Action Listener and How to Use Buttons, Check Boxes, and Radio Buttons for more details
You have added the ActionListener to the text field. So the event source is never going to be the button and hence, the code is never going to execute. What you want is to add the ActionListener to the JButton.
Also, the actionPerformed() is there for a reason. All your 'action' code goes inside this method.
So your code should look like this:
btnReadString.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String stringParameter = stringTextField.getText();
textPane.setText(stringParameter);
}
});

how to get selected row when click on combobox in table

I have a table and at each row i have a combox of operator from which we can choose any operator and value1 field value 2 field. COMBO BOX DEFAULT OPERATOR IS " EQUAL TO".so my question is when u click on combobox in any row i should get the value of the selected row and get the operator which i am selecting such that i can perform some operation based on selected operator....
Or else if i change the combobox operator from in between to equal to i should get clear the value 2 field....
Help me get out of this..
You should have an event to know item in the combo is clicked.
Like this:
combo.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
//doSomething();
}
});
You have three methods to get current item selected:
Will get the index of the item is order number.
int selectedIndex = myComboBox.getSelectedIndex();
-or-
Will get item selected with Object. You can do many method contains in this Object.
Object selectedObject = myComboBox.getSelectedValue();
-or-
Will get real values of item selected with string type.
String selectedValue = myComboBox.getSelectedValue().toString();
You can see full example code at here (from #secario member):
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class MyWind extends JFrame{
public MyWind() {
initialize();
}
private void initialize() {
setSize(300, 300);
setLayout(new FlowLayout(FlowLayout.LEFT));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField field = new JTextField();
field.setSize(200, 50);
field.setText(" ");
JComboBox comboBox = new JComboBox();
comboBox.setEditable(true);
comboBox.addItem("item1");
comboBox.addItem("item2");
//
// Create an ActionListener for the JComboBox component.
//
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//
// Get the source of the component, which is our combo
// box.
//
JComboBox comboBox = (JComboBox) event.getSource();
Object selected = comboBox.getSelectedItem();
if(selected.toString().equals("item1"))
field.setText("30");
else if(selected.toString().equals("item2"))
field.setText("40");
}
});
getContentPane().add(comboBox);
getContentPane().add(field);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MyWind().setVisible(true);
}
});
}
}

JComboBox editable and blank on selection

I want to make a JComboBox in which a particular item text should change and becomes editable on selection.For example if JComboBox has two items "ONE","TWO" in it's list then on Selection of "TWO".
I have wrote a sample program in which either i can make field editable or can change the Text but not both.So someone please suggest how to make selective item editable and changed text as well
Object[] items = new Object[]{"One","Two"};
DefaultComboBoxModel dcbm = new DefaultComboBoxModel(items);
final JComboBox comboBox = new JComboBox(dcbm);
comboBox.setPreferredSize(new Dimension(200, 20));
comboBox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
Object selectedItem = comboBox.getSelectedItem();
boolean editable = selectedItem instanceof String && ((String)selectedItem).equals("Two");
comboBox.setEditable(editable);
//comboBox.setSelectedItem("text has changed");
}
});
Something like...
String[] data = {"One", "Two"};
JComboBox<String> cb = new JComboBox<>(data);
add(cb);
cb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
cb.setEditable(cb.getSelectedIndex() != 0);
}
});
will basically do it, but what it won't do, is update the value of the model, just so you know ;)
If you want to make the editor "blank" when the combobox becomes editable, you could add...
if (cb.isEditable() && cb.getSelectedIndex() != -1) {
cb.setSelectedItem("");
}
to the ActionListener
So I'm not the best with jComboBox off the top of my head so this may not help but I would assume it uses an array to set the strings for the objects in the combo box along the lines of
(new String[] {"ONE","TWO"});
and with my understanding of arrays you could do something like
comboBox.addMouseListener(new MouseAdapter(){
public void ActionPerformed(MouseEvent click){
optionTwoClicked(click);
}
}
and then add the handler with something like
private void optionTwoClicked(MouseEvent click){
if (click.getSelectedItem()=String[2]){
String onTwo = JOptionPane.showInputDialog(null,"Enter your message","Messages",2);
textItem.setText()="onTwo";
}else{ //do something here?
}
}
Like I said before, not absolutely familiar with jComboBox, but,
Hope that helps!

Multiple itemlisteners in a single class

I am new to Java and Swing and am following zetcode tutorial. I want to add multiple JComboBoxes and store the index selected for each one of those. index1 should hold selected index from 1st instance of JComboBox and index2 should hold selected index from 2nd instance of JComboBox. For one JComboBox it can be done like this:
public ComboBox() {
setLayout(new BoxLayout(getContentPane(),
BoxLayout.Y_AXIS));
add(Box.createRigidArea(new Dimension(0, 35)));
combobox = new JComboBox(authors);
combobox.addItemListener(this);
add(combobox);
}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox combo = (JComboBox) e.getSource();
int index = combo.getSelectedIndex();
display.setIcon(new ImageIcon(
ClassLoader.getSystemResource(images[index])));
}
}
So if I could write the name of itemlistener that should be called for each JComboBox and then instead of writing combobox.addItemListener(this), I could write combobox.addItemListener(itemListener1). How do I do this?
try doing like this
combobox1.addItemListener(this);
combobox2.addItemListener(this);
..
comboboxn.addItemListener(this);
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(comboBox1))
{
\\do something
}
else if(e.getSource().equals(comboBox2))
{
\\do something
}
..
else if(e.getSource().equals(comboBoxn))
{
\\do something
}
Use inner or anonymous classes, it helps to avoid 'if - else' statements.
Exemple! of anonymous class

Categories

Resources