how to auto fill textfield in java using combo box - java

c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Object selected = c.getSelectedItem();
s = (String)selected;
tf1.setText(s);
}
});
when setting text it gives error attempt to mutate in notification. besides this if i write label.setText(s); it gives no error and set text according to selected item in combo... c is JComboBox object

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.

getting refrence for JTextFeild in actionListener

I am trying to use getText() method from a JTextFeild in an ActionListener attatched with it ... the problem is I don't have a reference that points to it ... that is I'm adding those textFeilds with in a loop that takes a string from arraylist and showing new textFeild , I searched the Internet trying to find a way to use getText() but it was pointless because I have no ref. on it , my question is how to get the text in the JTextFeild in this action Listener , and is there is any way to get a reference to this JTextFeild that the action performed on ????
JTextField t;
for(MyClass m: MyArraylist) {
t=new JTextField(m.toString());
t.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(getText());
}
});
}
I have tried getText(); super.getText(); t.getTaxt(); and for sure it will not work because t always changes ,also i tried system.out.println(m.toString()); and does not work
You should get the source for the event and cast it to the TextField
t.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
TextField tf = (TextField) e.getSource();
System.out.println(tf.getText());
}
});
you have to use the function from the object
t.getText()
see here

Java GUI adding buttons with a for loop

Hi i am making a lotto gui where the user picks 4 numbers from a selection of 28. The way i am currently doing it is as follows
private void no1InputButtonActionPerformed(java.awt.event.ActionEvent evt) {
numberSelectionList.add("1");
}
private void no2InputButtonActionPerformed(java.awt.event.ActionEvent evt) {
chosenNumDisplayLabel.setText(chosenNumDisplayLabel.getText()+" 2");
}
private void no3InputButtonActionPerformed(java.awt.event.ActionEvent evt) {
chosenNumDisplayLabel.setText(chosenNumDisplayLabel.getText()+" 3");
}
etc up through the 28 numbers.
Is there a way to add the actions to each button through a for loop
as this seems more logical?
Also is there a way to add each number picked into an array?
Create a single Action that can be shared by all buttons. The Action will then simply get the text of the button and then do some processing.
Check out setText method with panel and button. This example will show you how to:
create a single ActionListener to be shared by each button
"append" the text to the text field instead of replacing the text
use Key Bindings so the user can also just type the number
On each button you can set an action command:
button.setActionCommand("1");
And you can get the value after that using your ActionEvent:
evt.getActionEvent();
More complete:
ActionListener listener = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand()+" clicked");
}
};
int howMuchYouWant = 32;
for(int i = 0; i<howMuchYouWant; i++)
{
JButton button = new JButton(""+(i+1));
button.setActionCommand(""+i);
button.addActionListener(listener);
//add to whatever gui you want here
}

How to get currently entering text in JTable?

i am developing a project. On that i need to get currently typing characters in a particular column of JTable.
If i am using,
table.getValueAt(table.getSelectedRow(), 1)
it will return nothing till i select another column. How can i get currently entering text in JTable?
You can use jcombobox with autocomplete decorator in a JTable column,here is how Using a Combo Box as an Editor. and on key typed set the DefaultComboBoxModel
with the JList
You can get the editor being used by calling the method:
Component component = table.getEditorComponent();
Use this editor to get the text being edited, or attach a document listener to it, or anything you like, as follows:
JTextComponent editor = (JTextEditor)component;
String text = editor.getText();
editor.getDocument().addDocumentListener(new DocumentListener() {
void changedUpdate(DocumentEvent e) { ... }
void insertUpdate(DocumentEvent e) { ... }
void removeUpdate(DocumentEvent e) { ... }
});

Jcombobox value change while clicking on jbutton

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();
}
}

Categories

Resources