user3177987
user3177987

Reputation: 35

JButton + radiobox + checkbox

I'd like this program I have to have some kind of "sum" button which will add in the column "Description" summarised information about the movie. Lets say I have "die hard" as a title, age 7 from radiobutton, and horror selected from the checkbox. Pressin the button would put "Die hard, 7, horror" under the column. I have no idea how to aproach this case.

 package naplety.Swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;

public class SamodzielnaListaOsob extends JFrame implements ActionListener {

    JButton dodaj, erease;
    JTextField film;
    DefaultListModel<String> listFilm;
    DefaultTableModel tableFilm;

    public SamodzielnaListaOsob(String title) {
        super(title);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        final JTextField film = new JTextField("Wpisz tytul filmu", 10);
        film.setBorder(BorderFactory.createTitledBorder("Film"));

        JPanel p1 = new JPanel();
        p1.add(film);

        JButton dodaj = new JButton("Add to list");
        dodaj.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nowyFilm = film.getText();
                if (nowyFilm != "") {
                    listFilm.addElement(nowyFilm);
                    film.setText("");
                }
            }
        });
        JButton erease = new JButton("Clear");
        erease.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                film.setText("");

            }
        });
        JButton dodajDoTabeli = new JButton("Add to table");
        dodajDoTabeli.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nowyFilm = film.getText();
                if (nowyFilm != "") {
                    int ile = tableFilm.getRowCount();
                    tableFilm.addRow(new Object[] { ile + 1, nowyFilm });
                }
            }
        });

        JRadioButton sevenbutton = new JRadioButton("7");
        JRadioButton twbutton = new JRadioButton("12");
        JRadioButton sixbutton = new JRadioButton("16");
        JRadioButton eightbutton = new JRadioButton("18");

        ButtonGroup bg1 = new ButtonGroup();
        bg1.add(sevenbutton);
        bg1.add(twbutton);
        bg1.add(sixbutton);
        bg1.add(eightbutton);

        JPanel radioPanel = new JPanel();
        radioPanel.setLayout(new GridLayout(4, 0));
        radioPanel.add(sevenbutton);
        radioPanel.add(twbutton);
        radioPanel.add(sixbutton);
        radioPanel.add(eightbutton);

        radioPanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(), "Age"));
        radioPanel.setSize(200, 200);

        JCheckBox Horror = new JCheckBox("Horror");
        JCheckBox Komedia = new JCheckBox("Comedy");
        JCheckBox Thriller = new JCheckBox("Thriller");

        JCheckBoxMenuItem listac = new JCheckBoxMenuItem();
        listac.add(Horror);
        listac.add(Komedia);
        listac.add(Thriller);

        JPanel listaChceck = new JPanel();
        listaChceck.add(Horror);
        listaChceck.add(Komedia);
        listaChceck.add(Thriller);
        listaChceck.setLayout(new GridLayout(3, 0));

        JPanel p2 = new JPanel();
        p2.add(dodaj);
        p2.add(erease);
        p2.add(dodajDoTabeli);
        p2.add(radioPanel);
        p2.add(listaChceck);

        listFilm = new DefaultListModel<String>();
        listFilm.addElement("Achacy");
        listFilm.addElement("Bonifacy");
        listFilm.addElement("Cezary");

        JList<String> lista = new JList<String>(listFilm);
        JScrollPane sl = new JScrollPane(lista);
        sl.setPreferredSize(new Dimension(150, 150));
        sl.setBorder(BorderFactory.createTitledBorder("List"));

        String[] kolumnyTabeli = { "Nr", "Movie", "Description" };
        tableFilm = new DefaultTableModel(kolumnyTabeli, 0) {

        };
        JTable tabela = new JTable(tableFilm);
        JScrollPane st = new JScrollPane(tabela);
        st.setPreferredSize(new Dimension(300, 150));
        st.setBorder(BorderFactory.createTitledBorder("Table"));

        JPanel p3 = new JPanel();
        p3.add(sl);
        p3.add(st);

        setPreferredSize(new Dimension(900, 900));
        setVisible(true);
        p1.add(p2);
        p2.add(p3);
        setContentPane(p1);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SamodzielnaListaOsob("List of movies");
            }
        });
    }

}

Upvotes: 0

Views: 523

Answers (2)

Fco P.
Fco P.

Reputation: 2691

Radio buttons and Check Buttons don't work like textfields. In a textfield, the useful values are picked up from the "text" member (example: getText()). A radioButton instead defines a set of fixed choices (well, you can make them dynamic, but it doesn't worth it, there are better components for that kind of work) like "yes" and "no". Usually, when you pickup some rabiobutton, you use the isChecked() method (returns boolean, it may be isChecked(), I don't remember) to react in one way or another. Example:

String age = 0;
if(sevenbutton.isSelected()){
    age = 7;
}

Nonetheless, I think you can get the value from the text in the sevenbutton using getText(), but you're gonna need to check which radiobutton is checked anyway. A checkButton works in a pretty similar way, but for non-exclusive choices, so you need to use the isSelected() method, or similar, anyway. Your method should look like:

private void addMovie(){
   String age = "0";
   String gender = "";
   //Evaluate radioButtons
   if(sevenbutton.isSelected()){
         age = 7;
   }
   //Evaluate checkbuttons
   if(Horror.isSelected()){
        gender = gender+" horror";
   }

   String movie = film+" "+age+" "+gender;
   //do anything else
}

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 208994

  1. You need to declare your variables either before you try to access them, or declare them global, which I did. I prefer this way.

  2. Use .pack() on your frame to when you start the program, something actually shows.

  3. Learn to use LayoutManagers for a better look.

  4. Use arrays of RadioButtons and CheckBoxes so its easier to loop through them. I has to manually write a bunch of if statements, which would not be necessary if I could loop through them.

  5. To get is a RadioButton or CheckBox is selected, use .isSelected()

  6. .setVisible(true) after you add all your components.


Here's is your refactored code. I did nothing else to it, but fix the issue posted in your question. It now adds the info the desciption, when you hit the Add Film button.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class SamodzielnaListaOsob extends JFrame {

    JButton dodaj, erease;
    JTextField film;
    DefaultListModel<String> listFilm;
    DefaultTableModel tableFilm;
    JList<String> lista = null;

    JRadioButton sevenbutton = new JRadioButton("7");
    JRadioButton twbutton = new JRadioButton("12");
    JRadioButton sixbutton = new JRadioButton("16");
    JRadioButton eightbutton = new JRadioButton("18");

    JCheckBox Horror = new JCheckBox("Horror");
    JCheckBox Komedia = new JCheckBox("Comedy");
    JCheckBox Thriller = new JCheckBox("Thriller");

    ButtonGroup bg1 = new ButtonGroup();

    public SamodzielnaListaOsob(String title) {
        super(title);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        final JTextField film = new JTextField("Wpisz tytul filmu", 10);
        film.setBorder(BorderFactory.createTitledBorder("Film"));

        JPanel p1 = new JPanel();
        p1.add(film);

        JButton dodaj = new JButton("Add to list");
        dodaj.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nowyFilm = film.getText();
                if (nowyFilm != "") {
                    listFilm.addElement(nowyFilm);
                    film.setText("");
                }
            }
        });
        JButton erease = new JButton("Clear");
        erease.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                film.setText("");

            }
        });
        JButton dodajDoTabeli = new JButton("Add to table");
        dodajDoTabeli.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nowyFilm = film.getText();
                if (nowyFilm != "") {
                    int ile = tableFilm.getRowCount();
                    String title = lista.getSelectedValue();
                    int age;
                    if (sixbutton.isSelected()) {
                        age = 16;
                    } else if (sevenbutton.isSelected()) {
                        age = 7;
                    } else if (eightbutton.isSelected()) {
                        age = 18;
                    } else {
                        age = 12;
                    }
                    String genres = "";
                    if (Horror.isSelected()) {
                        genres += "Horror, ";
                    }
                    if (Komedia.isSelected()) {
                        genres += "Komedia, ";
                    }
                    if (Thriller.isSelected()) {
                        genres += "Thriller";
                    }

                    String desc = title + ", " + age + ", " + genres;
                    tableFilm.addRow(new Object[]{ile + 1, nowyFilm, desc});
                }
            }
        });

        bg1.add(sevenbutton);
        bg1.add(twbutton);
        bg1.add(sixbutton);
        bg1.add(eightbutton);

        JPanel radioPanel = new JPanel();
        radioPanel.setLayout(new GridLayout(4, 0));
        radioPanel.add(sevenbutton);
        radioPanel.add(twbutton);
        radioPanel.add(sixbutton);
        radioPanel.add(eightbutton);

        radioPanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(), "Age"));
        radioPanel.setSize(200, 200);

        JCheckBoxMenuItem listac = new JCheckBoxMenuItem();
        listac.add(Horror);
        listac.add(Komedia);
        listac.add(Thriller);

        JPanel listaChceck = new JPanel();
        listaChceck.add(Horror);
        listaChceck.add(Komedia);
        listaChceck.add(Thriller);
        listaChceck.setLayout(new GridLayout(3, 0));

        JPanel p2 = new JPanel();
        p2.add(dodaj);
        p2.add(erease);
        p2.add(dodajDoTabeli);
        p2.add(radioPanel);
        p2.add(listaChceck);

        listFilm = new DefaultListModel<String>();
        listFilm.addElement("Achacy");
        listFilm.addElement("Bonifacy");
        listFilm.addElement("Cezary");

        lista = new JList<String>(listFilm);
        JScrollPane sl = new JScrollPane(lista);
        sl.setPreferredSize(new Dimension(150, 150));
        sl.setBorder(BorderFactory.createTitledBorder("List"));

        String[] kolumnyTabeli = {"Nr", "Movie", "Description"};
        tableFilm = new DefaultTableModel(kolumnyTabeli, 0) {

        };
        JTable tabela = new JTable(tableFilm);
        JScrollPane st = new JScrollPane(tabela);
        st.setPreferredSize(new Dimension(300, 150));
        st.setBorder(BorderFactory.createTitledBorder("Table"));

        JPanel p3 = new JPanel();
        p3.add(sl);
        p3.add(st);


        p1.add(p2);
        p2.add(p3);
        setContentPane(p1);

        pack();
        setPreferredSize(new Dimension(900, 900));
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SamodzielnaListaOsob("List of movies");
            }
        });
    }

}

Upvotes: 1

Related Questions