Reputation: 13
I try to make a program where I save my links and password or any short words that can help me , a make this code.
This is the main class:
import javax.swing.*;
import java.awt.*;
public static void main(String[] args) {
Second ob = new Second();
ob.setLocation(450,30);
ob.setSize(600, 700);
ob.setVisible(true);
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
and here is the second class
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class Second extends JFrame {
private int i;
private int numberlink = 200;//numarul maxim de linkuri
private int[] deleteTable = new int[numberlink];
private JPanel pan1;
private JButton[] buton = new JButton[numberlink];
private Box box;
private String namefile = "stocarenume.txt";
private int numberbutton;
private Scanner scaner;
private String array_nume[] = new String[numberlink];
private String array_url[] = new String[numberlink];
private JButton butonadd;
private ActionListener listener, listenerDelete;
private String namefile_url = "stocare_url.txt";
private BufferedWriter file_set;
private JScrollPane scrollpane;
private GridBagConstraints constraints = new GridBagConstraints();
private JButton del = new JButton("Delete");
private JRadioButton[] deleteCeck = new JRadioButton[numberlink];
private JPanel pan2 = new JPanel();
private JButton delete = new JButton("Delete");
Second() {
box = new Box(BoxLayout.Y_AXIS);
pan1 = new JPanel();
pan1.setBackground(Color.gray);
pan1.setLocation(0, 0);
add(pan1, BorderLayout.NORTH);
butonadd = new JButton("Add");
pan1.add(butonadd);
pan1.add(delete);
//am facut o variabil listener in care am suprascris pentru cele numberlink de butoane
// in care am facut sa imi returneze linkul din array_url
listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
for (i = 0; i < numberlink; i++) {
if (array_nume[i] == e.getActionCommand()) {
String myString = array_url[i];
//iti pune in clipboard linkul ,ca si cum ai da copy la un test dar tu de fapt apesi doar pe un buton
StringSelection stringSelection = new StringSelection(myString);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);
}
}
}
}
};
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "Delete") {
for (i = 0; i < numberlink; i++) {
if (deleteTable[i] == 1) {
array_url[i] = "xxxxxx";
array_nume[i] = "xxxxxx";
}
}
scriere_fisier();
}
}
});
listenerDelete = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object c = (Object) e.getSource();
for (i = 0; i < numberlink; i++) {
if (c == deleteCeck[i]) {
deleteTable[i] = 1;
}
}
}
};
butonadd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "Add") {
numberbutton++;
String nume;
String url;
nume = JOptionPane.showInputDialog(null, "Introduceti numele ");
url = JOptionPane.showInputDialog(null, "Introduceti url ");
array_nume[numberbutton] = nume;
array_url[numberbutton] = url;
scriere_fisier();
}
}
});
citirefisier();
creerebutoane();
}
void citirefisier() {
try {
scaner = new Scanner(new File(namefile));
i = 0;
while (scaner.hasNext()) {
array_nume[i] = scaner.nextLine();
i++;
}
numberbutton = i - 1;
} catch (Exception e) {
}
try {
scaner = new Scanner(new File(namefile_url));
i = 0;
while (scaner.hasNext()) {
array_url[i] = scaner.nextLine();
i++;
}
} catch (Exception e) {
}
}
void creerebutoane() {
pan2.setLayout(new GridBagLayout());
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 0, 0, 0);
constraints.anchor = GridBagConstraints.CENTER;
for (i = 1; i <= numberbutton; i++) {
buton[i] = new JButton(array_nume[i]);
buton[i].addActionListener(listener);
pan2.add(buton[i], constraints);
constraints.gridy++;
}
constraints.gridx = 2;
constraints.gridy = 0;
constraints.insets = new Insets(10, 50, 0, 0);
for (i = 1; i <= numberbutton; i++) {
deleteCeck[i] = new JRadioButton();
deleteCeck[i].addActionListener(listenerDelete);
pan2.add(deleteCeck[i], constraints);
constraints.gridy++;
}
scrollpane = new JScrollPane(pan2);
Dimension d = new Dimension(500, 200);
scrollpane.getVerticalScrollBar().setUnitIncrement(d.height);
d.height *= 10; // Show at least 10 buttons
scrollpane.getViewport().setPreferredSize(d);
add(scrollpane);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
void scriere_fisier() {
try {
file_set = new BufferedWriter(new FileWriter(namefile));
for (i = 1; i <= numberbutton; i++) {
file_set = new BufferedWriter(new FileWriter(namefile, true));
if (array_nume[i].equals("xxxxxx")) {
} else {
file_set.newLine();
file_set.write(array_nume[i]);
}
file_set.close();
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "nu sa putut scrie in " + namefile);
}
try {
file_set = new BufferedWriter(new FileWriter(namefile_url));
for (i = 1; i <= numberbutton; i++) {
file_set = new BufferedWriter(new FileWriter(namefile_url, true));
if (array_url[i].equals("xxxxxx")) {
} else {
file_set.newLine();
file_set.write(array_url[i]);
}
file_set.close();
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "nu sa putut scrie in " + namefile_url);
}
}
}
And now I try to explain what I make , a make 2 file where save the url as the name.
Here is a image where look the app:
And when a press add show me 2 InputDialog and a write name and what I want to save , and the problem is I add something or I delete something but I need to close and open again the program to see the change that I make, I try with repaint to refresh the pan2 , but nothing.
Upvotes: 0
Views: 117
Reputation: 347314
Your code is all over the place, for example, you create a delete and add listener which do nothing and then add different listeners to the buttons, so I have no idea what's actually going on...
This...
if (e.getActionCommand() == "Add") {
is not how String
comparison works in Java. If you're making this simple mistake, then you might not be ready for something as complex as GUI programing
Based on what I've read, when you add or remove an item, you update the model, save it, but never actually add any components to the screen.
Having said all that, I would suggest a different approach and use either a JList
or a JTable
, for example
Upvotes: 2
Reputation: 11
I think you should revalidate first and then repaint after any change. Revalidate check if there is any change in the component. Also you can use a JButton when you click on it reload all content.
Like a suggestion is more reliable if you have more separated your code. All the logic in one or two classes is not good. A simple program does not mean that good programming practices have to be abandoned.
Upvotes: 0