我正在做一個項目有關波動在這我面臨一個問題,像一些5複選框在這裏if i delete two non immediate check boxes (or) single check box i am able to delete but if i delete two immediate check boxes i am not able to delete
i.e
我有複選框像1,2,3,4,5如果我刪除1和3,1和4,或3和5等...(或)1,2,3等。如果我刪除使用這種組合我能夠刪除,但如果我刪除1和2或2和3或 4和5像立即複選框我無法刪除我是新的搖擺我不不會發生什麼錯誤和在哪裏。當我在尋找答案,我發現這個鏈接 Deleting check boxes刪除波動中的複選框
我用不同的方式在同一個程序,我在這裏創建自己的自定義類在這裏我的程序:
import java.awt.*;
import java.util.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class ListModels extends JDialog {
@SuppressWarnings("rawtypes")
private JList list;
private JPanel rightPanel;
JButton cancel = new JButton("Cancel");
JButton delbtn = new JButton("Delete");
public ListModels() {
createList();
createButtons();
initUI();
}
public ListModels(List<String> values) {
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void createList() {
myModel mModel = new myModel(new CheckListItem[] {
new CheckListItem("78"), new CheckListItem("79"),
new CheckListItem("80"), new CheckListItem("81"),
new CheckListItem("82") });
list = new JList(mModel);
list.setCellRenderer(new CheckListRenderer());
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
JList list = (JList) event.getSource();
// Get index of item clicked
int index = list.locationToIndex(event.getPoint());
CheckListItem item = (CheckListItem) list.getModel()
.getElementAt(index);
if (item.isSelected) {
System.out.println("i am selected ");
}
// Toggle selected state
item.setSelected(!item.isSelected());
// Repaint cell
list.repaint(list.getCellBounds(index, index));
}
});
}
private void createButtons() {
rightPanel = new JPanel();
// JButton cancel = new JButton("Cancel");
cancel.setMaximumSize(cancel.getMaximumSize());
// JButton delbtn = new JButton("Delete");
delbtn.setMaximumSize(cancel.getMaximumSize());
// Cancel button
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
// Cancel
delbtn.addActionListener(new ActionListener() {
@SuppressWarnings("rawtypes")
public void actionPerformed(ActionEvent event) {
// CheckListItem item = (CheckListItem)
// list.getModel().getElementAt(index);
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(null,
"Are you sure you want to delete the selected map",
"Delete", dialogButton);
myModel mModel = (myModel) list.getModel();
ListModel currentModel = list.getModel();
for (int i = 0; i < mModel.getSize(); i++) {
CheckListItem item = (CheckListItem) mModel.getElementAt(i);
if(!item.isSelected)
{
System.out.println("i am in not selected item "+item);
}
else if (item.isSelected()) {
System.out.println("i am selected item "+item);
if (dialogResult == JOptionPane.YES_OPTION) {
// Location loc=(Location) mModel.getElementAt(i);
mModel.removeAt(i);
mModel.fireIntervalRemoved(this, i,mModel.getSize());
}
}
}
}
});
// JPanel buttonPane = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.LINE_AXIS));
rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4));
rightPanel.add(Box.createHorizontalStrut(60));
rightPanel.add(delbtn);
rightPanel.add(Box.createRigidArea(new Dimension(10, 0)));
rightPanel.add(cancel);
}
private void initUI() {
// JScroll Panel
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(250, 80));
listScroller.setAlignmentX(LEFT_ALIGNMENT);
// Lay out the label and scroll pane from top to bottom.
JPanel listPane = new JPanel();
listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
// Add all to the panel
listPane.add(Box.createRigidArea(new Dimension(0, 5)));
listPane.add(listScroller);
listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Lay out the buttons from left to right.
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
buttonPane.add(Box.createHorizontalStrut(60));
buttonPane.add(delbtn);
buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPane.add(cancel);
listPane.add(buttonPane);
// Put everything together, using the content pane's BorderLayout.
Container contentPane = getContentPane();
contentPane.add(listPane, BorderLayout.CENTER);
contentPane.add(buttonPane, BorderLayout.PAGE_END);
add(listPane);
add(listPane);
setTitle("Delete Map");
setSize(300, 250);
setLocationRelativeTo(null);
}
class CheckListItem {
private String label;
private boolean isSelected = false;
public CheckListItem(String label) {
this.label = label;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
public String toString() {
return label;
}
}
@SuppressWarnings({ "rawtypes" })
class CheckListRenderer extends JCheckBox implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean hasFocus) {
setEnabled(list.isEnabled());
setSelected(((CheckListItem) value).isSelected());
setFont(list.getFont());
setBackground(list.getBackground());
setForeground(list.getForeground());
setText(value.toString());
return this;
}
}
class myModel extends AbstractListModel<CheckListItem> {
private List<CheckListItem> items;
public myModel(CheckListItem[] items) {
super();
this.items = new ArrayList<ListModels.CheckListItem>();
for (CheckListItem item : items) {
this.items.add(item);
}
}
/*
* @Override protected void fireContentsChanged(Object Source, int
* index0, int index1) { super.fireContentsChanged(Source, index0,
* index1); }
*/
@Override
public CheckListItem getElementAt(int index) {
return items.get(index);
}
@Override
protected void fireIntervalRemoved(Object Source, int index0, int index1) {
// TODO Auto-generated method stub
super.fireIntervalRemoved(Source, index0, index1);
}
@Override
public int getSize() {
return items.size();
}
public void removeAt(int item) {
items.remove(item);
}
public void addAt(int index, CheckListItem item) {
items.set(index, item);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
List<String> values = new ArrayList<String>();
values.addAll(newList);
ListModels ex = new ListModels();
ex.setVisible(true);
}
});
}
}
最後這個方案我通過我的自定義類直接給數字加載複選框,我想從我的主方法調用這個類,並通過數組列表或數組傳遞數字。任何幫助歡迎和感謝。
一些*非常重要的*評論:(1)不要抑制「未經檢查」和「粗糙」的警告,這是一個糟糕的主意。 (2)除非用於特殊的雙擊行爲,否則不要使用鼠標監聽器(或適配器)來管理JLists中的選擇,而應使用「ListSelectionListener」。 (3)不要使用'setMaximumSize',參見[this](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi)獲取更多信息。 – user1803551
在另一個說明中,使用一些標點符號。嘗試從一開始直到第一個階段(。)一口氣讀。 – user1803551
感謝您的建議!我會在下一次做。 – rakeshhatwar