JCombobox - change value in JTable - java

first post here.
How to write method which can change values in JTable when app is on running? getValIn() returns me values in english (i used ResourceBundle for that and its work), but if i change the combobox I want have this value in french in JTable.
It's a piece of code.
public void show(){
String[] lang = {"en", "fr"};
List<String> columns = new ArrayList<String>();
Object[][] obj;
JComboBox combobox = new JComboBox(lang);
combobox.setSelectedIndex(0);
combobox.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
if(combobox.getSelectedItem().toString().equals("en")){
selectedLang ="en";
}else{
selectedLang = "fr";
}
}
});
obj = getValIn(selectedLang);
TableModel tb= new DefaultTableModel(obj, columns.toArray());
JTable table = new JTable(tb);
It's not necessary to be in JCombobox.
Thanks, have a nice day!

Related

Add DATA from JTable to another one

I have a JTable that displays a list of questions.
What I want to do is when I click on a question among the list view it will be added in the other JTable ( and the question still in the first JTable).
I must of course add a click event on the JTable but after I know what to do.
The result of the execution of the program
List<Question> questions=new ArrayList<>();
List<Question> questions2=new ArrayList<>();
table_1 = new JTable();
scrollPane_1.setViewportView(table_1);
table = new JTable();
scrollPane.setViewportView(table);
setLayout(groupLayout);
initDataBindings();
}
protected void initDataBindings() {
JTableBinding<Question, List<Question>, JTable> jTableBinding = SwingBindings.createJTableBinding(UpdateStrategy.READ, questions, table);
//
BeanProperty<Question, String> questionBeanProperty = BeanProperty.create("contenu");
jTableBinding.addColumnBinding(questionBeanProperty).setColumnName("Contenu");
//
BeanProperty<Question, Collection<Reponse>> questionBeanProperty_1 = BeanProperty.create("reponses");
jTableBinding.addColumnBinding(questionBeanProperty_1).setColumnName("Les reponses");
//
BeanProperty<Question, String> questionBeanProperty_2 = BeanProperty.create("niveauDeDifficulte");
jTableBinding.addColumnBinding(questionBeanProperty_2).setColumnName("Niveau de difficult\u00E9");
//
jTableBinding.bind();
}
Hello,
#Override
public void mouseClicked(MouseEvent e) {
questions2.add(questions.get(table.getSelectedRow()));
}
});

making sure JCombo actions only execute on initial selection?

I need to use a JCombo box and at the moment I am just printing messages to screen for testing. When I make a selection it works as expected, however when I then re-click the combo box to change selection I get the same message box before it lets me make another selection. How would I get it so that the action is only performed on initial selection?
String[] positions={"1","2","3","4"};
JComboBox combo = new JComboBox<String>(positions);
combo.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae ){
//Display selected stuff
JOptionPane.showMessageDialog(null, combo.getSelectedItem());
}
});
Use a boolean to know if it has already been checked or not.
Solution
String[] positions={"1","2","3","4"};
JComboBox combo = new JComboBox<String>(positions);
combo.addActionListener(new ActionListener(){
boolean comboAlreadyChecked = false;
#Override
public void actionPerformed(ActionEvent ae ){
//Display selected stuff
if (!comboAlreadyChecked){
JOptionPane.showMessageDialog(null, combo.getSelectedItem());
comboAlreadyChecked = true;
}
}
});
PS : The name of your boolean may be a little easier than this one. This is just to clarify.
After taking onboard the answers provided and through modifying the solution given in the aforementioned tutorial, I came up with the following solution:
String labels[] = {"", "A", "B", "C", "D", "E", "F"};
JComboBox comboBox = new JComboBox(labels);
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
int state = itemEvent.getStateChange();
ItemSelectable is = itemEvent.getItemSelectable();
if (selectedString(is) == "A" & state == ItemEvent.SELECTED) {
System.out.println("A");
}
}
};
comboBox.addItemListener(itemListener);
Please try this
public static void main(String args[]) {
JComboBox comboBox = new JComboBox();
comboBox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
System.err.println("new item: " + e.getItem());
}
});
}

Getting a row of names from the first column of JTable

I have a JTable with the column names " Names " , " Quantity " and " Unit " .
I'm coding a program where you get the ingredients names.
So i need to get the whole row of one column and String it all up together,
because i need to store it in mySql and i have already set it all as String.
Any idea how i can do this ?
My code are as follows :
JTable Code:
DefaultTableModel model = (DefaultTableModel)table.getModel();
if(!txtQty.getText().trim().equals("")){
model.addRow(new Object[]{ingCB.getSelectedItem().toString(),txtQty.getText(),unitCB.getSelectedItem().toString()});
}else{
JOptionPane.showMessageDialog(null,"*Quantity field left blank");
}
Getting the values and for storing :
for(int i = 1; i<= i ; i++){
ingredients = table.getName();
}
This is for loop is wrong and it does not work because i have a constructor to take in Ingredients but because it is inside the loop, it cannot take it in.
Any suggestions please ? Thank you.
Constructor :
Food e2 = new Food(Name, Description, priceDbl, Image, Category, Ingredients, promotion );
e2.createFood();
I'm coding a program where you get the ingredients names. So i need to get the whole row of one column and String it all up together, because i need to store it in mySql and i have already set it all as String.
Want to do so, try this. Here I am getting result into ArrayList and String, as I am commented ArrayList you can avoid it.
public class TableValuePrint extends JFrame implements ActionListener{
private final JButton print;
private final JTable table;
private String str="";
public TableValuePrint() {
setSize(600, 300);
String[] columnNames = {"A", "B", "C"};
Object[][] data = {
{"Moni", "adsad", "Pass"},
{"Jhon", "ewrewr", "Fail"},
{"Max", "zxczxc", "Pass"}
};
table = new JTable(data, columnNames);
JScrollPane tableSP = new JScrollPane(table);
JPanel tablePanel = new JPanel();
tablePanel.add(tableSP);
tablePanel.setBackground(Color.red);
add(tablePanel);
setTitle("Result");
setSize(1000,700);
print=new JButton("Print");
JPanel jpi1 = new JPanel();
jpi1.add(print);
tablePanel.add(jpi1,BorderLayout.SOUTH);
print.addActionListener(this);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TableValuePrint ex = new TableValuePrint();
ex.setVisible(true);
}
});
}
#Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==print){
// ArrayList list = new ArrayList();
for(int i = 0;i<table.getModel().getRowCount();i++)
{
//list.add(table.getModel().getValueAt(i, 0)); //get the all row values at column index 1
str=str+table.getModel().getValueAt(i,0).toString();
}
//System.out.println("List="+list);
System.out.println("String="+str);
}
}
}
Output
String=MoniJhonMax

have problems in Linking multiple JComboBox in JTable one time

I got a quite tricky problem when I do some code on JTable
I need to add one line In JTable when I click "Add" button, and I want one of the columns to render
as a JComboBox
the problem is that when I only add one line, it works fine.
but when I add multiple lines a time, no matter which combobox I choose item from, It will always trigger the last comboBox's event(seems always the same combobox since I have printed the hashcode of jComboBox in MyComboxActionListener class. it's the same).
why is that happens , I can't figure it out. Since It's totally a new comboBox and a new listener when I add one line.
Following is the code.
Thanks in advance.
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
ProducedProcedure_new addedProducedProcedure = new ProducedProcedure_new(); // the new item
componentProcedureTableModel.getWorks().add(addedProducedProcedure); //add one line to the table
componentProcedureTableModel.fireTableRowsInserted(componentProcedureTableModel.getRowCount()-1, componentProcedureTableModel.getRowCount()-1);
jTable1.changeSelection(componentProcedureTableModel.getRowCount()-1,0, false, false);
List<String> procedureNames = produceCardManager.getProcedureNames(componentIdTextField.getText().trim(),false); //get the items added to combobox
renderColumnAsCombox(1,procedureNames,addedProducedProcedure); //-------------------------------------------
}
void renderColumnAsCombox(int columnIndex , List<String> items,ProducedProcedure_new producedProcedure) {
TableColumn col = jTable1.getColumnModel().getColumn(columnIndex);
JComboBox comboBox = new JComboBox();
for(String item : items) {
comboBox.addItem(item);
}
MyComboxActionListener myComboxActionListener = new MyComboxActionListener(columnIndex,comboBox,producedProcedure);
comboBox.addActionListener(myComboxActionListener);
col.setCellEditor(new DefaultCellEditor(comboBox));
}
class MyComboxActionListener implements ActionListener { // listen for the select event of the combobox
private JComboBox jComboBox;
private ProducedProcedure_new producedProcedure;
private int columnIndex;
public MyComboxActionListener(int columnIndex,JComboBox jComboBox,ProducedProcedure_new producedProcedure) {
this.columnIndex = columnIndex;
this.jComboBox = jComboBox;
this.producedProcedure = producedProcedure;
}
#Override
public void actionPerformed(ActionEvent e) {
String selectedItem = (String)jComboBox.getSelectedItem();
producedProcedure.getProcedure().setProcedureName(selectedItem);
producedProcedure.getProcedure().setProcedureId(String.valueOf(produceCardManager.getProcedureId(selectedItem)));
producedProcedure.getProcedure().setFactor(produceCardManager.getProcedureFactor(selectedItem)); //automately update the factor
((ComponentProcedureTableModel_new)jTable1.getModel()).fireTableDataChanged();
}
}

How do I load a JComboBox with values from an Array or ArrayList?

I need to put the following array into a JComboBox and then store the selected value when the "Submit" button is clicked.
listOfDepartments = new String[5];
listOfDepartments[0] = "Mens Clothing";
listOfDepartments[1] = "Womens Clothing";
listOfDepartments[2] = "Childrens Clothing";
listOfDepartments[3] = "Electronics";
listOfDepartments[4] = "Toys";
//Department: ComboBox that loads from array
// Store values
JButton buttonSubmit = new JButton();
buttonSubmit.setText("Submit");
container.add(buttonSubmit);
buttonSubmit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
//store value from combobox in a variable
}
});
First, create a model...
DefaultComboBoxModel model = new DefaultComboBoxModel(listOfDepartments);
comboBox.setModel(model);
Second, get the selected value when the actionPerformed event is raised...
String value = (String)comboBox.getSelectedItem();
Take a look at How to Use Combo Boxes for more details.

Categories