Mam problem, mam licznik znakow w notatniku lecz chcialbym zeby on pokazywal na biezaco liczbe znakow a nie dopiero gdy klikne button obok niego i sprawdze jak zrobic by odswiezalo ten licznik co jakis czas? licznik jest w case 42
package notat;
import java.awt.BorderLayout;
import java.awt.Color;
import static java.awt.Color.black;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
public class Notatnik extends JFrame implements ActionListener {
private JTextArea textArea;
private JCheckBox zawijanie, pogrubienie, kursywa;
private String tekstKop, czcionka = "Arial";
private JTextField poleTekst, wskazuje;
public Notatnik() {
super();
setTitle("Notatnik");
Toolkit zestaw = Toolkit.getDefaultToolkit();
Dimension rozmiarEkranu = zestaw.getScreenSize();
int szerEkranu = rozmiarEkranu.width;
int wysEkranu = rozmiarEkranu.height;
setBounds(szerEkranu / 4, wysEkranu / 4, szerEkranu / 2, wysEkranu / 2);
setResizable(false);
setJMenuBar(doJMenuBar());
setLayout(new BorderLayout());
add(doTextArea(), BorderLayout.CENTER);
add(doButtons(), BorderLayout.SOUTH);
add((licznik()), BorderLayout.NORTH);
textArea.setComponentPopupMenu(popup());
}
public JScrollPane doTextArea() {
textArea = new JTextArea();
JScrollPane sp = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
return sp;
}
public JPopupMenu popup() {
JPopupMenu menuPopup = new JPopupMenu();
JMenuItem kopiuj = new JMenuItem("kopiuj");
kopiuj.addActionListener(this);
kopiuj.setActionCommand("91");
JMenuItem wklej = new JMenuItem("wklej");
wklej.addActionListener(this);
wklej.setActionCommand("92");
JMenuItem metal = new JMenuItem("metal look-and-feel");
metal.addActionListener(this);
metal.setActionCommand("93");
JMenuItem windows = new JMenuItem("windows look-and-feel");
windows.addActionListener(this);
windows.setActionCommand("94");
JMenuItem motif = new JMenuItem("motif look-and-feel");
motif.addActionListener(this);
motif.setActionCommand("95");
JMenuItem nimbus = new JMenuItem("nimbus look-and-feel");
nimbus.addActionListener(this);
nimbus.setActionCommand("96");
menuPopup.add(kopiuj);
menuPopup.add(wklej);
menuPopup.addSeparator();
menuPopup.add(metal);
menuPopup.add(windows);
menuPopup.add(motif);
menuPopup.add(nimbus);
return menuPopup;
}
public JPanel licznik() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton sprawdz = new JButton("Sprawdz");
sprawdz.addActionListener(this);
sprawdz.setActionCommand("42");
JLabel etykieta = new JLabel("Liczba znakow:");
wskazuje = new JTextField(5);
wskazuje.setEditable(false);
wskazuje.setHorizontalAlignment(JTextField.RIGHT);
panel.add(etykieta);
panel.add(wskazuje);
panel.add(sprawdz);
return panel;
}
public JPanel doButtons() {
JPanel panelPrzyciski = new JPanel(new GridLayout(1, 4));
JPanel panelLewy = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel panelSrodek = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel panelSrodek2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel panelPrawy = new JPanel(new FlowLayout(FlowLayout.CENTER));
panelPrzyciski.add(panelLewy);
panelPrzyciski.add(panelSrodek);
panelPrzyciski.add(panelSrodek2);
panelPrzyciski.add(panelPrawy);
JButton data = new JButton("data");
data.addActionListener(this);
data.setActionCommand("51");
JButton tytul = new JButton("tytul");
tytul.addActionListener(this);
tytul.setActionCommand("52");
JButton podpis = new JButton("podpis");
podpis.addActionListener(this);
podpis.setActionCommand("53");
panelLewy.add(data);
panelLewy.add(tytul);
panelLewy.add(podpis);
Border ramkaCiagla = BorderFactory.createLineBorder(black);
Border tytulCiagla = BorderFactory.createTitledBorder(ramkaCiagla, "Wstawki");
panelLewy.setBorder(tytulCiagla);
Border ramkaCiagla1 = BorderFactory.createLineBorder(black);
Border tytulCiagla1 = BorderFactory.createTitledBorder(ramkaCiagla1, "Kolory czcionek");
panelSrodek.setBorder(tytulCiagla1);
String[] czKolory = {"czerwona", "zielona", "niebieska", "czarna", "pomaranczowy"};
JLabel etyKolory = new JLabel("Kolory: ");
JComboBox kolorList = new JComboBox(czKolory);
kolorList.setSelectedIndex(3);
kolorList.addActionListener(this);
kolorList.setActionCommand("60");
panelSrodek.add(etyKolory);
panelSrodek.add(kolorList);
Border ramkaCiagla2 = BorderFactory.createLineBorder(black);
Border tytulCiagla2 = BorderFactory.createTitledBorder(ramkaCiagla2, "Wyszukiwawnie");
panelSrodek2.setBorder(tytulCiagla2);
poleTekst = new JTextField(10);
panelSrodek2.add(poleTekst);
JButton szukaj = new JButton("szukaj");
szukaj.addActionListener(this);
szukaj.setActionCommand("70");
panelSrodek2.add(szukaj);
Border ramkaCiagla3 = BorderFactory.createLineBorder(black);
Border tytulCiagla3 = BorderFactory.createTitledBorder(ramkaCiagla3, "Kolory tła");
panelPrawy.setBorder(tytulCiagla3);
JRadioButton bi = new JRadioButton("bialy", true);
bi.addActionListener(this);
bi.setActionCommand("81");
JRadioButton zo = new JRadioButton("zolty");
zo.addActionListener(this);
zo.setActionCommand("82");
JRadioButton zi = new JRadioButton("zielony");
zi.addActionListener(this);
zi.setActionCommand("83");
ButtonGroup bg1 = new ButtonGroup();
bg1.add(bi);
bg1.add(zo);
bg1.add(zi);
panelPrawy.add(bi);
panelPrawy.add(zo);
panelPrawy.add(zi);
return panelPrzyciski;
}
public JMenuBar doJMenuBar() {
JMenuBar pasekMenu = new JMenuBar();
JMenu mPlik = new JMenu("Plik");
mPlik.setMnemonic('P');
JMenuItem otworz = new JMenuItem("Otworz");
otworz.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));
otworz.addActionListener(this);
otworz.setActionCommand("11");
JMenuItem zapisz = new JMenuItem("Zapisz");
zapisz.setAccelerator(KeyStroke.getKeyStroke("ctrl Z"));
zapisz.addActionListener(this);
zapisz.setActionCommand("12");
JMenuItem zakoncz = new JMenuItem("Zakoncz");
zakoncz.setAccelerator(KeyStroke.getKeyStroke("ctrl K"));
zakoncz.addActionListener(this);
zakoncz.setActionCommand("13");
mPlik.add(otworz);
mPlik.add(zapisz);
mPlik.addSeparator();
mPlik.add(zakoncz);
JMenu mEdycja = new JMenu("Edycja");
mEdycja.setMnemonic('E');
JRadioButtonMenuItem serif = new JRadioButtonMenuItem("Czcionka Serif");
serif.addActionListener(this);
serif.setActionCommand("21");
JRadioButtonMenuItem arial = new JRadioButtonMenuItem("Czcionka Arial", true);
arial.addActionListener(this);
arial.setActionCommand("22");
JRadioButtonMenuItem forte = new JRadioButtonMenuItem("Czcionka Forte");
forte.addActionListener(this);
forte.setActionCommand("23");
JRadioButtonMenuItem georgia = new JRadioButtonMenuItem("Czcionka Georgia");
georgia.addActionListener(this);
georgia.setActionCommand("24");
ButtonGroup czcionki = new ButtonGroup();
czcionki.add(arial);
czcionki.add(serif);
czcionki.add(forte);
czcionki.add(georgia);
JRadioButtonMenuItem pomniejszC = new JRadioButtonMenuItem("Pomnijesz czcionke");
pomniejszC.addActionListener(this);
pomniejszC.setActionCommand("25");
JRadioButtonMenuItem normalC = new JRadioButtonMenuItem("Normalna Czcionka", true);
normalC.addActionListener(this);
normalC.setActionCommand("26");
JRadioButtonMenuItem powiekszC = new JRadioButtonMenuItem("Powieksz Czcionke");
powiekszC.addActionListener(this);
powiekszC.setActionCommand("27");
JMenuItem wyczysc = new JMenuItem("Wyczysc");
wyczysc.setAccelerator(KeyStroke.getKeyStroke("ctrl D"));
wyczysc.addActionListener(this);
wyczysc.setActionCommand("28");
mEdycja.add(serif);
mEdycja.add(arial);
mEdycja.add(forte);
mEdycja.add(georgia);
mEdycja.addSeparator();
mEdycja.add(pomniejszC);
mEdycja.add(normalC);
mEdycja.add(powiekszC);
mEdycja.addSeparator();
mEdycja.add(wyczysc);
ButtonGroup bg = new ButtonGroup();
bg.add(powiekszC);
bg.add(pomniejszC);
bg.add(normalC);
JMenu format = new JMenu("Format");
format.setMnemonic('f');
zawijanie = new JCheckBox("Zawijanie");
zawijanie.addActionListener(this);
zawijanie.setActionCommand("31");
pogrubienie = new JCheckBox("Pogrubienie");
pogrubienie.addActionListener(this);
pogrubienie.setActionCommand("32");
kursywa = new JCheckBox("Kursywa");
kursywa.addActionListener(this);
kursywa.setActionCommand("33");
format.add(zawijanie);
format.add(pogrubienie);
format.add(kursywa);
JMenu mPomoc = new JMenu("Pomoc");
mPomoc.setMnemonic('o');
JMenuItem oAutorze = new JMenuItem("O Autorze");
oAutorze.addActionListener(this);
oAutorze.setActionCommand("41");
mPomoc.add(oAutorze);
pasekMenu.add(mPlik);
pasekMenu.add(mEdycja);
pasekMenu.add(format);
pasekMenu.add(mPomoc);
return pasekMenu;
}
public static void main(String[] args) {
Notatnik nt = new Notatnik();
nt.setVisible(true);
nt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
switch (Integer.parseInt(e.getActionCommand())) {
case 11: {
JFileChooser pliki = new JFileChooser(".");
if (JFileChooser.APPROVE_OPTION == pliki.showOpenDialog(this)) {
try {
File f = pliki.getSelectedFile();
setTitle(f.getAbsolutePath() + " Notatnik");
BufferedReader br = new BufferedReader(new FileReader(f));
String temp = "";
while (br.ready()) {
temp += br.readLine() + "\n";
}
textArea.setText(temp);
} catch (IOException ex) {
System.out.println("Brak pliku");
}
}
break;
}
case 12: {
JFileChooser pliki = new JFileChooser(".");
if (JFileChooser.APPROVE_OPTION == pliki.showSaveDialog(this)) {
try {
File f = pliki.getSelectedFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write(textArea.getText());
bw.flush();
bw.close();
} catch (IOException ee) {
System.out.println("Problemy z zapisem");
}
}
break;
}
case 13: {
System.exit(0);
break;
}
case 21: {
czcionka = "Serif";
Font f = new Font(czcionka, textArea.getFont().getStyle(), textArea.getFont().getSize());
textArea.setFont(f);
break;
}
case 22: {
czcionka = "Arial";
Font f = new Font(czcionka, textArea.getFont().getStyle(), textArea.getFont().getSize());
textArea.setFont(f);
break;
}
case 23: {
czcionka = "Forte";
Font f = new Font(czcionka, textArea.getFont().getStyle(), textArea.getFont().getSize());
textArea.setFont(f);
break;
}
case 24: {
czcionka = "Georgia";
Font f = new Font(czcionka, textArea.getFont().getStyle(), textArea.getFont().getSize());
textArea.setFont(f);
break;
}
case 25: {
Font f = new Font(czcionka, Font.PLAIN, 10);
textArea.setFont(f);
break;
}
case 26: {
Font f = new Font(czcionka, Font.PLAIN, 12);
textArea.setFont(f);
break;
}
case 27: {
Font f = new Font(czcionka, Font.PLAIN, 22);
textArea.setFont(f);
break;
}
case 28: {
textArea.setText("");
break;
}
case 31: {
if (zawijanie.isSelected()) {
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
} else {
textArea.setLineWrap(false);
}
break;
}
case 32: {
if (pogrubienie.isSelected()) {
Font font = textArea.getFont();
textArea.setFont(font);
textArea.setFont(font.deriveFont(Font.BOLD));
} else if (kursywa.isSelected()) {
Font font = textArea.getFont();
textArea.setFont(font);
textArea.setFont(font.deriveFont(Font.ITALIC));
} else if (kursywa.isSelected() && pogrubienie.isSelected()) {
Font font = textArea.getFont();
textArea.setFont(font);
textArea.setFont(font.deriveFont(Font.ITALIC + Font.BOLD));
} else {
Font font = textArea.getFont();
textArea.setFont(font.deriveFont(Font.PLAIN));
}
break;
}
case 33: {
if (kursywa.isSelected()) {
Font font = textArea.getFont();
textArea.setFont(font.deriveFont(Font.ITALIC));
} else if (pogrubienie.isSelected()) {
Font font = textArea.getFont();
textArea.setFont(font);
textArea.setFont(font.deriveFont(Font.BOLD));
} else if (kursywa.isSelected() && pogrubienie.isSelected()) {
Font font = textArea.getFont();
textArea.setFont(font);
textArea.setFont(font.deriveFont(Font.ITALIC + Font.BOLD));
} else {
Font font = textArea.getFont();
textArea.setFont(font.deriveFont(Font.PLAIN));
}
break;
}
case 41: {
JOptionPane.showMessageDialog(this, "Mateusz Jabłoński");
break;
}
case 42: {
String tekst = textArea.getText();
tekst = tekst.replaceAll(" ", "").replaceAll("\n", "").replaceAll("\t", "");
wskazuje.setText(Integer.toString(tekst.length()));
break;
}
case 51: {
LocalDateTime localTime1 = LocalDateTime.now();
String data = localTime1.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
textArea.setText(data + "\n" + textArea.getText());
break;
}
case 52: {
textArea.setText("Szanowny Panie \n\n" + textArea.getText());
break;
}
case 53: {
textArea.setText(textArea.getText() + "\n\n Z poważaniem");
break;
}
case 60: {
JComboBox komboBox = (JComboBox) (e.getSource());
switch (komboBox.getSelectedIndex()) {
case 0: {
textArea.setForeground(Color.RED);
break;
}
case 1: {
textArea.setForeground(Color.GREEN);
break;
}
case 2: {
textArea.setForeground(Color.BLUE);
break;
}
case 3: {
textArea.setForeground(Color.BLACK);
break;
}
case 4: {
textArea.setForeground(Color.orange);
break;
}
}
break;
}
case 70: {
String tekst = textArea.getText().toLowerCase();
String szukany = poleTekst.getText();
String wystapienia = "";
int index;
int startIndex = 0;
while ((index = tekst.indexOf(szukany, startIndex)) != -1) {
startIndex = index + szukany.length();
wystapienia = wystapienia + " " + index;
}
JOptionPane.showMessageDialog(null, "Znaleziona frazę na pozycjach:" + wystapienia);
break;
}
case 81: {
textArea.setBackground(Color.WHITE);
break;
}
case 82: {
textArea.setBackground(Color.YELLOW);
break;
}
case 83: {
textArea.setBackground(Color.GREEN);
break;
}
case 91: {
tekstKop = textArea.getSelectedText();
break;
}
case 92: {
textArea.insert(tekstKop, textArea.getCaretPosition());
break;
}
case 93: {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
}
SwingUtilities.updateComponentTreeUI(this);
break;
}
case 94: {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
}
SwingUtilities.updateComponentTreeUI(this);
break;
}
case 95: {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
}
SwingUtilities.updateComponentTreeUI(this);
break;
}
case 96: {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(Notatnik.class.getName()).log(Level.SEVERE, null, ex);
}
SwingUtilities.updateComponentTreeUI(this);
break;
}
}
}
}