Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Java Programming 24-Hour Trainer, 2nd Edition 17 tryit

[es] :: Java :: Java Programming 24-Hour Trainer, 2nd Edition 17 tryit

[ Pregleda: 1802 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

bane

Član broj: 449
Poruke: 224
*.teol.net.



Profil

icon Java Programming 24-Hour Trainer, 2nd Edition 17 tryit19.09.2017. u 08:07 - pre 20 meseci
Radi se o poglavlju concurrency and multithreading. U primeru je klasa NewsReader prošitrila SwingWorker <String, String>. Kako sam razumio ova dva String-a se odnose na metode doInBackground() i process(). Meni nije jasno u ovome slučaju zašto se poziva publish(currentLine); gde je currentLine tipa String. Na taj način se poziva metoda process(), a zatim stoji, protected void process(List<String> fragments). Zašto List<String>.

***************************************************************************************************
***************************************************************************************************
package tryit;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;

public class NewsReader extends SwingWorker<String, String> {

File newsFile = null;
long fileSize;

JTextArea textAreaToFill = null;
JProgressBar progressBar = null;

NewsReader(File file, JTextArea textArea, JProgressBar pbar){

progressBar = pbar;

if (file.exists()){
newsFile = file;
textAreaToFill = textArea;
} else {
JOptionPane.showMessageDialog(null, "File ".concat(file.getName()).concat(" not found"));
}
}

protected String doInBackground() throws Exception {
String fileContent = null;
try (FileReader fr = new FileReader(newsFile);
BufferedReader br = new BufferedReader(fr);){

fileSize = newsFile.length();

fileContent = new String();
String currentLine = new String();

while ((currentLine = br.readLine()) != null){
fileContent = fileContent.concat(currentLine + "\n");
int read = fileContent.length();
int percent = (int)(((double) read) / ((double) fileSize) * 100.);
setProgress(percent);
publish(currentLine);
}
setProgress(100);
publish("");
br.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}

return fileContent;
}

protected void process(List<String> fragments) {
for (String line : fragments) {
if (isCancelled()) {
break;
}
progressBar.setValue(getProgress());
}
}

protected void done(){
try {
textAreaToFill.setText(get());
} catch (Exception e) {
e.printStackTrace();
}
}

}

***************************************************************************************
***************************************************************************************

package tryit;

import java.io.*;
import java.awt.*;

import javax.swing.*;
import javax.swing.border.Border;

public class NewsReaderGUI {

private JTextArea textAreaLeft = new JTextArea(30,30);
private JTextArea textAreaRight = new JTextArea(30,30);

static JProgressBar leftProgressBar;
static JProgressBar rightProgressBar;
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridBagLayout());

private void displayGUI() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GridBagConstraints c = new GridBagConstraints();

textAreaLeft.setLineWrap(true);
textAreaLeft.setWrapStyleWord(true);

JScrollPane scrolltxtLeft = new JScrollPane();
scrolltxtLeft.setViewportView(textAreaLeft);

JScrollPane scrolltxtRight = new JScrollPane();
scrolltxtRight.setViewportView(textAreaRight);

JButton button = new JButton("Get the News");

UIManager.put("ProgressBar.selectionBackground", Color.black);
UIManager.put("ProgressBar.selectionForeground", Color.white);
UIManager.put("ProgressBar.foreground", new Color(8, 32, 128));

DefaultBoundedRangeModel model1 = new DefaultBoundedRangeModel(0,0,0,100);
DefaultBoundedRangeModel model2 = new DefaultBoundedRangeModel(0,0,0,100);

leftProgressBar = new JProgressBar(model1);
rightProgressBar = new JProgressBar(model2);

c.insets = new Insets(2,2,2,2);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
panel.add(button,c);

c.gridy = 1;
panel.add(scrolltxtLeft,c);

c.gridx = 1;
panel.add(scrolltxtRight,c);

addProgressBar(c, 0,2, leftProgressBar);
addProgressBar(c, 1,2, rightProgressBar);

frame.getContentPane().add(panel, BorderLayout.NORTH);

frame.pack();
frame.setVisible(true);

button.addActionListener( event ->{
new NewsReader(new File("data/left.txt"),
textAreaLeft, leftProgressBar).execute();
new NewsReader(new File("data/right.txt"),
textAreaRight, rightProgressBar).execute();
});
}

private void addProgressBar(GridBagConstraints c, int gridx, int gridy, JProgressBar progressBar) {

c.gridx = gridx;
c.gridy = gridy;

progressBar.setBorderPainted(true);
progressBar.setMinimum(0);
progressBar.setMaximum(100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.setIndeterminate(false);
Border border = BorderFactory.createTitledBorder("Reading File");
progressBar.setBorder(border);
panel.add(progressBar, c);
}

public static void main( String[] args) {

EventQueue.invokeLater(() ->
new NewsReaderGUI().displayGUI());
}
}


1000 zasto 1000 zato
 
Odgovor na temu

bane

Član broj: 449
Poruke: 224
*.teol.net.



Profil

icon Re: Java Programming 24-Hour Trainer, 2nd Edition 17 tryit19.09.2017. u 08:15 - pre 20 meseci
Zaboravo sam da pitam i ovo:

String fileContent = null; //OK

fileContent = new String(); //Zašto
String currentLine = new String(); //Šta je ovim postigao?
1000 zasto 1000 zato
 
Odgovor na temu

dusans
Stojanov Dušan
Pančevo

Član broj: 9551
Poruke: 1313
*.dynamic.sbb.rs.



+304 Profil

icon Re: Java Programming 24-Hour Trainer, 2nd Edition 17 tryit19.09.2017. u 08:21 - pre 20 meseci
Pročitaj dokumentaciju:
https://docs.oracle.com/javase...ing/SwingWorker.html#publish(V...)
publish je mogao biti pozvan više puta pre nego što se izvrši process,
tako da u process ne dobijaš jednu vrednost već listu vrednosti, za svaki poziv publish-a po jednu.
Dakle, publish dodaje vrednost na gomilu, a kada se pozove process on uzme celu gomilu.
 
Odgovor na temu

bane

Član broj: 449
Poruke: 224
*.teol.net.



Profil

icon Re: Java Programming 24-Hour Trainer, 2nd Edition 17 tryit19.09.2017. u 10:18 - pre 20 meseci
Hvala!
1000 zasto 1000 zato
 
Odgovor na temu

bane

Član broj: 449
Poruke: 224
31.223.139.*



Profil

icon Re: Java Programming 24-Hour Trainer, 2nd Edition 17 tryit19.09.2017. u 18:12 - pre 20 meseci
Imam još jedno pitanje:

Našao sam da isCancelled() proverava da li je zadatak urađen. U ovom konkretnom slučaju metoda se poziva u okviru metode process, gde se prelazi po String-ovima liste, i u svakoj iteraciji se poziva isCanecelled(). Nisam siguran što to u ovom konkretnom slučaju radi?
1000 zasto 1000 zato
 
Odgovor na temu

Rapaic Rajko
Bgd

Član broj: 4105
Poruke: 773
*.e-info.ba.



+60 Profil

icon Re: Java Programming 24-Hour Trainer, 2nd Edition 17 tryit20.09.2017. u 08:34 - pre 20 meseci
Zar ne kaze samo ime funkcije - IsCancelled() valjda proverava da li je proces prekinut (a ne uradjen)..?

Pozz
 
Odgovor na temu

bane

Član broj: 449
Poruke: 224
31.223.139.*



Profil

icon Re: Java Programming 24-Hour Trainer, 2nd Edition 17 tryit20.09.2017. u 19:27 - pre 20 meseci
OK, nisam se dobro izrazio, više sam se fokusirao na to da prolazi kroz stringove i poziva isCancelled(), nisam siguran šta se poštiže na ovaj način.
1000 zasto 1000 zato
 
Odgovor na temu

[es] :: Java :: Java Programming 24-Hour Trainer, 2nd Edition 17 tryit

[ Pregleda: 1802 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.