Salve galera do forum!
tipo, estou com problemas aqui pra fazer um dicionário. Utilizei de outros tutos daqui pra conseguir ler o arquivo, e me dei bem!
o grande problema, é que eu preciso colocar um botão para adicionar palavras nesse dicionário ( tmb num intendi pra que, jah q se eu sei do significado, num preciso de dicionário
).
o foda é que eu coloquei um vetor para ler o arquivo, e vetor é finito. Me disseram algo de lista, mas não achei nada referente a isso.
Segue o código :
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Dicionario implements ActionListener {
// janela
JFrame janela = new JFrame();
// campo de busca
JTextField busca = new JTextField();
// botão de procurar
JButton procurar = new JButton("Procurar");
// lista de palavras
DefaultListModel listModel;
JList palavras;
JScrollPane painellista;
JScrollPane painelsignificados;
// campo de significados
JTextArea significados = new JTextArea();
// botão sobre
JButton sobre = new JButton("Sobre");
// botões de temas
JButton tema1 = new JButton("Tema 1");
JButton tema2 = new JButton("Tema 2");
JPanel painel = new JPanel();
// Itens dos vetores das linhas
static String linha;
static int n = 0;
static String[] vetorPalavras;
static String[] vetorSignificados;
static String linha2;
static int m = 0;
public static void main(String args[]) throws IOException {
// Pegando linhas do texto de palavras(devem ser colocados os numeros de
// termos dentro do vetor)
vetorPalavras = new String[5];
FileReader reader = new FileReader(new File("texto1.txt"));
BufferedReader leitor = new BufferedReader(reader, 1024 * 1024);
while ((linha = leitor.readLine()) != null) {
vetorPalavras[n] = linha;
n = n + 1;
}
// Pegando linhas do texto de significados
FileReader reader2 = new FileReader(new File("texto2.txt"));
BufferedReader leitor2 = new BufferedReader(reader2, 1024 * 1024);
vetorSignificados = new String[5];
String linha2;
int m = 0;
while ((linha2 = leitor2.readLine()) != null) {
vetorSignificados[m] = linha2;
m = m + 1;
}
new Dicionario();
}
Dicionario() {
// janela
janela.setTitle("Dicionário de Informática");
janela.setLocation(300, 200);
janela.setSize(700, 389);
janela.setResizable(false);
janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
janela.setVisible(true);
janela.add(painel);
// Layout dos elementos
painel.setLayout(null);
painel.setBackground(new Color(0, 124, 249));
// add campo de busca
painel.add(busca);
busca.setBounds(20, 10, 460, 35);
busca.setFont(new Font("SansSerif", Font.BOLD, 16));
// botão de procurar
painel.add(procurar);
procurar.setBounds(500, 0, 180, 50);
procurar.addActionListener(this);
// botão de sobre
painel.add(sobre);
sobre.setBounds(610, 330, 80, 23);
sobre.setBackground(Color.orange);
sobre.setBorder(BorderFactory.createBevelBorder(1, Color.white,
Color.black));
sobre.addActionListener(this);
// botão tema2
painel.add(tema2);
tema2.setBounds(500, 330, 100, 23);
tema2.setBackground(Color.gray);
tema2.setBorder(BorderFactory.createBevelBorder(1, Color.white,
Color.black));
tema2.addActionListener(this);
// painel palavras(lista)
listModel = new DefaultListModel();
palavras = new JList(listModel);
painellista = new JScrollPane(palavras);
painel.add(painellista);
painellista.setBounds(20, 60, 180, 285);
palavras.setFont(new Font("SansSerif", Font.BOLD, 16));
palavras.setBackground(new Color(170, 255, 85));
palavras.setSelectionBackground(Color.orange);
// painel significados
painelsignificados = new JScrollPane(significados);
painel.add(painelsignificados);
painelsignificados.setBounds(220, 60, 470, 270);
significados.setFont(new Font("SansSerif", Font.PLAIN, 14));
significados.setEditable(true);
significados.setBackground(Color.white);
painel.add(painellista);
painellista.setBounds(20, 60, 180, 285);
palavras.setFont(new Font("SansSerif", Font.BOLD, 16));
palavras.setBackground(new Color(170, 255, 85));
// AQUI DEVEM SER ADICIONADOS OS ELEMENTOS DA LISTA DE PALAVRAS
int i = 0;
while (i <= vetorPalavras.length) {
listModel.addElement(vetorPalavras[i]);
i = i + 1;
}
}
// Ações dos botões
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sobre) {
JOptionPane
.showMessageDialog(
null,
"Criado por: \n Danilo Meneses Cataneo \n Ricardo Gonçalves Rodrigues \n Victor Fideles Freitas");
}
if (e.getSource() == tema1) {
painel.setBackground(new Color(0, 124, 249));
janela.setLocation(300, 200);
janela.setSize(700, 389);
busca.setBounds(20, 10, 460, 35);
procurar.setBounds(500, 0, 180, 50);
painellista.setBounds(20, 60, 180, 285);
palavras.setBackground(new Color(170, 255, 85));
palavras.setSelectionBackground(Color.orange);
painelsignificados.setBounds(220, 60, 470, 270);
significados.setBackground(Color.white);
significados.setForeground(Color.black);
painel.add(tema2);
tema2.setBounds(500, 330, 100, 23);
tema2.setBackground(Color.gray);
tema2.setBorder(BorderFactory.createBevelBorder(1, Color.white,
Color.black));
tema2.addActionListener(this);
}
if (e.getSource() == tema2) {
painel.setBackground(new Color(0, 0, 0));
palavras.setBackground(new Color(229, 229, 229));
palavras.setSelectionBackground(new Color(181, 230, 29));
janela.setLocation(5, 5);
janela.setSize(300, 730);
busca.setBounds(10, 10, 170, 30);
procurar.setBounds(185, 10, 100, 30);
painellista.setBounds(5, 50, 280, 200);
painelsignificados.setBounds(5, 260, 280, 400);
significados.setBackground(new Color(65, 65, 65));
significados.setForeground(Color.white);
painel.add(tema1);
tema1.setBackground(new Color(0, 124, 249));
tema1.setBorder(BorderFactory.createBevelBorder(1, Color.white,
Color.black));
tema1.addActionListener(this);
tema1.setBounds(10, 670, 100, 23);
}
if (e.getSource() == procurar) {
int u = 0;
while (u <= (vetorPalavras.length) - 1) {
listModel.remove(0);
u = u + 1;
}
// PROBLEMA AQUI
String coisaDigitada = busca.getText();
coisaDigitada = coisaDigitada.toLowerCase();
for (int v = 0; v < vetorPalavras.length; v = v + 1) {
if (vetorPalavras[v].startsWith(coisaDigitada)) {
listModel.addElement(vetorPalavras[v]);
}
}
}
}
}
se alguém puder me ajudar a fazer isso com outra coisa ao invés de vetor…
VLW!