选择JRadioButton时如何设置不可编辑的JTable单元?

时间:2014-09-13 16:06:24

标签: java swing jtable jradiobutton

我有一些带有一些行的JTable。如果选择了radiobutton unlock,我怎么能阻止编辑行?在一个小项目下面用作工作示例

更新
ElencoPersoneFrame类

package test;
import java.awt.*;
import java.util.*;

import javax.swing.*;

public class ElencoPersoneFrame extends JFrame {
    private PersonaTableModel tableModel;
    private JTable table;
    private JScrollPane scrollPane;

    JRadioButton rdbtnFilm = new JRadioButton("Editable");
    JRadioButton rdbtnSerieTv = new JRadioButton("Not editable");
    public ElencoPersoneFrame()
    {
        super ("Elenco Persone");

        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setSize(400, 250);

        ArrayList<Persona> listaPersone = new ArrayList<Persona>();
        listaPersone.add(new Persona("Mario", "Rossi", 1972, false));
        listaPersone.add(new Persona("Giacomo", "Bianchi", 1946, false));
        listaPersone.add(new Persona("Roberto", "Verdi", 1985, true));
        tableModel = new PersonaTableModel(listaPersone);
        table = new JTable(tableModel);
        scrollPane = new JScrollPane(table);

        JPanel rdpnl=new radioPanel();

        getContentPane().add(rdpnl, BorderLayout.NORTH);
        getContentPane().add(scrollPane, BorderLayout.CENTER);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ElencoPersoneFrame f = new ElencoPersoneFrame();
                f.setVisible(true);
            }
        });
    }
}

人物角色

package test;
public class Persona 
{
        private String nome;
        private String cognome;
        private int annoNascita;
        private boolean disoccupato;

        /*costruttore*/
        public Persona(String nome, String cognome, int annoNascita, boolean disoccupato) {
            this.nome = nome;
            this.cognome = cognome;
            this.annoNascita = annoNascita;
            this.disoccupato = disoccupato;
        }

        public String getNome() { return nome; }
        public String getCognome() { return cognome; }
        public int getAnnoNascita() { return annoNascita; }
        public boolean isDisoccupato() { return disoccupato; }

        public void setNome(String nome) { this.nome = nome; }
        public void setCognome(String cognome) { this.cognome = cognome; }
        public void setAnnoNascita(int annoNascita) { this.annoNascita = annoNascita; }
        public void setDisoccupato(boolean disoccupato) { this.disoccupato = disoccupato; }
}

PersonaTableModel类

package test;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

public class PersonaTableModel extends AbstractTableModel 
{
    private ArrayList<Persona> listaPersone;

    public PersonaTableModel(ArrayList<Persona> listaPersone) {
        this.listaPersone = listaPersone;
    }

    public int getRowCount() {
        return listaPersone.size();
    }

    public int getColumnCount() {
        return 4;
    }

    public String getColumnName(int column) {
        switch (column) {
            case 0: return "Nome";
            case 1: return "Cognome";
            case 2: return "Anno nascita";
            case 3: return "Disoccupato?";
        }

        return "";
    }

    public Class getColumnClass(int column) {
        switch (column) {
            case 0: return String.class;
            case 1: return String.class;
            case 2: return Number.class;    
            case 3: return Boolean.class;  
        }
        return Object.class;
    }

    public boolean isCellEditable(int row, int column) {
        return true;
    }

    public Object getValueAt(int row, int column) {
        Persona p = listaPersone.get(row);

        switch (column) {
            case 0: return p.getNome();
            case 1: return p.getCognome();
            case 2: return p.getAnnoNascita();   
            case 3: return p.isDisoccupato();   
        }

        return null;
    }

   public void setValueAt(Object value, int row, int column) 
   {
        Persona p = listaPersone.get(row);

        switch (column) 
        {
            case 0: p.setNome((String) value); break;
            case 1: p.setCognome((String) value); break;
            case 2: p.setAnnoNascita((Integer) value); break;   
            case 3: p.setDisoccupato((Boolean) value); break;   
        }
    }

    public void aggiungiPersona(Persona p) {
        listaPersone.add(p);
        int row = listaPersone.size() - 1;
        fireTableRowsInserted(row, row);
    }
}

radioPanel类

package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class radioPanel extends JPanel implements ActionListener 
{
    private JRadioButton unlock;
    private JRadioButton lock;
    private ButtonGroup rdgroup;

    public radioPanel()
    {
        rdgroup=new ButtonGroup();
        unlock = new JRadioButton("Editable");
        lock = new JRadioButton("Not editable");

        rdgroup.add(unlock);
        rdgroup.add(lock);
        rdgroup.setSelected(unlock.getModel(), true);

        this.add(unlock);   
        this.add(lock);

        lock.addActionListener(this);
        unlock.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == this.lock)
        {

        }

        if(e.getSource() == this.unlock)
        {

        }   
    }
}

2 个答案:

答案 0 :(得分:1)

所以似乎没有(AFAIK)一些神奇的方法来设置表不可编辑。您可以做的是在您的模型中创建一个方法,例如setEditable(boolean),您可以在其中设置类成员boolean editable。对isCellEditable使用相同的字段。更改状态后,应该触发表更改。像

这样的东西
class PersonaTableModel extends AbstractTableModel {
    ...
    private boolean editable = true;

    public boolean isEditable() {
        return editable;
    }

    public boolean isCellEditable(int row, int column) {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
        fireTableDataChanged();
    }
    ...
}

您还需要让ActionListener获取表格模型。因此,您可以将您的无线电面板重构为

class radioPanel extends JPanel {
    private JRadioButton unlock;
    private JRadioButton lock;
    private ButtonGroup rdgroup;

    ...

    public ButtonGroup getButtonGroup() {
        return rdgroup;
    }

    public AbstractButton getUnlock() {
        return unlock;
    }

    public AbstractButton getLock() {
        return lock;
    }
}

这样您就可以从任何地方获取单选按钮并添加ActionListener。因此,您可以将主类代码更改为:

radioPanel rdpnl = new radioPanel();
rdpnl.getUnlock().addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        tableModel.setEditable(true);
    }
});
rdpnl.getLock().addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        tableModel.setEditable(false);
    }
});

getContentPane().add(rdpnl, BorderLayout.NORTH);

经过测试,似乎按预期工作。


除此之外:请注意Java命名约定。类名应以大写字母开头,即radioPanelRadioPanel

答案 1 :(得分:0)

您需要为isCellEditable

实施TableModel方法
public boolean isCellEditable(int row, int col)
        return false;

另请参阅tutorials