Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Jtable:JAVA中将值设置为JtextField_Java_Swing_Jtable_Jtextfield - Fatal编程技术网

在Jtable:JAVA中将值设置为JtextField

在Jtable:JAVA中将值设置为JtextField,java,swing,jtable,jtextfield,Java,Swing,Jtable,Jtextfield,我尝试设置默认路径并将该值设置为JtextField。当我添加新行并选择其工作路径时。但是当脚本第一次启动时,我尝试为第一行提供默认路径,但它没有使用正确的字符串 这是屏幕截图:我给出了路径->//bie-pvcs-01/logs\u 122,但它需要角色脚本。MainGuiComp@127077b 这是我的密码: public class MainGui extends JFrame { private JPanel contentPane; private JTextField user

我尝试设置默认路径并将该值设置为JtextField。当我添加新行并选择其工作路径时。但是当脚本第一次启动时,我尝试为第一行提供默认路径,但它没有使用正确的字符串

这是屏幕截图:我给出了路径->//bie-pvcs-01/logs\u 122,但它需要角色脚本。MainGuiComp@127077b

这是我的密码:

public class MainGui extends JFrame {

private JPanel contentPane;
private JTextField userNametextField;
private JLabel searchLabel,lblNewLabel_2;
private JTable CompTable = null;
private CompTableModel CompModel = null;
private JButton addButton = null;
private String folder = File.separator+File.separator+"bie-pvcs-01"+File.separator+"logs_122"+File.separator;
private JComboBox<String> comboBox;
private JLabel pathWarningLabel;
private ArrayList<String> plist = new ArrayList<>();
File lastDirectory = new File(File.separator+File.separator+"bie-pvcs-01"+File.separator+"logs_122"+File.separator);
private JTextField textField;
private JTable table;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {
        // If Nimbus is not available, you can set the GUI to another look and feel.
    }
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainGui frame = new MainGui();
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                int w = frame.getSize().width;
                int h = frame.getSize().height;
                int x = (dim.width-w)/4;
                int y = (dim.height-h)/2;
                frame.setLocation(x, y);
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public MainGui() {
    getContentPane().setBackground(SystemColor.inactiveCaptionText);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 650, 650);
    contentPane = new JPanel();
    getContentPane().setLayout(null);
    setTitle("Role Finding Script");


    CompTable = CreateCompTable();
    JScrollPane CompTableScrollpane = new JScrollPane(CompTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    CompTableScrollpane.setBounds(10, 325, 599, 108);
    CompTableScrollpane.getViewport().setBackground(Color.white);
    getContentPane().add(CompTableScrollpane);

    JLabel lblUsername = new JLabel("   Username  :");
    lblUsername.setFont(new Font("LucidaSans", Font.BOLD, 13));
    lblUsername.setBounds(10, 111, 172, 30);
    Border border = BorderFactory.createRaisedSoftBevelBorder();
    border = BorderFactory.createEtchedBorder();
    lblUsername.setBorder(border);
    getContentPane().add(lblUsername);


    userNametextField = new JTextField();
    userNametextField.setBounds(216, 111, 393, 30);
    userNametextField.setFont(new Font("LucidaSans", Font.PLAIN, 13));
    getContentPane().add(userNametextField);
    userNametextField.setColumns(20);


    JLabel lblRole = new JLabel("    Roles        :");
    lblRole.setFont(new Font("LucidaSans", Font.BOLD, 13));
    lblRole.setBounds(10, 174, 172, 30);
    border = BorderFactory.createEtchedBorder();
    lblRole.setBorder(border);
    getContentPane().add(lblRole);

    comboBox = new JComboBox<String>();
    comboBox.setBounds(216, 175, 393, 30);
     //     comboBox.addItem("CHANGE-MANAGER");
     //     comboBox.addItem("PCMS-PART-MANAGER");
     //     comboBox.addItem("PMCS-ROLE-MANAGER");
    comboBox.addItem("VR_ANALYST");

    getContentPane().add(comboBox);


    textField = new JTextField();
    textField.setFont(new Font("Dialog", Font.PLAIN, 13));
    textField.setColumns(20);
    textField.setBounds(216, 239, 393, 30);
    getContentPane().add(textField);

    searchLabel = new JLabel("Searching.. Please wait..");
    searchLabel.setFont(new Font("LucidaSans", Font.BOLD, 13));
    searchLabel.setBounds(216, 564, 195, 30);
    searchLabel.setVisible(false);
    getContentPane().add(searchLabel);

    JButton btnNewButton = new JButton("Execute !");
    btnNewButton.setFont(new Font("LucidaSans", Font.BOLD, 13));
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            //check the username
            System.out.println(CompTable.getRowCount());
            if(userNametextField.getText() == null || userNametextField.getText().equals("")){
                lblNewLabel_2.setText("Please filled in the username part !");               
            }else if(CompTable.getRowCount() == 0){
                pathWarningLabel.setText("Please add at least one path..");
                pathWarningLabel.setVisible(true);
            }else{
                for(int i=0; i<CompTable.getRowCount(); i++){
                    if(CompTable.getValueAt(i, 0).toString() != null && !CompTable.getValueAt(i, 0).toString().equals("")){
                        plist.add(CompTable.getValueAt(i,0).toString());
                    }
                }
                for(String a : plist){
                    System.out.println("1" + a);
                }
                if(plist.size() != 0){
                    searchLabel.setVisible(true);
                    SearchingClass task = new SearchingClass(searchLabel,userNametextField.getText(),comboBox.getSelectedItem().toString(),plist,textField.getText());
                    //run the script
                    task.execute();
                }
            }
        }
    });
    btnNewButton.setBounds(227, 523, 126, 30);
    getContentPane().add(btnNewButton);

    JButton btnNewButton_1 = new JButton("+");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            CompModel.addRow("");
        }
    });
    btnNewButton_1.setBounds(566, 481, 43, 23);
    getContentPane().add(btnNewButton_1);

    JLabel lblPleaseSelectThe = new JLabel("Please select the log files directories");
    lblPleaseSelectThe.setForeground(Color.RED);
    lblPleaseSelectThe.setHorizontalAlignment(SwingConstants.CENTER);
    lblPleaseSelectThe.setBounds(180, 300, 297, 14);
    getContentPane().add(lblPleaseSelectThe);

    JLabel lblNewLabel_1 = new JLabel("Add New Path");
    lblNewLabel_1.setFont(new Font("Tahoma", Font.ITALIC, 11));
    lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
    lblNewLabel_1.setBounds(444, 481, 126, 23);
    getContentPane().add(lblNewLabel_1);

    Color my_color1 = new Color(204,74,20);
    JLabel lblRoleFindingScript = new JLabel("Role Finding Script ");
    lblRoleFindingScript.setFont(new Font("Tahoma", Font.BOLD, 15));
    lblRoleFindingScript.setHorizontalAlignment(SwingConstants.CENTER);
    lblRoleFindingScript.setForeground(my_color1);
    lblRoleFindingScript.setBounds(197, 43, 297, 30);
    getContentPane().add(lblRoleFindingScript);

    lblNewLabel_2 = new JLabel("");
    lblNewLabel_2.setForeground(Color.RED);
    lblNewLabel_2.setFont(new Font("Tahoma", Font.ITALIC, 10));
    lblNewLabel_2.setBounds(226, 150, 282, 14);
    getContentPane().add(lblNewLabel_2);

    pathWarningLabel = new JLabel();
    pathWarningLabel.setFont(new Font("Tahoma", Font.ITALIC, 11));
    pathWarningLabel.setHorizontalAlignment(SwingConstants.CENTER);
    pathWarningLabel.setForeground(Color.RED);
    pathWarningLabel.setBounds(140, 444, 384, 14);
    pathWarningLabel.setVisible(false);
    getContentPane().add(pathWarningLabel);

    JLabel lblNewLabel = new JLabel("");
    lblNewLabel.setIcon(new ImageIcon("valeo2.png"));
    lblNewLabel.setBounds(10, 11, 172, 89);
    getContentPane().add(lblNewLabel);

    JLabel lblRegex = new JLabel("   Regex        :");
    lblRegex.setFont(new Font("Dialog", Font.BOLD, 13));
    lblRegex.setBounds(10, 239, 172, 30);
    border = BorderFactory.createEtchedBorder();
    lblRegex.setBorder(border);
    getContentPane().add(lblRegex);

}

public JTable CreateCompTable() {
    CompModel = new CompTableModel();

    CompModel.addRow(folder);
//  CompModel.addRow("");

    table = new JTable(CompModel);
    table.setRowHeight(new CompCellPanel().getPreferredSize().height);
    table.setTableHeader(null);
    CompCellEditorRenderer compCellEditorRenderer = new CompCellEditorRenderer();
    table.setDefaultRenderer(Object.class, compCellEditorRenderer);
    table.setDefaultEditor(Object.class, compCellEditorRenderer);
    return table;
}

class CompTableModel extends DefaultTableModel {

    private static final long serialVersionUID = 1L;

    @Override
    public int getColumnCount() {
        return 1;
    }

    public void addRow(String string) {
        super.addRow(new Object[]{new Comp(string)});

    }

}

class CompCellPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private JLabel labelWith = new JLabel("Path : ");
    private JTextField lowerField = new JTextField();
    private JButton browseButton = new JButton("browse");
    private JButton removeButton = new JButton("remove");

    CompCellPanel() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

        removeButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            //  JTable table = (JTable) SwingUtilities.getAncestorOfClass(JTable.class, (Component) e.getSource());
                int row = table.getEditingRow();
                table.getCellEditor().stopCellEditing();
                ((DefaultTableModel) table.getModel()).removeRow(row);
            }
        });

        browseButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            //  JTable table = (JTable) SwingUtilities.getAncestorOfClass(JTable.class, (Component) e.getSource());
                int row = table.getEditingRow();
                Chooser frame = new Chooser();
                table.getModel().setValueAt(frame.fileName, row, 0);
                lowerField.setText(frame.fileName);
            }
        });

        lowerField.setEditable(false);

        add(labelWith);
        add(lowerField);
        add(browseButton);
        add(removeButton);

    }
    public void setComp(Comp Comp) {
        lowerField.setText(Comp.lower); 
    }

    public Comp getComp() {
        return new Comp(lowerField.getText());
    }
}

class CompCellEditorRenderer extends AbstractCellEditor implements TableCellRenderer, TableCellEditor {

    private static final long serialVersionUID = 1L;
    private CompCellPanel renderer = new CompCellPanel();
    private CompCellPanel editor = new CompCellPanel();

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        renderer.setComp((Comp) value);
        return renderer;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        editor.setComp((Comp) value);
        return editor;
    }

    @Override
    public Object getCellEditorValue() {
        return editor.getComp();
    }

    @Override
    public boolean isCellEditable(EventObject anEvent) {
        return true;
    }

    @Override
    public boolean shouldSelectCell(EventObject anEvent) {
        return false;
    }
}
class Comp {
    String lower;

    public Comp(String text) {
        this.lower = text;
    }
}

class Chooser extends JPanel {
    //File chooser class
    JFileChooser chooser;
    String fileName;

    public Chooser() {
        chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    //  chooser.setCurrentDirectory(lastDirectory);
        int r = chooser.showOpenDialog(new JPanel());
        if (r == JFileChooser.APPROVE_OPTION) {
            fileName = chooser.getSelectedFile().getPath();
    //      lastDirectory = chooser.getSelectedFile();
        }
    }
}
  }
public类MainGui扩展JFrame{
私有JPanel内容窗格;
私有JTextField用户名textfield;
专用JLabel搜索标签,lblNewLabel_2;
私有JTable CompTable=null;
私有CompTableModel CompModel=null;
私有JButton addButton=null;
private String folder=File.separator+File.separator+“bie-pvcs-01”+File.separator+“logs\u 122”+File.separator;
专用JComboBox组合框;
专用JLabel路径警告标签;
private ArrayList plist=new ArrayList();
File lastDirectory=新文件(File.separator+File.separator+“bie-pvcs-01”+File.separator+“logs_122”+File.separator);
私有JTextField textField;
专用JTable表;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
试一试{
for(LookAndFeelInfo:UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
UIManager.setLookAndFeel(info.getClassName());
打破
}
}
}捕获(例外e){
//如果Nimbus不可用,您可以将GUI设置为其他外观。
}
invokeLater(新的Runnable(){
公开募捐{
试一试{
MainGui frame=newmaingui();
维度dim=Toolkit.getDefaultToolkit().getScreenSize();
int w=frame.getSize().width;
int h=frame.getSize()高度;
INTX=(尺寸宽度-w)/4;
int y=(尺寸高度-h)/2;
帧设置位置(x,y);
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公共MainGui(){
getContentPane().setBackground(SystemColor.InactiveCaptContext);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100650650);
contentPane=newjpanel();
getContentPane().setLayout(null);
setTitle(“角色查找脚本”);
CompTable=CreateCompTable();
JScrollPane CompTableScrollpane=新的JScrollPane(CompTable,JScrollPane.VERTICAL\u滚动条\u根据需要,
JScrollPane.水平滚动条(根据需要);
CompTableScrollpane.setBounds(10325599108);
CompTableScrollpane.getViewport().setBackground(颜色.白色);
getContentPane().add(CompTableScrollpane);
JLabel lblUsername=新的JLabel(“用户名:”);
setFont(新字体(“LucidaSans”,Font.BOLD,13));
lblUsername.挫折(101111172,30);
Border Border=BorderFactory.createRaisedSoftBevelOrder();
border=BorderFactory.create蚀刻边界();
lblUsername.订单(边界);
getContentPane().add(lblUsername);
userNametextField=newjtextfield();
setBounds(216111393,30);
setFont(新字体(“LucidaSans”,Font.PLAIN,13));
getContentPane().add(userNametextField);
userNametextField.setColumns(20);
JLabel lblRole=新的JLabel(“角色:”);
setFont(新字体(“LucidaSans”,Font.BOLD,13));
lblRole.立根(10174172,30);
border=BorderFactory.create蚀刻边界();
lblRole.setboorder(边框);
getContentPane().add(lblRole);
comboBox=新的JComboBox();
组合框.立根(216,175,393,30);
//comboBox.addItem(“变更管理人”);
//comboBox.addItem(“PCMS-PART-MANAGER”);
//comboBox.addItem(“PMCS-ROLE-MANAGER”);
comboBox.addItem(“VR_分析师”);
getContentPane().add(组合框);
textField=新的JTextField();
setFont(新字体(“Dialog”,Font.PLAIN,13));
textField.setColumns(20);
textField.setBounds(216239393,30);
getContentPane().add(文本字段);
searchLabel=new JLabel(“正在搜索..请稍候..”);
searchLabel.setFont(新字体(“LucidaSans”,Font.BOLD,13));
searchLabel.setBounds(21656419530);
searchLabel.setVisible(false);
getContentPane().add(searchLabel);
JButton btnNewButton=新JButton(“执行!”);
setFont(新字体(“LucidaSans”,Font.BOLD,13));
addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
//检查用户名
System.out.println(CompTable.getRowCount());
if(userNametextField.getText()==null | | userNametextField.getText().equals(“”){
lblNewLabel_2.setText(“请填写用户名部分!”);
}else if(CompTable.getRowCount()==0){
pathWarningLabel.setText(“请至少添加一条路径…”);
pathWarningLabel.setVisible(true);
}否则{

对于(int i=0;i似乎您将值存储在
JTabel
中,就像您自己的对象(
Comp
)一样。尝试在
btnNewButton
ActionListener
方法的下一行替换:

 for(int i=0; i<CompTable.getRowCount(); i++){
       if(CompTable.getValueAt(i, 0).toString() != null && !CompTable.getValueAt(i, 0).toString().equals("")){
             plist.add(CompTable.getValueAt(i,0).toString());
       }
 }
for(int i=0;i
   for(int i=0; i<CompTable.getRowCount(); i++){
         String value = ((Comp)CompTable.getValueAt(i, 0)).lower;
         if(value!= null && !value.isEmpty()){
               plist.add(value);
         }
   }