复选框工具7.22——删除某几行-文本框-多次添加目录

本文档介绍了如何使用复选框工具进行目录路径获取、删除表格中的特定行以及重新载入路径的操作。删除表格时,从最后一行开始清空,并对后续行号进行调整;而获取目录路径可通过配置文件或文本框实现。
摘要由CSDN通过智能技术生成

获取目录路径:

1.通过配置文件获取(目前是通过基本的io流输入)

2.通过文本框获取

private void dathinit() throws IOException {
		DefaultTableModel model = (DefaultTableModel) jTabcheckbox.getModel();

		// String filedath = null;

		// String dath = "./config/Checkbox.config";
		// FileReader fr = new FileReader(dath);
		// BufferedReader br = new BufferedReader(fr);
		//
		// String lineText = null;
		// while ((lineText = br.readLine()) != null) {
		// System.out.println(lineText);
		// filedath = lineText;
		// }
		//
		// System.out.println(filedath);
		// br.close();

		// filedath = "D://CPUZ";
		String filedath = null;
		filedath = jTextArea1.getText();
		jTextArea1.setText(filedath);
		//		if (filedath.equals("") == true) {
		//
		//		} 

		if (filedath == null || filedath.trim().equals("")) {

		} 
		else {
			File file = new File(filedath);
			File[] fileList = file.listFiles();

			for (int i = 0; i < fileList.length; i++) {
				if (fileList[i].isFile()) {
					String fileName = fileList[i].getName();
					System.out.println("fileName:  " + fileName);
					model.addRow(new Object[] { true, model.getRowCount() + 1 ,fileName });

				}

				if (fileList[i].isDirectory()) {
					String fileName = fileList[i].getName();
					System.out.println("DirectoryName:  " + fileName);
				}
			}
		}

	}

删除表格:

1.从最后一行开始清空表格

2.删除被勾选的行,注意:当删除一行时,后面的需要 加一(i++) 以保证行数

		jBClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				while(model.getRowCount()>0){
					model.removeRow(model.getRowCount()-1);
				}
			}
		});

		jBDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					int i = 0;
					int sum = model.getRowCount();
					while(i < sum)
					{
						if ( (boolean) model.getValueAt(i, 0) == true)
						{
							model.removeRow(i);
						} else {
								i++;
							 }
						sum = model.getRowCount();
					}
				}


			
		});

重新载入路径:

	jBDath.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					dathinit();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});

完整代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.table.DefaultTableModel;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Highgo
 */
public class CheckboxSwing extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Creates new form CheckboxSwing
	 * 
	 * @throws IOException
	 */
	public CheckboxSwing() throws IOException {
		this.setTitle("CheckboxSwing");
		initComponents();
		inittable();
		this.setLocationRelativeTo(null);

	}

	private CheckboxSwing getThis() {
		return this;
	}

	private void dathinit() throws IOException {
		DefaultTableModel model = (DefaultTableModel) jTabcheckbox.getModel();

		// String filedath = null;

		// String dath = "./config/Checkbox.config";
		// FileReader fr = new FileReader(dath);
		// BufferedReader br = new BufferedReader(fr);
		//
		// String lineText = null;
		// while ((lineText = br.readLine()) != null) {
		// System.out.println(lineText);
		// filedath = lineText;
		// }
		//
		// System.out.println(filedath);
		// br.close();

		// filedath = "D://CPUZ";
		String filedath = null;
		filedath = jTextArea1.getText();
		jTextArea1.setText(filedath);
		//		if (filedath.equals("") == true) {
		//
		//		} 

		if (filedath == null || filedath.trim().equals("")) {

		} 
		else {
			File file = new File(filedath);
			File[] fileList = file.listFiles();

			for (int i = 0; i < fileList.length; i++) {
				if (fileList[i].isFile()) {
					String fileName = fileList[i].getName();
					System.out.println("fileName:  " + fileName);
					model.addRow(new Object[] { true, model.getRowCount() + 1 ,fileName });

				}

				if (fileList[i].isDirectory()) {
					String fileName = fileList[i].getName();
					System.out.println("DirectoryName:  " + fileName);
				}
			}
		}

	}

	private void inittable() throws IOException {

		DefaultTableModel model = (DefaultTableModel) jTabcheckbox.getModel();

		dathinit();

		jBAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for (int i = 0; i < model.getRowCount(); i++) {
					model.setValueAt(Boolean.valueOf(true), i, 0);
				}

			}
		});

		jBAllNot.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for (int i = 0; i < model.getRowCount(); i++) {
					model.setValueAt(Boolean.valueOf(false), i, 0);
				}
			}
		});

		jBClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				while(model.getRowCount()>0){
					model.removeRow(model.getRowCount()-1);
				}
			}
		});

		jBDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					int i = 0;
					int sum = model.getRowCount();
					while(i < sum)
					{
						if ( (boolean) model.getValueAt(i, 0) == true)
						{
							model.removeRow(i);
						} else {
								i++;
							 }
						sum = model.getRowCount();
					}
				}


			
		});

		getRootPane().setDefaultButton(jBOK);
		jBOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object str = null;
				for (int i = 0; i < model.getRowCount(); i++) {
					str = model.getValueAt(i, 0);
					// System.out.println(str);
					if (str.equals(true)) {
						Object mojdlog = model.getValueAt(i, 2);
						System.out.println(mojdlog);
						System.out.println("true" + (i + 1));
					} else {
						System.out.println("false" + (i + 1));
					}
				}
				new NewJDialog(getThis(), true);
			}
		});

		jBDath.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					dathinit();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});

		addKeyListener(new KeyListener() {
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					System.exit(0);
					System.out.println("exit");
				}
			}

			public void keyTyped(KeyEvent e) {
			}

			public void keyPressed(KeyEvent e) {
			}
		});

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jBAllNot = new javax.swing.JButton();
        jBAll = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTabcheckbox = new javax.swing.JTable();
        jBOK = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jBDath = new javax.swing.JButton();
        jBClear = new javax.swing.JButton();
        jBDelete = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jBAllNot.setText("ANOT");

        jBAll.setText("ALL");

        jTabcheckbox.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Choose", "line", "Name"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Integer.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTabcheckbox);
        if (jTabcheckbox.getColumnModel().getColumnCount() > 0) {
            jTabcheckbox.getColumnModel().getColumn(0).setMinWidth(30);
            jTabcheckbox.getColumnModel().getColumn(0).setPreferredWidth(100);
            jTabcheckbox.getColumnModel().getColumn(0).setMaxWidth(200);
            jTabcheckbox.getColumnModel().getColumn(1).setMinWidth(30);
            jTabcheckbox.getColumnModel().getColumn(1).setPreferredWidth(65);
            jTabcheckbox.getColumnModel().getColumn(1).setMaxWidth(200);
        }

        jBOK.setText("OK");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jBDath.setText("Dath");

        jBClear.setText("Clear");

        jBDelete.setText("Delete");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jBAllNot, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jBDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBDath, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(162, 162, 162)
                .addComponent(jBOK, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(169, 169, 169))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jBAll)
                            .addComponent(jBClear))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jBAllNot)
                            .addComponent(jBDelete)))
                    .addComponent(jBDath))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                .addGap(3, 3, 3)
                .addComponent(jBOK)
                .addGap(6, 6, 6))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed" desc=" Look and feel setting code
		// (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the default
		 * look and feel. For details see
		 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(CheckboxSwing.class.getName()).log(java.util.logging.Level.SEVERE, null,
					ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(CheckboxSwing.class.getName()).log(java.util.logging.Level.SEVERE, null,
					ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(CheckboxSwing.class.getName()).log(java.util.logging.Level.SEVERE, null,
					ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(CheckboxSwing.class.getName()).log(java.util.logging.Level.SEVERE, null,
					ex);
		}
		// </editor-fold>
		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					new CheckboxSwing().setVisible(true);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAll;
    private javax.swing.JButton jBAllNot;
    private javax.swing.JButton jBClear;
    private javax.swing.JButton jBDath;
    private javax.swing.JButton jBDelete;
    private javax.swing.JButton jBOK;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTabcheckbox;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值