Загвоздка по JComboBox - Java

Узнай цену своей работы

Формулировка задачи:

Приветствую коллеги! Пишу программу для заполнения формы документа. Реализовал вывод данных на форму документа из фрейма для заполнения, но возникла задача с JComboBox так как на документ выводится данные находящиеся в начале списка и выбор пользователя совершенно не влияет на вывод данных из JCB(выводится только первый элемент в списке).
public class CreateForm extends JFrame implements ActionListener {

    SpringLayout sl = new SpringLayout();
        JPanel panel = new JPanel(sl);
        JLabel jlFormSp; // форма справки
        JButton searchJB;
        JComboBox jtFormSp; // форма справки
        JLabel jlNumberSp; // номер справки
        JTextField jtNumberSp; // номер справки
        JCheckBox jCBox; //автомат номера справки
        JLabel jlDateSp; // дата справки
        String[] days = {"01","02","03","04","05","06","07","08","09","10",
                        "11","12","13","14","15","16","17","18","19","20",
                        "21","22","23","24","25","26","27","28","29","30","31"};
        String [] months = {"января", "февраля", "марта", "апреля","мая",
                            "июня", "июля", "августа", "сентября", "октября",
                            "ноября", "декабря"};
        String[] forms = {"1","2","3"};
        String[] wFromSp = {"первой форме", "второй форме", "третьей форме"};
        String[] litFormSp = {"А/","Б/","Д/"};
        JComboBox<?> daysJCB; // список дней
        JComboBox<?> monthsJCB; // список месяцев
        JTextField yearsJTF;    // поле год
        JTextField secondNameJTF; // Фамилия
        JLabel secondNameJL;    // Фамилия
        JTextField nameJTF;     // Имя
        JLabel nameJL;      //Имя
        JTextField fatherNameJTF;   // Отчество
        JLabel fatherNameJL;        // Отчество
        JLabel numDopJL;            // Номер допуска
        JTextField numDopJTF;       // Номер допуска
        JLabel whatDateJL;
        JComboBox<?> whatDateDayJCB;
        JComboBox<?> whatDateMonthJCB;
        JTextField whatDateYearJTF;
        JLabel whatDateAcceptJL;
        JComboBox<?> whatDateAcceptDayJCB;
        JComboBox<?> whatDateAcceptMonthJCB;
        JTextField whatDateAcceptYearJTF;
        JLabel whatFinDateJL;
        JComboBox<?> whatFinDateDayJCB;
        JComboBox<?> whatFinDateMonthJCB;
        JTextField whatFinDateYearJTF;
        JLabel whatFormSpJL;
        JComboBox<?> whatFormSpJCB;
        JComboBox<?> litDopJCB;

        String formSpCF;
        String whatFormSpCF;
        String numberSpCF;
        String daysSpCF;
        String monthsSpCF;
        String litDopCF;
        String whatDaysDopCF;
        String whatMonthsDopCF;
        String wDateAcceptDayCF;
        String wDateAcceptMonthCF;
        String wDateFinDayCF;
        String wDateFinMonthCF;

    public CreateForm(String string) {
        
        setSize(1000, 800);
        setLocation(200, 200);
        panel.setSize(100, 50);     
        panel.add(jlFormSp = new JLabel("Форма справки"));
        panel.add(jlNumberSp = new JLabel("Номер справки"));
        panel.add(jtFormSp = new JComboBox (forms));    // форма справки    
        panel.add(jtNumberSp = new JTextField (4)); // номер справки
        panel.add(jCBox = new JCheckBox("Автоматически")); jCBox.setEnabled(true); // автомат номера справки
        panel.add(jlDateSp = new JLabel("Дата создания справки"));
        panel.add(daysJCB = new JComboBox<Object>(days));   // дни даты
        panel.add(monthsJCB = new JComboBox<Object>(months)); // месяцы даты
        panel.add(yearsJTF = new JTextField(4));
        panel.add(secondNameJTF = new JTextField(20));
        panel.add(secondNameJL = new JLabel("Фамилия"));
        panel.add(nameJTF = new JTextField(20));
        panel.add(nameJL = new JLabel("Имя"));
        panel.add(fatherNameJTF = new JTextField(20));
        panel.add(fatherNameJL = new JLabel("Отчество"));
        panel.add(searchJB = new JButton("Создать форму"));
        panel.add(numDopJL = new JLabel("Номер допуска"));
        panel.add(numDopJTF = new JTextField(10));
        panel.add(whatDateJL = new JLabel("от..."));
        panel.add(whatDateDayJCB = new JComboBox<Object>(days));
        panel.add(whatDateMonthJCB = new JComboBox<Object>(months));
        panel.add(whatDateYearJTF = new JTextField(5));
        panel.add(whatDateAcceptJL = new JLabel("Дата утверждения"));
        panel.add(whatDateAcceptDayJCB = new JComboBox(days));
        panel.add(whatDateAcceptMonthJCB = new JComboBox<Object>(months));
        panel.add(whatDateAcceptYearJTF = new JTextField(5));
        panel.add(whatFinDateJL = new JLabel("Дата окончания справки"));
        panel.add(whatFinDateDayJCB = new JComboBox<Object>(days));
        panel.add(whatFinDateMonthJCB = new JComboBox<Object>(months));
        panel.add(whatFinDateYearJTF = new JTextField(5));
        panel.add(whatFormSpJCB = new JComboBox<Object>(wFromSp));
        panel.add(whatFormSpJL = new JLabel("создать справку по ..."));
        panel.add(litDopJCB = new JComboBox<Object>(litFormSp));

        sl.putConstraint(SpringLayout.WEST, jlFormSp, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, jlFormSp, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, jtFormSp, 130, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, jtFormSp, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, jlNumberSp, 50, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, jlNumberSp, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, jtNumberSp, 50, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, jtNumberSp, 130, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, jCBox, 90, SpringLayout.WEST,panel);
        sl.putConstraint(SpringLayout.WEST, jCBox, 350, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, jlDateSp, 90, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, jlDateSp, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, daysJCB, 90, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, daysJCB, 160, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, monthsJCB, 90, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, monthsJCB, 210, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, yearsJTF, 90, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, yearsJTF, 300, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, secondNameJL, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, secondNameJL, 130, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, secondNameJTF, 130, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, secondNameJTF, 130, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, nameJL, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, nameJL, 170, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, nameJTF, 170, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, nameJTF, 130, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, fatherNameJL, 210, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, fatherNameJL, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, fatherNameJTF, 210, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, fatherNameJTF, 130, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, numDopJL, 250, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, numDopJL, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, numDopJTF, 250, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, numDopJTF, 180, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatDateJL, 250, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatDateJL, 320, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatDateDayJCB, 250, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatDateDayJCB, 360, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatDateMonthJCB, 250, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatDateMonthJCB, 410, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatDateYearJTF, 500, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatDateYearJTF, 250, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatDateAcceptJL, 245, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatDateAcceptJL, 300, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatDateAcceptDayJCB, 300, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatDateAcceptDayJCB,360, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatDateAcceptMonthJCB, 300, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatDateAcceptMonthJCB, 410, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatDateAcceptYearJTF, 300, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatDateAcceptYearJTF, 500, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatFinDateJL, 350, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatFinDateJL, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatFinDateDayJCB, 350, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatFinDateDayJCB, 200, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatFinDateMonthJCB, 350, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatFinDateMonthJCB, 250, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatFinDateYearJTF, 350, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatFinDateYearJTF, 340, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatFormSpJCB, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatFormSpJCB, 320, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, whatFormSpJL, 15, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, whatFormSpJL, 190, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.NORTH, litDopJCB, 250, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, litDopJCB, 130, SpringLayout.WEST, panel);

        sl.putConstraint(SpringLayout.NORTH, searchJB, 400, SpringLayout.WEST, panel);
        sl.putConstraint(SpringLayout.WEST, searchJB, 25, SpringLayout.WEST, panel);
        
        formSpCF = (String) jtFormSp.getSelectedItem();
        whatFormSpCF = (String) whatFormSpJCB.getSelectedItem();
        daysSpCF = (String) daysJCB.getSelectedItem();
        monthsSpCF = (String) monthsJCB.getSelectedItem();
        litDopCF = (String) litDopJCB.getSelectedItem(); 
        whatDaysDopCF = (String) whatDateDayJCB.getSelectedItem();
        whatMonthsDopCF = (String) whatDateMonthJCB.getSelectedItem();
        wDateAcceptDayCF = (String) whatDateAcceptDayJCB.getSelectedItem();
        wDateAcceptMonthCF = (String) whatDateAcceptMonthJCB.getSelectedItem();
        wDateFinDayCF = (String) whatFinDateDayJCB.getSelectedItem();
        wDateFinMonthCF = (String) whatFinDateMonthJCB.getSelectedItem();

        searchJB.addActionListener(this);

        setContentPane(panel);
        setVisible(true);

    }

    public void actionPerformed(ActionEvent sjb) {
        if (sjb.getActionCommand().equals("Создать форму")){
            
            new FillForm(this);
            
        }
        
    }

}
class FillForm extends JFrame {
    
     public Image spravkaDva;
    
    SpringLayout inSL = new SpringLayout();
    JPanel panel;
    String name;
    String formSp;
    String numberSp;
    String whatFromSpJCBFF;
    String numberSpFF;
    String daysSpFF;
    String monthsSpFF;
    String yearsSpFF;
    String secondNameFF;
    String fatherNameFF;
    String litDopFF;
    String numDopFF;
    String wDaysDopFF;
    String wMonthsDopFF;
    String wYearsDopFF;
    String wAcceptDayFF;
    String wDateAcceptMonthFF;
    String wDateAcceptYearFF;
    String wDateFinDayFF;
    String wDateFinMonthFF;
    String wDateFinYearFF;
 
    public FillForm(CreateForm form) {
        super ("Форма справки");
        int width = 1174;
        int hegth = 1000;
        setBounds(0, 0, width, hegth);
        ImageIcon ii = new ImageIcon("C:/Users/User/Desktop/Java/Lesson0/src/Spravka3.jpg");
        spravkaDva = ii.getImage();
        
        panel = new ImagePanel(spravkaDva);
        panel.setBounds(0, 0, 400, 400);

        formSp = form.formSpCF;
        whatFromSpJCBFF = form.whatFormSpCF;
        daysSpFF = form.daysSpCF;
        monthsSpFF = form.monthsSpCF;
        litDopFF = form.litDopCF;
        wDaysDopFF = form.whatDaysDopCF;
        wMonthsDopFF = form.whatMonthsDopCF;
        wAcceptDayFF = form.wDateAcceptDayCF;
        wDateAcceptMonthFF = form.wDateAcceptMonthCF;
        wDateFinDayFF = form.wDateFinDayCF;
        wDateFinMonthFF = form.wDateFinMonthCF;
        numberSpFF = form.jtNumberSp.getText();
        name = form.nameJTF.getText();
        yearsSpFF = form.yearsJTF.getText();
        secondNameFF = form.secondNameJTF.getText();
        fatherNameFF = form.fatherNameJTF.getText();
        numDopFF = form.numDopJTF.getText();
        wYearsDopFF = form.whatDateYearJTF.getText();
        wDateAcceptYearFF = form.whatDateAcceptYearJTF.getText();
        wDateFinYearFF = form.whatFinDateYearJTF.getText();

        getContentPane().add(panel);
        setVisible(true);
    }
    
    private class ImagePanel extends JPanel{
        private Image img;
 
        public ImagePanel(Image img) {
            this.img = img;
             setLayout(null);
        }
        
        public void paintComponent(Graphics g) {
        g = (Graphics2D)g;
        g.drawImage(img, 0, 0, null);
        g.setFont(new Font("Times new Roman", Font.ROMAN_BASELINE, 26));
        g.drawString(name, 400, 435);
        g.drawString(formSp, 115, 347);
        g.drawString(whatFromSpJCBFF, 535, 137);
        g.drawString(numberSpFF, 170, 347);
        g.drawString(daysSpFF, 94, 390);
        g.drawString(monthsSpFF, 200, 387);
        g.drawString(yearsSpFF, 370, 390);
        g.drawString(secondNameFF, 530, 385);
        g.drawString(fatherNameFF, 520, 435);
        g.drawString(litDopFF, 250, 490);
        g.drawString(numDopFF, 280, 490);
        g.drawString(wDaysDopFF, 480, 490);
        g.drawString(wMonthsDopFF, 580, 490);
        g.drawString(wYearsDopFF, 740, 490);
        g.drawString(wAcceptDayFF, 108, 515);
        g.drawString(wDateAcceptMonthFF, 200, 515);
        g.drawString(wDateAcceptYearFF, 420, 515);
        g.drawString(wDateFinDayFF, 330, 610);
        g.drawString(wDateFinMonthFF, 380, 610);
        g.drawString(wDateFinYearFF, 540, 610);
  }
 
    }

}
Пожалуйста помогите разобраться...

Решение задачи: «Загвоздка по JComboBox»

textual
Листинг программы
String frm;
............
public FillForm(CreateForm form) {
...........
frm = form.jtFormSp.getSelectedItem().toString();
...........
}
 
public void paintComponent(Graphics g) {
............
g.drawString(frm, 20, 20);
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 3.933 из 5