私の最初の「パスワードボールト」タブでは、パスワードを保存する「ウェブサイト」のリストを持つJListがあります。 2番目のタブには「パスワード」機能が追加されています。パスワードを追加すると、JListはペインでのみ更新されます。もう一度他のタブ「パスワードボールト」に切り替えると、更新されていません。プログラムを再起動すると、更新されました。フィールドを使用するので、ローカル変数の問題ではありません。JListが別のペインで更新されない(Swing、Java)
public final class PasswordManagerView1 extends JFrame
implements PasswordManagerView, ListSelectionListener {
/**
* JList.
*/
private JList<String> list;
/**
* JButtons.
*/
private JButton enter, unlock, reset, buttonAddEnter;
/**
* Controller.
*/
private PasswordManagerController controller;
/**
* Dimensions.
*/
private Dimension maxSize;
/**
* JTabbedPanes.
*/
private JTabbedPane tabbedPane;
/**
* JTextField.
*/
private JTextField passwordDisplay, textField;
/**
* PasswordField.
*/
private JPasswordField passwordField, resetField, passwordFieldadd;
/**
* Useful Constants.
*/
private static final int MAX_SIZE_HORI = 800, MAX_SIZE_VERTI = 400,
EMPTY_BORDER_SIZE = 5;
/**
* Constructor.
*/
public PasswordManagerView1() {
super("Password Manager");
JTabbedPane tabbedPane = new JTabbedPane();
//Initial JPanel creation
tabbedPane.setBorder(
new EmptyBorder(PasswordManagerView1.EMPTY_BORDER_SIZE,
PasswordManagerView1.EMPTY_BORDER_SIZE,
PasswordManagerView1.EMPTY_BORDER_SIZE,
PasswordManagerView1.EMPTY_BORDER_SIZE));
this.maxSize = new Dimension(PasswordManagerView1.MAX_SIZE_HORI,
PasswordManagerView1.MAX_SIZE_VERTI);
tabbedPane.setPreferredSize(this.maxSize);
this.getContentPane().add(tabbedPane);
//Initial JTabbedPane creation
//Tab creation
JComponent panel1 = this.makePasswordVault();
ImageIcon icon = new ImageIcon("lock-icon.png");
tabbedPane.addTab("Password Vault", icon, panel1,
"View the passwords in the vault");
JComponent panel3 = this.makeAddPanel("Add Password");
tabbedPane.addTab("Add Password", icon, panel3,
"Add passwords to the vault");
//JComponent panel2 = this.makeAddPanel("ALSO ADDS");
//tabbedPane.addTab("Delete Password", icon, panel2,
// "Deletes a password from the vault");
JComponent panel4 = this.makeInfoPanel();
tabbedPane.addTab("Info", icon, panel4,
"View settings and program info");
//Pack up
this.setResizable(false);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public JComponent makeAddPanel(String text) {
JPanel panel = new JPanel();
panel.setLayout(null);
//Creation of string array of with stores
String[] listContentC = this.getStore();
this.list = new JList<>(listContentC);
this.list.setBounds(0, 0, 233, 360);
JScrollPane pane = new JScrollPane(this.list);
pane.setBounds(0, 0, 233, 360);
panel.add(pane);
JLabel labelAdd = new JLabel("Add Password");
labelAdd.setHorizontalAlignment(SwingConstants.CENTER);
labelAdd.setFont(new Font("Times New Roman", Font.PLAIN, 24));
labelAdd.setBounds(427, 38, 163, 31);
panel.add(labelAdd);
this.passwordFieldadd = new JPasswordField();
this.passwordFieldadd.setBounds(536, 100, 116, 22);
panel.add(this.passwordFieldadd);
this.textField = new JTextField();
this.textField.setBounds(375, 100, 116, 22);
panel.add(this.textField);
JLabel lblWebsite = new JLabel("Website");
lblWebsite.setHorizontalAlignment(SwingConstants.CENTER);
lblWebsite.setFont(new Font("Times New Roman", Font.PLAIN, 13));
lblWebsite.setBounds(401, 126, 56, 16);
panel.add(lblWebsite);
JLabel lblPassword = new JLabel("Password");
lblPassword.setHorizontalAlignment(SwingConstants.CENTER);
lblPassword.setFont(new Font("Times New Roman", Font.PLAIN, 13));
lblPassword.setBounds(566, 126, 56, 16);
panel.add(lblPassword);
this.buttonAddEnter = new JButton("Enter");
this.buttonAddEnter.setBounds(465, 161, 97, 25);
panel.add(this.buttonAddEnter);
this.buttonAddEnter.addActionListener(this);
return panel;
}
@Override
public JComponent makeInfoPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 1));
StringBuilder toPrint = new StringBuilder();
SimpleReader in = new SimpleReader1L("data/Notice.txt");
while (!in.atEOS()) {
toPrint.append(in.nextLine() + "\n");
}
String toPrintString = toPrint.toString();
JTextArea noticeText = new JTextArea(toPrintString);
noticeText.setEditable(false);
JScrollPane noticeTextScroll = new JScrollPane(noticeText);
panel.add(noticeTextScroll);
in.close();
return panel;
}
@Override
public JComponent makePasswordVault() {
JPanel panel = new JPanel();
panel.setLayout(null);
/*
* Ask for help on this.
*
* I would have liked to create the listContentC by passing nothing into
* the controller and then the controller using the model to extract the
* data from a text file. However, my array was always initialized to
* something null and caused a runtime error.
*
*
*/
//Creation of string array of with stores
String[] listContentC = this.getStore();
//GUI setup of list
this.list = new JList<>(listContentC);
this.list.setLayoutOrientation(JList.VERTICAL);
this.list.addListSelectionListener(this);
JScrollPane pane = new JScrollPane(this.list);
pane.setBounds(0, 0, 233, 360);
panel.add(pane);
//The label creation (for instructions)
JLabel labelA = new JLabel("the store and press enter!");
labelA.setFont(new Font("Times New Roman", Font.PLAIN, 13));
labelA.setBounds(294, 70, 157, 31);
panel.add(labelA);
JLabel labelB = new JLabel("To view a password, click on");
labelB.setFont(new Font("Times New Roman", Font.PLAIN, 13));
labelB.setBounds(284, 54, 163, 31);
panel.add(labelB);
//Enter button creation
this.enter = new JButton("Enter");
this.enter.setBounds(303, 128, 116, 25);
panel.add(this.enter);
this.enter.setEnabled(false);
this.enter.addActionListener(this);
//Password Display field creation
this.passwordDisplay = new JTextField();
this.passwordDisplay.setBackground(Color.WHITE);
this.passwordDisplay.setEditable(false);
this.passwordDisplay
.setFont(new Font("Times New Roman", Font.PLAIN, 13));
this.passwordDisplay.setHorizontalAlignment(SwingConstants.CENTER);
this.passwordDisplay.setText("");
this.passwordDisplay.setBounds(303, 247, 116, 22);
panel.add(this.passwordDisplay);
this.passwordDisplay.setColumns(10);
//Password Label creation
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setHorizontalAlignment(SwingConstants.CENTER);
passwordLabel.setFont(new Font("Times New Roman", Font.PLAIN, 13));
passwordLabel.setBounds(336, 218, 56, 16);
panel.add(passwordLabel);
//Master password notice
JLabel mastPass = new JLabel("Enter master password to unlock vault:");
mastPass.setFont(new Font("Times New Roman", Font.PLAIN, 13));
mastPass.setBounds(532, 54, 218, 31);
panel.add(mastPass);
//Password Field
this.passwordField = new JPasswordField();
this.passwordField.setBounds(532, 128, 116, 24);
panel.add(this.passwordField);
//Unlock button
this.unlock = new JButton("Unlock");
this.unlock.setFont(new Font("Times New Roman", Font.PLAIN, 13));
this.unlock.setBounds(660, 127, 97, 25);
panel.add(this.unlock);
this.unlock.addActionListener(this);
//New setup label
JLabel labelC = new JLabel("Reset/Set up new master pass:");
labelC.setHorizontalAlignment(SwingConstants.CENTER);
labelC.setFont(new Font("Times New Roman", Font.PLAIN, 13));
labelC.setBounds(532, 217, 218, 16);
panel.add(labelC);
//New setup label
JLabel defaultLabel = new JLabel("Default Password = \"password\"");
defaultLabel.setHorizontalAlignment(SwingConstants.CENTER);
defaultLabel.setFont(new Font("Times New Roman", Font.PLAIN, 13));
defaultLabel.setBounds(560, 77, 171, 16);
panel.add(defaultLabel);
this.resetField = new JPasswordField();
this.resetField.setBounds(532, 246, 116, 24);
panel.add(this.resetField);
this.reset = new JButton("Update");
this.reset.setFont(new Font("Times New Roman", Font.PLAIN, 13));
this.reset.setBounds(660, 245, 97, 25);
this.reset.setEnabled(false);
panel.add(this.reset);
this.reset.addActionListener(this);
return panel;
}
@Override
public String[] getStore() {
int storeCount = 0;
SimpleReader in = new SimpleReader1L("data/store.txt");
while (!in.atEOS()) {
storeCount++;
in.nextLine();
}
in.close();
String[] listContentC = new String[storeCount];
SimpleReader in2 = new SimpleReader1L("data/store.txt");
for (int i = 0; i < storeCount; i++) {
listContentC[i] = in2.nextLine();
}
in2.close();
return listContentC;
}
@Override
public void registerObserver(PasswordManagerController controller) {
this.controller = controller;
}
@Override
public void updateEnterButtonVaultTab(boolean result) {
this.enter.setEnabled(result);
}
@Override
public void actionPerformed(ActionEvent event) {
//Wait cursor
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//What button was pressed
Object source = event.getSource();
if (source == this.enter) {
int index = this.list.getSelectedIndex();
System.out.println(index);
if (this.list.getSelectedIndex() != -1) {
this.controller.processEnterEvent(this.list.getSelectedValue());
}
} else if (source == this.unlock) {
this.controller
.processUnlockEvent(this.passwordField.getPassword());
} else if (source == this.reset) {
this.controller.processResetEvent(this.resetField.getPassword());
} else if (source == this.buttonAddEnter) {
this.controller.processAddEvent(this.textField.getText(),
this.passwordFieldadd.getPassword());
//needs done in model
this.updateListModel();
}
this.setCursor(Cursor.getDefaultCursor());
}
@Override
public void updateListModel() {
//MOVE TO THE MODEL
String[] temp = this.getStore();
DefaultListModel<String> temp2 = new DefaultListModel<>();
for (int i = 0; i < temp.length; i++) {
temp2.addElement(temp[i]);
}
this.list.setModel(temp2);
}
@Override
public JTabbedPane getTabbedPane() {
return this.tabbedPane;
}
@Override
public void setTabbedPane(JTabbedPane tabbedPane) {
this.tabbedPane = tabbedPane;
}
@Override
public void updatePasswordField() {
this.passwordField.setText("");
}
@Override
public void updateResetPasswordDisplay() {
this.resetField.setText("");
}
@Override
public void displayWrongPass() {
JOptionPane.showMessageDialog(this, "Wrong password entered!");
}
@Override
public void displayUpdatedPass() {
JOptionPane.showMessageDialog(this, "Master password updated!");
}
@Override
public void updateResetButton(boolean result) {
this.reset.setEnabled(result);
}
@Override
public void updatePasswordDisplay(char[] password) {
StringBuilder passwordCreation = new StringBuilder();
for (int i = 0; i < password.length; i++) {
passwordCreation.append(password[i]);
}
this.passwordDisplay.setText(passwordCreation.toString());
}
@Override
public JComponent makeTextPanel(String text) {
// TODO Auto-generated method stub
return null;
}
@Override
public void valueChanged(ListSelectionEvent arg0) {
}
}
[MCVE]を投稿したり、[:また私は、コンパイル、実行、および問題や解決策を示し、minimal code example programまたはMCVEするために必要な最小に近くなるようにコードを最小限に抑える方法をチェックしてくださいショート、自己包含、正しい例](http://www.sscce.org/)。 I. MCVE/SSCCEには、コンパイルに必要なインポート、それを実行する 'main(String [])'メソッド、そして問題を示すために明示的に必要とされないコードをすべて削除する必要があります。 –
私はAndrewThompsonの提案を2番目に挙げます。私は、上記のエラーの明白な原因は見当たりません。そして、おそらく複数のPasswordManagerViewインスタンスを作成しているかもしれません。私たちの推測を避けるために、[mcve]または[sscce](http://sscce.org)を作成してください。 –
面倒な点は、ヌルレイアウトを使用することです。ヌルレイアウトと 'setBounds()'はSwingの初心者にとっては複雑なGUIを作成する最も簡単で最良の方法のように思えるかもしれませんが、Swing GUIを使用すると、より多くの深刻な問題が発生します。 GUIのサイズが変更されたときにコンポーネントのサイズを変更することはありません。これらのコンポーネントは、拡張または維持するロイヤルウィジェットであり、スクロールペインに配置すると完全に失敗し、元のプラットフォームとは異なるすべてのプラットフォームまたは画面解像度で表示されます。 –