我尝试设置默认路径并将此值设置为JtextField。当我添加新行并选择它的工作路径时。但是当脚本第一次启动时,我尝试将默认路径提供给第一行,但它没有采用正确的字符串。
这是截图:我给出了路径 - > // bie-pvcs-01 / logs_122但它需要role_script.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();
}
}
}
}
答案 0 :(得分:1)
似乎您将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<CompTable.getRowCount(); i++){
String value = ((Comp)CompTable.getValueAt(i, 0)).lower;
if(value!= null && !value.isEmpty()){
plist.add(value);
}
}
它可以帮助你。因为当您使用getValueAt()
方法获得Comp
实例时,您可以将其转换并从该对象获取值。
另请阅读name convention。