認沽使細胞(行,列)一組,使您的表使用該設置爲檢查是否啓用電池。
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);
}
}
輸出:
但是,爲什麼我會被迫通過JTextField,只是一個可編輯所有非可編輯單元格的按鈕,因爲我的修改將在數據庫上完成 – Recay
然後,更容易使所有單元格都可編輯。是你想要的:make **所有單元格可編輯**? – Willmore
哈利路亞非常感謝你匹配Spooliecookie – Recay