我在一段代码上停留了两天,我的表格的单元格是不可编辑的默认值,我想在我点击编辑按钮时可编辑,但我无法运行此代码。 提前感谢您的帮助。
table = new JTable(model) {
public boolean isCellEditable(int row, int column) {
return false;
}
};
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sorter = new TableRowSorter < TableModel > (model);
table.setPreferredScrollableViewportSize(new Dimension(560, 200));
JScrollPane scrollpane = new JScrollPane((table));
table.setRowSorter(sorter);
add(ScrollPane);
scrollPane.setBounds(10, 180, 560, 200);
table.setRowHeight(28);
table.setBackground(Color.DARK_GRAY);
table.setForeground(Color.WHITE);
modifier.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if ((JButton) e.getSource() == change) {
boolean isCellEditable(int row, int column) {
return true;
}
}
}
});
答案 0 :(得分:1)
将启用的单元格(row,col)放到一个集合中,并使您的表格使用该集合来检查单元格是否已启用。
package javaapplication33;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import javax.swing.Action;
import javax.swing.AbstractAction;
import javax.swing.JTextField;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.util.Set;
import java.util.HashSet;
import java.util.Collection;
import java.awt.EventQueue;
import javax.swing.Box;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.table.AbstractTableModel;
public class JavaApplication33
{
public static void main(String[] args)
{
Runnable r = new Runnable()
{
@Override
public void run()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new BorderLayout());
p.setPreferredSize(new Dimension(300,300));
final Set<Point> erc = new HashSet<>(); //this set contains (row,cell) pairs of editable cells
//put some data to the table
final JTable table = new JTable(new String[][]
{{"a", "aa"},
{"b", "bb"},
{"c", "cc"}},
new String[]{"col1", "col2"}) //name columns
{
Collection<Point> editableRowsCells = erc;
Collection<Point> getEditableRowsCells()
{
return editableRowsCells;
}
public boolean isCellEditable(int row, int column)
{
return getEditableRowsCells()
.contains(new Point(row, column));
}
};
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollpane = new JScrollPane(table);
p.add(scrollpane, BorderLayout.CENTER);
table.setRowHeight(28);
table.setBackground(Color.DARK_GRAY);
table.setForeground(Color.WHITE);
final JTextField xf = new JTextField();
final JTextField yf = new JTextField();
Action enableAction = new AbstractAction("Enable")
{
@Override
public void actionPerformed(ActionEvent e)
{
int row = Integer.valueOf(xf.getText());
int col = Integer.valueOf(yf.getText());
erc.add(new Point(row,col));
((AbstractTableModel) table.getModel())
.fireTableCellUpdated(row, col);
}
};
JButton enableButton = new JButton();
enableButton.setAction(enableAction);
Box b = new Box(BoxLayout.X_AXIS);
b.add(new JLabel("Row"));
b.add(xf);
b.add(new JLabel("Col"));
b.add(yf);
b.add(enableButton);
p.add(b, BorderLayout.NORTH);
f.add(p);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
这是输出:
更新:使所有单元格可编辑
package javaapplication33;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import javax.swing.Action;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import java.awt.EventQueue;
import java.awt.Color;
import javax.swing.table.AbstractTableModel;
public class JavaApplication33
{
public static class Toggle
{
Boolean val;
public Toggle(Boolean val)
{
this.val = val;
}
public void setVal(Boolean val)
{
this.val = val;
}
public Boolean getVal()
{
return val;
}
public void toggle()
{
setVal(!getVal());
}
}
public static void main(String[] args)
{
Runnable r = new Runnable()
{
@Override
public void run()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new BorderLayout());
p.setPreferredSize(new Dimension(300, 300));
final Toggle editable = new Toggle(false);
final JTable table = new JTable(
new String[][]{
{"a", "aa"},
{"b", "bb"},
{"c", "cc"}},
new String[] {"col1", "col2"}) //name columns
{
public boolean isCellEditable(int row, int column)
{
return editable.getVal(); //request current value
}
};
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollpane = new JScrollPane(table);
p.add(scrollpane, BorderLayout.CENTER);
table.setRowHeight(28);
table.setBackground(Color.DARK_GRAY);
table.setForeground(Color.WHITE);
Action enableAction = new AbstractAction("Toggle editable")
{
@Override
public void actionPerformed(ActionEvent e)
{
editable.toggle(); //toggle current value
((AbstractTableModel) table.getModel()).fireTableStructureChanged();
}
};
JButton enableButton = new JButton();
enableButton.setAction(enableAction);
p.add(enableButton, BorderLayout.NORTH);
f.add(p);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
输出:
答案 1 :(得分:0)
这是我的代码:
table = new JTable(model){
public boolean isCellEditable(int row, int column) {
return false;
}
};
modifier.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if ((JButton)e.getSource()== modifier)
{
public boolean isCellEditable(int row, int column) {
return true;
}
}
}
});