如何在javaSWING的面板中添加Scrollable(滚动条组件)复选框?

时间:2016-02-16 08:37:36

标签: java swing checkbox scrollbar

已经参考了问题 " how to restrict Joptionpane message dialog box prompting once for the Condition?"

我的代码可能是一个大部分完成的GUI程序。 我的代码是显示Jfilechooser选择的目录中的文件。

文件将以框架中的复选框形式出现。问题是我将文件放在带有复选框的arraylist中,并尝试将其显示在框架上。

但问题是我无法在textarea中创建这些文件,因为我无法在textarea中获取组件。

因此,面板中的文件(复选框)已破碎。

我尝试应用滚动窗格将其放入框架中并查看它。但是我卡在某处我无法将滚动窗格应用于arraylist。

欢迎任何建议。 我需要在可视区域中访问这些文件(复选框)。

这是我目前的输出: enter image description here

完全我有1000 +文件。所以我需要他们放入滚动窗格来查看和选择。 (有时当我读2000 +文件时,GUI会卡住),不知道为什么吗?

我需要的示例输出LIKE。

enter image description here

full code below:

package latestprojectswing;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class latestswingpgm3 extends Thread implements ActionListener
{
 JFrame frame;
JPanel panel;
JTextField tf,text,tf1;
JTextArea ta,text1;
JLabel lab1;
String str;
JScrollBar scrol;
JScrollPane scrollPane;
File fl;
private JCheckBox chckbxSelectAll;
private JCheckBox chckbxf1;
private JTextField textField;
private JLabel lblSourceFolderfiles;
private JButton btnChoosedirectoryfrom;
private JButton btnDisplay;
private JLabel lblListFilesBelow;
private JScrollPane jScrollPane1;
ArrayList<JCheckBox> aL = new ArrayList<JCheckBox>();
int selectedCounter = 0;
int y=100;
JPanel contentPane;
String outputDir;
latestswingpgm3()
{
frame = new JFrame( "Search box" );
frame.getContentPane().setLayout( null );
frame.setSize( 820, 700 );

panel = new JPanel();
panel.setBounds( 25, 90, 750, 400 );
//panel.setLayout(null);
//panel.setBounds( 25, 90, 750, 400 );
scrollPane = new JScrollPane( panel );
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//scrollPane.setBounds( 25, 90, 750, 300 );
scrollPane.setPreferredSize(new Dimension(500, 300));
//panel.setVisible(true);
scrollPane.setVisible(true);
frame.add(panel);
frame.add(scrollPane);

tf = new JTextField();
tf.setBounds( 25, 50, 750, 40 );
tf.setFont( new Font( "Latha", Font.BOLD, 20 ) );
tf.setHorizontalAlignment( JTextField.CENTER );
frame.getContentPane().add( tf );


chckbxSelectAll = new JCheckBox("Select All");
chckbxSelectAll.setBounds(25, 557, 97, 23);
frame.getContentPane().add(chckbxSelectAll);
chckbxSelectAll.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Iterator<JCheckBox> i = aL.iterator();
        while(i.hasNext()) {
            JCheckBox tmp = i.next();
            if(chckbxSelectAll.isSelected()) {
                tmp.doClick();
            } else {
                tmp.setSelected(false);
                selectedCounter -= 1;
                if(selectedCounter < 0) {
                    selectedCounter = 0;
                }
                textField.setText(Integer.toString(selectedCounter));
            }

        }
    }
    });

JButton btnGenerate = new JButton("Generate");
btnGenerate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
    Iterator<JCheckBox> i = aL.iterator();
    while(i.hasNext()) {
        JCheckBox tmp = i.next();

        //scroll adder
        jScrollPane1 = new JScrollPane(frame);
        jScrollPane1.add(tmp);
        jScrollPane1.revalidate();
        if(tmp.isSelected()) {
            String filepathNName =  tf.getText() +"\\"+ tmp.getText();
            //filepathNName = System.getProperty("path.separator");
            String filename = tmp.getText();
            System.out.println("filename = " + filename);
            System.out.println("filepaname = " + filepathNName);
            System.out.println("outputDir = " + outputDir);
            boolean success = latestex1.exlCreator(filepathNName, outputDir, filename);

            if(success) {
                JOptionPane.showMessageDialog(null, "Successfully Completed. Pls refer to the path " + outputDir + " for output files");


            } else {

                JOptionPane.showMessageDialog(null, "OOPS. Some Error!!!");

            }
        }
    }

}
});
btnGenerate.setBounds(316, 604, 89, 23);
frame.getContentPane().add(btnGenerate);

textField = new JTextField();
textField.setBounds(268, 558, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);

JLabel lblNoOfFiles = new JLabel("NO of Files Selected");
lblNoOfFiles.setBounds(141, 561, 139, 14);
frame.getContentPane().add(lblNoOfFiles);

JLabel lblDestinationFolderTo = new JLabel("Destination PathTo Generate Files");
lblDestinationFolderTo.setBounds(553, 561, 226, 14);
frame.getContentPane().add(lblDestinationFolderTo);

JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {   

    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int returnVal = chooser.showOpenDialog(frame);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
        str = chooser.getSelectedFile().getAbsolutePath();
    }

    if ( str != null && !str.trim().equals( "" ) )
    {
        tf1.setText( str );
        outputDir = str;
      // Enable the search button
      //btnDisplay.setEnabled( true );
    }
    else
    {
   //btnDisplay.setEnabled( false );
    }
}
});
btnBrowse.setBounds(553, 583, 89, 23);
frame.getContentPane().add(btnBrowse);

tf1 = new JTextField();
tf1.setBounds( 553, 620, 400, 30 );
tf1.setFont( new Font( "Latha", Font.BOLD, 20 ) );
frame.getContentPane().add( tf1 );

lblSourceFolderfiles = new JLabel("Source Folder/ Files");
lblSourceFolderfiles.setBounds(6, 17, 138, 14);
frame.getContentPane().add(lblSourceFolderfiles);


btnChoosedirectoryfrom = new JButton("ChooseDirectory From");
btnChoosedirectoryfrom.addActionListener(this);
btnChoosedirectoryfrom.setBounds(141, 9, 170, 30);
frame.getContentPane().add(btnChoosedirectoryfrom);

btnDisplay = new JButton("Select To Display");
btnDisplay.setEnabled(false);
btnDisplay.setBounds(534, 9, 180, 30);
btnDisplay.addActionListener( this );

frame.getContentPane().add(btnDisplay);

lblListFilesBelow = new JLabel("List files Below to choose ");
lblListFilesBelow.setBounds(344, 17, 180, 14);
frame.getContentPane().add(lblListFilesBelow);
frame.setVisible( true );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

public void actionPerformed( ActionEvent ae )
{
if ( ae.getActionCommand().equals( "ChooseDirectory From" ) )
{
  JFileChooser chooser = new JFileChooser();
  chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  int returnVal = chooser.showOpenDialog(frame);
  if(returnVal == JFileChooser.APPROVE_OPTION) {
        str = chooser.getSelectedFile().getAbsolutePath();
    }

  if ( str != null && !str.trim().equals( "" ) )
  {
    tf.setText( str );

    // Enable the search button
    btnDisplay.setEnabled( true );
  }
  else
  {
 btnDisplay.setEnabled( false );
  }
}

if ( ae.getActionCommand().equals( "Select To Display" ) )
{
  if(aL!=null) {
      Iterator<JCheckBox> i = aL.iterator();
      while(i.hasNext()) {
          panel.remove(i.next());
      }
      selectedCounter = 0;
      textField.setText(Integer.toString(selectedCounter));
  }
  fl = new File( str );
  File[] flist = fl.listFiles();
  for ( int i = 0; i < flist.length; i++ )
  {
      if ( flist[i].isFile() )
      {
     final JCheckBox cb1 = new JCheckBox(flist[i].getName());


     scrollPane.add(cb1);
     panel.add(cb1);



     cb1.setBounds(25, y, 200, 25);
     cb1.setFont(new Font( "Latha", Font.BOLD, 20 ));
     cb1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e){






                if(cb1.isSelected()) {
                    selectedCounter += 1;
                } else {
                    selectedCounter -= 1;
                    if(selectedCounter < 0) {
                        selectedCounter = 0;
                    }
                }
                textField.setText(Integer.toString(selectedCounter));
            }
         });
     aL.add(cb1);
     panel.add(cb1);
     y+=30;   
      }
  }
}
}

public static void main( String args[] )
 {
new latestswingpgm3();
}
}

2 个答案:

答案 0 :(得分:2)

您有几个问题:

  • grep -rl "$old" /etc | xargs -r sed -i "s/$old/$new/g" 中使用JTable表格数据。

  • 为避免阻止冻结GUI的event dispatch thread,请使用SwingWorker在后台执行文件I / O;完整示例显示为here

  • 要获取每行的复选框,请确保第一列的类型为JScrollPanedefault editor and renderer将为您管理复选框;完整示例显示为here

答案 1 :(得分:0)

我遇到了与JListBox和ScrollPanel类似的问题。

解决方案是在1个组件中添加所有组件,可以是其他类型的组件。然后将这个1大组件添加到JScrollPane。

希望它有所帮助。

EDIT01: 我现在无法访问我的代码,因此我在线编译。我希望你明白这一点。

public static void main(String[] args) {
JFrame jFrame = new JFrame("Checkable list");

JPanel contentPane = new JPanel();
JPanel listOfFiles = new JPanel();
listOfFiles.setLayout(new BoxLayout(listOfFiles, BoxLayout.Y_AXIS));

JCheckBox ch1 = new JCheckBox("ch1");
JCheckBox ch2 = new JCheckBox("ch2");
JCheckBox ch3 = new JCheckBox("ch3");
JCheckBox ch4 = new JCheckBox("ch4");
JCheckBox ch5 = new JCheckBox("ch5");
JCheckBox ch6 = new JCheckBox("ch6");
JLabel lbl = new JLabel("inside the scroll");
listOfFiles.add(ch1);
listOfFiles.add(ch2);
listOfFiles.add(ch3);
listOfFiles.add(ch4);
listOfFiles.add(ch5);
listOfFiles.add(ch6);
listOfFiles.add(lbl);

JLabel lbl1 = new JLabel("Other stuff");
contentPane.add(lbl1);

JScrollPane jScrollPane = new JScrollPane(listOfFiles);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setPreferredSize(new Dimension (200, 100));
contentPane.add(jScrollPane);
jFrame.add(contentPane);

jFrame.setSize(400, 300);
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);

}