java listbox_如何使用swing java中checklistbox中的select all选项?

在我的代码片段中! “checkBoxList”没有文件选择器选择并存储在其中的文件

复选框“tmp”它有文件的复选框!

当我在我的面板中显示文件[checkboxlist]时,它是未经检查的!

我有选项后勾选/取消它.

我有以下代码用于选择/取消选择选项

我需要知道何时显示文件!文件应显示为选中(勾选)

然后我可以修改我可以勾选/取消勾选的内容.

我坚持这个逻辑!

[

编辑:我做了并更新了这部分的答案(见图).

我将select / deselectall添加到面板(框)并且它有效

box.add(chckbxSelectAll);

&安培;&安培;我需要并且好奇地知道如何将我的selectall复选框放在我的面板中

]

public void selectAllMethod() {

Iterator i = checkBoxList.iterator();

while (i.hasNext()) {

JCheckBox tmp = i.next();

if (chckbxSelectAll.isSelected()) {

tmp.doClick();

} else {

tmp.setSelected(false);

selectedCounter -= 1;

if (selectedCounter < 0) {

selectedCounter = 0;

}

noOfFileTxt.setText(Integer.toString(selectedCounter));

}

}

}

这是我的按钮选择方法,用于选择文件夹并在带有复选框的面板中显示它

public void chooseDirectoryFrom() {

String tempStr = null;

try {

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

fileChooser = new JFileChooser();

Font font = new Font("Latha", Font.ITALIC, 10);

fileChooser.setFont(new Font("Latha", Font.PLAIN, 13));

fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

fileChooser.setFont(font);

int returnVal = fileChooser.showOpenDialog(frame);

if (returnVal == JFileChooser.APPROVE_OPTION) {

tempStr = fileChooser.getSelectedFile().getCanonicalPath();

}

if (tempStr != null && !tempStr.trim().equals("")) {

searchBox.setText(tempStr);

// Enable the search button

// btnDisplay.setEnabled(true);

} else {

//btnDisplay.setEnabled(false);

}

} catch (Exception e) {

e.printStackTrace();

}

// public void selectToDisplay() { //disabled btn to display

File sourceFolder = null;

Box box = Box.createVerticalBox();

if (boxList.size() != 0) {

middlePanel.remove(scrollPane);

middlePanel.repaint();

frame.repaint();

boxList = new ArrayList();

checkBoxList = new ArrayList();

fileNamesMap = new HashMap();

selectedCounter = 0;

noOfFileTxt.setText(Integer.toString(selectedCounter));

}

sourceFolder = new File(searchBox.getText());

File[] sourceFilesList = sourceFolder.listFiles();

JCheckBox cb1 = null;

for (int i = 0; i < sourceFilesList.length; i++) {

if (sourceFilesList[i].isFile() & sourceFilesList[i].getName().endsWith(".txt")) {

fileNamesMap.put(sourceFilesList[i].getAbsolutePath(), sourceFilesList[i].getName());

cb1 = new JCheckBox(sourceFilesList[i].getAbsolutePath());

cb1.setFont(new Font("Latha", Font.BOLD, 20));

box.add(cb1);

checkBoxList.add(cb1);

cb1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (((AbstractButton) e.getSource()).isSelected()) {

selectedCounter += 1;

} else {

selectedCounter -= 1;

if (selectedCounter < 0) {

selectedCounter = 0;

}

}

noOfFileTxt.setText(Integer.toString(selectedCounter));

}

});

}

}

boxList.add(box);

scrollPane = new JScrollPane(box);

scrollPane.setPreferredSize(new Dimension(1050, 350));

scrollPane.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

middlePanel.add ( scrollPane );

frame.getContentPane().add(middlePanel);

frame.repaint();

frame.revalidate();

}

这是我的图像(没有选择)!当我在面板中加载文件

2ykIe.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
删除之类操作需要全选功能,方便选择 public class MainActivity extends Activity { private ListView lv; private MyAdapter mAdapter; private ArrayList list; private Button bt_selectall; // private Button bt_cancel; // private Button bt_deselectall; private int checkNum; // 记录选的条目数量 private TextView tv_show;// 用于显示选的条目数量 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 实例化各个控件 */ lv = (ListView) findViewById(R.id.lv); bt_selectall = (Button) findViewById(R.id.bt_selectall); // bt_cancel = (Button) findViewById(R.id.bt_cancelselectall); // bt_deselectall = (Button) findViewById(R.id.bt_deselectall); tv_show = (TextView) findViewById(R.id.tv); list = new ArrayList(); // 为Adapter准备数据 initDate(); // 实例化自定义的MyAdapter mAdapter = new MyAdapter(list, this); // 绑定Adapter lv.setAdapter(mAdapter); // 全选按钮的回调接口 bt_selectall.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 遍历list的长度,将MyAdapter的map值全部设为true for (int i = 0; i < list.size(); i++) { MyAdapter.getIsSelected().put(i, true); } // 数量设为list的长度 checkNum = list.size(); // 刷新listview和TextView的显示 dataChanged(); } }); // 反选按钮的回调接口 // bt_cancel.setOnClickListener(new OnClickListener() { // @Override // public void onClick(View v) { // // 遍历list的长度,将已选的设为未选,未选的设为已选 // for (int i = 0; i < list.siz
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值