我正在爲一個項目創建這個軟件,我做了什麼是我在這裏創建了一個方法「沙拉」會發生什麼是用戶可以選擇最大數量的2個沙拉。沙拉的名稱來自數據庫,我動態地創建了複選框。ActionListener類不會觸發CheckBox
這工作正常,我可以得到的值,並將其插入到database.so然後我想要的是更新插入row.i創建一個方法setselected()
用戶選擇之前的所有複選框。所以我上面告訴我,我創建了這種方法來選擇最大數量沒有2複選框。當我嘗試插入它工作正常..但是當我嘗試更新該ActionListener不起作用。
我在構造函數中使用setSelected(true)
,ActionListener
沒有觸發。我應該怎麼做Salad()
?
public void Salad()
{
int count =0 ;//to count rows in result set
String sql="Select name from foodmenue where type='Salad' and menue='"+selecmenue+"' ";
try {
pst=con.prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next())
{
count++;
}
if(count!=0)
{
rs=pst.executeQuery();
JCheckBox [] a=new JCheckBox[count];
jPanel7.setLayout(new GridLayout());
int x=0;
while(rs.next())
{
a[x]=new JCheckBox(rs.getString("name"));
a[x].setName(rs.getString("name"));
jPanel7.add(a[x]);
a[x].setVisible(true);
a[x].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof JCheckBox){
String s=((JCheckBox) e.getSource()).getName();
if(((JCheckBox) e.getSource()).isSelected())
{
if(selsalad<2)//selsalad is 0 in the bigining
{
selectedsalad[selsalad]=((JCheckBox) e.getSource()).getName();
selsalad=selsalad+1;
}
else
{
((JCheckBox) e.getSource()).setSelected(false);
showMessageDialog(null, "you cant have more than 2 salads");
}
}
if(!((JCheckBox) e.getSource()).isSelected())
{
if(selectedsalad[0].equals(s)|| selectedsalad[1].equals(s) )
{
if(selsalad<2)
{
selectedsalad[selsalad]="";
}
selsalad=selsalad-1;
showMessageDialog(null, selsalad);
}
} }
}
});
x++;
}
}
} catch (Exception e) {
showMessageDialog(null, e);
}
}
這是我如何填寫支票boxex
for(Component c : jPanel7.getComponents())
{
if(c instanceof JCheckBox)
{
JCheckBox temp=(JCheckBox) c;
if(temp.getName().equals(s1)){
temp.setSelected(true);
}
if(temp.getName().equals(s2)){
temp.setSelected(true);
}
}
}
MCVE:爲了給出一個更好的主意
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static javax.swing.JOptionPane.showMessageDialog;
public class NewJFrame extends javax.swing.JFrame {
int selsalad=0;//count of the selected checkbox this cant go more than 2
String selectedsalad[]=new String[2];
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
public NewJFrame() {
initComponents();
Salad();
showMessageDialog(null, "uncomment promlemmethod to see the probelm after you try this one");
problemmehtod();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 561, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 470, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
public void problemmehtod(){ // this is the problem now i can select any much when i call this i commented this one
for(Component c : jPanel1.getComponents())
{
if(c instanceof JCheckBox)
{
JCheckBox temp=(JCheckBox) c;
if(temp.getName().equals("a1")){
temp.setSelected(true);
}
if(temp.getName().equals("a2")){
temp.setSelected(true);
}
}
}
}
public void Salad()
{
JCheckBox a[]=new JCheckBox[5];
// int count =0 ;//to count rows in result set
try {
for(int x=0;x<5;x++)
{
jPanel1.setLayout(new GridLayout());
a[x]=new JCheckBox("a"+String.valueOf(x));
a[x].setName("a"+String.valueOf(x));
jPanel1.add(a[x]);
a[x].setVisible(true);
a[x].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof JCheckBox){
String s=((JCheckBox) e.getSource()).getName();
if(((JCheckBox) e.getSource()).isSelected())
{
if(selsalad<2)
{
selectedsalad[selsalad]=((JCheckBox) e.getSource()).getName();
selsalad=selsalad+1;
}
else
{
((JCheckBox) e.getSource()).setSelected(false);
showMessageDialog(null, "you cant have more than 2 salads");
}
}
if(!((JCheckBox) e.getSource()).isSelected())
{
if(selectedsalad[0].equals(s)|| selectedsalad[1].equals(s) )
{
if(selsalad<2)
{
selectedsalad[selsalad]="";
}
selsalad=selsalad-1;
//showMessageDialog(null, selsalad);
}
}
}
}
});
}
} catch (Exception e) {
showMessageDialog(null, e);
}
}
// End of variables declaration
}
這是工作非常感謝你的幫助和時間你花了我 – pavithra