2014-09-13 83 views
1

我有一些行的JTable。如何在選擇單選按鈕解鎖時阻止編輯行?下面一個小項目作爲一個工作示例使用如何在選擇JRadioButton時設置不可編輯的JTable單元格?

更新
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) 
     { 

     } 
    } 
} 
+0

您的代碼不會編譯。把它變成一個不起作用的例子,以及你希望如何工作。我需要打包複製粘貼並運行它。 – user1803551 2014-09-14 14:26:04

+0

現在的問題應該更清楚:) – Raziel 2014-09-15 08:16:09

回答

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

+0

太容易了......謝謝。 – Raziel 2014-09-15 12:10:17

0

您需要實現您TableModelisCellEditable方法:

public boolean isCellEditable(int row, int col) 
     return false; 

又見tutorials

+0

是的,我知道它,但不知道如何設置爲假,當我在編輯模式。我認爲需要重寫isCellEditable方法或者我不知道。 – Raziel 2014-09-13 22:53:29

+0

@Raziel如果你使用上面的方法,你不能編輯任何單元格 - 沒有編輯模式。我上面寫的*是*覆蓋'isCellEditable'。 – user1803551 2014-09-13 22:54:57

+0

如何看到,我已經使用isCellarEditable方法,但是我不知道如何在checkEdiding返回false時將其設置爲false。 – Raziel 2014-09-14 08:53:09

相關問題