Rectangle 27 3

I ended up deciding to get the coordinates from Postgres using the ST_AsText() method, rather than extracting them from some sort of Geography object in Java. Then I just parsed the polygon string in Java.

SELECT id, name, ST_AsText(area) FROM area_table;

In Java I extract the String from the ResultSet after doing my JDBC query:

String area = rs.getString("ST_AsText");
"POLYGON((-49 52,123 52,123 -4,-49 -4,-49 52))"

Then I just parsed the points out of that.

double[] bounds = new double[8];
int k = 0;
for (int i = 0; i < points.length; i++) {
    String[] lonLat = points[i].split(" ");
    for (int j = 0; j < lonLat.length; j++) {
        bounds[k++] = Double.parseDouble(lonLat[j]);
    }
}

I don't know if this is the best way to do it, but it's the best way I could figure out on my own.

java - Reading points from a Geography Polygon in a PostGIS database -...

java postgresql jdbc postgis
Rectangle 27 2

Your question is a specific example of a basic problem in programming in Java -- how to transfer information between classes. There are several ways to do this, one of the most elegant being giving to use a "model" class that holds your program's logic code and key data, having one class change the model's state by changing a text String that it holds. Then using a listener or observer pattern, have the model notify the other class that it has been changed so the other class can extract the new information, its new String from the model. While this is likely the best solution, it may be a bit of overkill and likely is above your current level of coding, so for you, I'm not going to recommend this.

Instead, I'm going to recommend a simpler less elegant solution, that you instead have one class call a setter method of the other to push the new String into it.

One problem we have as volunteer answerers here is that your question is hard to answer because it lacks critical code, making it hard for us to guess why your code is misbehaving, why specifically you're running into a NullPointerException (or NPE) when you try to run it. So all I can do is guess, but guess I will try nevertheless.

For simplicity's sake, let's call one class the, the one that holds the JLabel, the LabelClass and the other class the ButtonTextFieldClass.

One possible reason is that you've got a NullPointerException is because your ButtonTextFieldClass may have a LabelClass variable, but never initialized the variable, something like so:

// this guy is null because it is never initialized
private LabelClass labelClass;
private LabelClass labelClass = new LabelClass();

But this won't work because while it does create and assign a LabelClass instance, it's likely not the LabelClass instance that is visualized in the running GUI.

A better solution is to give the ButtonTextFieldClass a setter method that allows other classes to set the ButtonTextFieldClass with the proper LabelClass instance.

public void setLabelClass(LabelClass labelClass) {
   this.labelClass = labelClass;
}

This way the code that sets up both classes can pass the visualized LabelClass to the first class, and it can call methods on it.

A simple example of LabelClass could look like so:

class LabelClass extends JPanel {
   private JLabel label = new JLabel("");

   public LabelClass() {
      setBorder(BorderFactory.createTitledBorder("Label Panel"));
      add(label);
   }

   public void setLabelText(String text) {
      label.setText(text);
   }
}

I have it extend JPanel because this way it gives me the freedom of placing it into a JFrame or JDialog or other JPanel as I see fit. Note that I've made the JLabel private and have given the class a public setter method, setLabelText(String text), that allows outside classes the ability to set the JLabel's text.

class ButtonTextFieldClass extends JPanel {
   private JTextField textField = new JTextField(10);
   private JButton button = new JButton(new ButtonAction("Send Text"));
   private LabelClass labelClass;

   public ButtonTextFieldClass() {
      setBorder(BorderFactory.createTitledBorder("Button TextField Panel"));
      add(textField);
      add(button);
   }

   // here we allow other classes to set instances of our LabelClass  
   public void setLabelClass(LabelClass labelClass) {
      this.labelClass = labelClass;
   }

   // ....

I've also given the button an AbstractAction in place of an ActionListener since it is like a super ActionListener on steroids. Inside of it, I'd get the text from the JTextField and then call the LabelClass's setter method (if the variable is not null) to set the label's text:

public void actionPerformed(ActionEvent e) {
     String text = textField.getText(); 
     if (labelClass != null) {
        labelClass.setLabelText(text);
     }
  }

Then to set everything up, in another class I'd create instances of both LabelClass and ButtonTextFieldClass, and then "hook them up" by calling the setter method:

LabelClass labelClass = new LabelClass();
  ButtonTextFieldClass buttonTextFieldClass = new ButtonTextFieldClass();
  buttonTextFieldClass.setLabelClass(labelClass); // set our reference
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class TransferData {

   private static void createAndShowGui() {
      LabelClass labelClass = new LabelClass();
      ButtonTextFieldClass buttonTextFieldClass = new ButtonTextFieldClass();
      buttonTextFieldClass.setLabelClass(labelClass); // set our reference

      JPanel mainPanel = new JPanel(new GridLayout(0, 1));
      mainPanel.add(buttonTextFieldClass);
      mainPanel.add(labelClass);

      JFrame frame = new JFrame("TransferData");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class LabelClass extends JPanel {
   private JLabel label = new JLabel("");

   public LabelClass() {
      setBorder(BorderFactory.createTitledBorder("Label Panel"));
      add(label);
   }

   public void setLabelText(String text) {
      label.setText(text);
   }
}

class ButtonTextFieldClass extends JPanel {
   private JTextField textField = new JTextField(10);
   private JButton button = new JButton(new ButtonAction("Send Text"));

   // one possible solution -- give this class a variable 
   // of the LabelClass -- but don't initialize the variable 
   // here, but rather do it in a setter
   private LabelClass labelClass;

   public ButtonTextFieldClass() {
      setBorder(BorderFactory.createTitledBorder("Button TextField Panel"));
      add(textField);
      add(button);
   }

   // here we allow other classes to set instances of our LabelClass  
   public void setLabelClass(LabelClass labelClass) {
      this.labelClass = labelClass;
   }

   // an AbstractAction is like a "super" ActionListener
   private class ButtonAction extends AbstractAction {
      public ButtonAction(String name) {
         super(name); // set the button's text and actionCommand
         int mnemonic = (int) name.charAt(0); // get first char
         putValue(MNEMONIC_KEY, mnemonic); // set mnemonic
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         String text = textField.getText(); 
         if (labelClass != null) {
            labelClass.setLabelText(text);
         }
      }
   }
}

For simplicity's sake, I've displayed both JPanels within the same GUI, but it could work just as well if one JPanel were in one JFrame and the other within a JDialog.

Thanks man! But the problem isnt that its not getting the information its just that it wont update, all i really need to do is somehow update my method with the click of the button in my other class. I have ALOT of code almost 1000 lines added up togethere, so it's hard so simplify :(

I've tried while loop to instantly keeping it updated but then my window wont even show up because it's updated so many times..

@Alexander: your first order of business is to debug your issue, to find out why your current code is throwing a NPE. Please read my link to your original question regarding that. And yeah, a while loop is a bad idea as it will stomp on the Swing event thread freezing your GUI. But you don't need to poll the data (the solution that you tried in your comment above). Instead you want to be notified of changes.

I added some code and edited my text, hope this will get you to understand the problem instead of guessing :D

java - How to change a JLabel with a button from another class? - Stac...

java swing nullpointerexception
Rectangle 27 0

Its nice you put a bounty on this. Unfortunately, as you probably already know, this can't be done using Java's string split method directly.

If it can't be done directly, there is no reason to kludge it as it is, well .. a kludge.

To start off, you need to define a good regex as a base. This is the only regex I know that will validate and extract a proper form:

# "((?=[+-]?\\d*\\.?\\d)[+-]?\\d*\\.?\\d*)"

 (                             # (1 start)
      (?= [+-]? \d* \.? \d )
      [+-]? \d* \.? \d* 
 )                             # (1 end)

So, looking at this base regex, its clear you want this form that it matches. In the case of split, you don't want the form that this matches, because that's where you want the breaks to be.

As I look at Java's split, I see that no matter what it matches, it will be excluded from the resulting array.

So, presuming split usage, the first thing to match (and consume) is all the stuff that is not this. That part will be something like this:

Since the only thing left is valid decimal numbers, the next break will be somewhere between valid numbers. This part, added to the first part, will be something like this:

And all of a sudden, we have a problem .. a variable length lookbehind assertion So, its game over for the whole thing.

Lastly and unfortunately, Java does not (as far as I can see) have a provision to include capture group contents (matched in the regex) as an element in the resulting array. Perl does, but I can't find that ability in Java.

If Java had that provision, the break sub expressions could be combined to do a seamless split. Like this:

(?:
      (?!
           (?= [+-]? \d* \.? \d )
           [+-]? \d* \.? \d* 
      )
      . 
 )*
 (
      (?= [+-]? \d* \.? \d )
      [+-]? \d* \.? \d* 
 )

I like the effort you spend into this, but the straightforward solution to the lack of provision is to break this into two steps, which is more effective in this case than your Qtax trick, anyway.

@Unihedro - The problem is it's impossible to break this into two steps. If it could, both steps would have to incorporate numeric validity (?=[+-]?\d*\.?\d)[+-]?\d*\.?\d*. In the end, doing the same thing twice is pointless. If you think there is a two step process, I'd be interested to debunk it.

Your regex with the variable length look-behind is totally bogus, since it will match the empty string in between a string of numbers 234235 (using .NET tester).

Another thing - in languages which supports splitting, the split regex at the end is not seamless - it will produce a bunch of empty strings in between.

Extracting numbers from a String in Java by splitting on a regex - Sta...

java regex
Rectangle 27 -1

For reading content of the table from pdf file,you have to do only just convert the pdf file into a text file by using any API(I have use PdfTextExtracter.getTextFromPage() of iText) and then read that txt file by your java program..now after reading it the major task is done.. you have to filter the data of your need. you can do it by continuously using split method of String class until you find record of your intrest.. here is my code by which I have extract part of record by an PDF file and write it into a .CSV file.. Url of PDF file is..http://www.cea.nic.in/reports/monthly/generation_rep/actual/jan13/opm_02.pdf

public static void genrateCsvMonth_Region(String pdfpath, String csvpath) {
        try {
            String line = null;
            // Appending Header in CSV file...
            BufferedWriter writer1 = new BufferedWriter(new FileWriter(csvpath,
                    true));
            writer1.close();
            // Checking whether file is empty or not..
            BufferedReader br = new BufferedReader(new FileReader(csvpath));

            if ((line = br.readLine()) == null) {
                BufferedWriter writer = new BufferedWriter(new FileWriter(
                        csvpath, true));
                writer.append("REGION,");
                writer.append("YEAR,");
                writer.append("MONTH,");
                writer.append("THERMAL,");
                writer.append("NUCLEAR,");
                writer.append("HYDRO,");
                writer.append("TOTAL\n");
                writer.close();
            }
            // Reading the pdf file..
            PdfReader reader = new PdfReader(pdfpath);
            BufferedWriter writer = new BufferedWriter(new FileWriter(csvpath,
                    true));

            // Extracting records from page into String..
            String page = PdfTextExtractor.getTextFromPage(reader, 1);
            // Extracting month and Year from String..
            String period1[] = page.split("PEROID");
            String period2[] = period1[0].split(":");
            String month[] = period2[1].split("-");
            String period3[] = month[1].split("ENERGY");
            String year[] = period3[0].split("VIS");

            // Extracting Northen region
            String northen[] = page.split("NORTHEN REGION");
            String nthermal1[] = northen[0].split("THERMAL");
            String nthermal2[] = nthermal1[1].split(" ");

            String nnuclear1[] = northen[0].split("NUCLEAR");
            String nnuclear2[] = nnuclear1[1].split(" ");

            String nhydro1[] = northen[0].split("HYDRO");
            String nhydro2[] = nhydro1[1].split(" ");

            String ntotal1[] = northen[0].split("TOTAL");
            String ntotal2[] = ntotal1[1].split(" ");

            // Appending filtered data into CSV file..
            writer.append("NORTHEN" + ",");
            writer.append(year[0] + ",");
            writer.append(month[0] + ",");
            writer.append(nthermal2[4] + ",");
            writer.append(nnuclear2[4] + ",");
            writer.append(nhydro2[4] + ",");
            writer.append(ntotal2[4] + "\n");

            // Extracting Western region
            String western[] = page.split("WESTERN");

            String wthermal1[] = western[1].split("THERMAL");
            String wthermal2[] = wthermal1[1].split(" ");

            String wnuclear1[] = western[1].split("NUCLEAR");
            String wnuclear2[] = wnuclear1[1].split(" ");

            String whydro1[] = western[1].split("HYDRO");
            String whydro2[] = whydro1[1].split(" ");

            String wtotal1[] = western[1].split("TOTAL");
            String wtotal2[] = wtotal1[1].split(" ");

            // Appending filtered data into CSV file..
            writer.append("WESTERN" + ",");
            writer.append(year[0] + ",");
            writer.append(month[0] + ",");
            writer.append(wthermal2[4] + ",");
            writer.append(wnuclear2[4] + ",");
            writer.append(whydro2[4] + ",");
            writer.append(wtotal2[4] + "\n");

            // Extracting Southern Region
            String southern[] = page.split("SOUTHERN");

            String sthermal1[] = southern[1].split("THERMAL");
            String sthermal2[] = sthermal1[1].split(" ");

            String snuclear1[] = southern[1].split("NUCLEAR");
            String snuclear2[] = snuclear1[1].split(" ");

            String shydro1[] = southern[1].split("HYDRO");
            String shydro2[] = shydro1[1].split(" ");

            String stotal1[] = southern[1].split("TOTAL");
            String stotal2[] = stotal1[1].split(" ");

            // Appending filtered data into CSV file..
            writer.append("SOUTHERN" + ",");
            writer.append(year[0] + ",");
            writer.append(month[0] + ",");
            writer.append(sthermal2[4] + ",");
            writer.append(snuclear2[4] + ",");
            writer.append(shydro2[4] + ",");
            writer.append(stotal2[4] + "\n");

            // Extracting eastern region
            String eastern[] = page.split("EASTERN");

            String ethermal1[] = eastern[1].split("THERMAL");
            String ethermal2[] = ethermal1[1].split(" ");

            String ehydro1[] = eastern[1].split("HYDRO");
            String ehydro2[] = ehydro1[1].split(" ");

            String etotal1[] = eastern[1].split("TOTAL");
            String etotal2[] = etotal1[1].split(" ");
            // Appending filtered data into CSV file..
            writer.append("EASTERN" + ",");
            writer.append(year[0] + ",");
            writer.append(month[0] + ",");
            writer.append(ethermal2[4] + ",");
            writer.append(" " + ",");
            writer.append(ehydro2[4] + ",");
            writer.append(etotal2[4] + "\n");

            // Extracting northernEastern region
            String neestern[] = page.split("NORTH");

            String nethermal1[] = neestern[2].split("THERMAL");
            String nethermal2[] = nethermal1[1].split(" ");

            String nehydro1[] = neestern[2].split("HYDRO");
            String nehydro2[] = nehydro1[1].split(" ");

            String netotal1[] = neestern[2].split("TOTAL");
            String netotal2[] = netotal1[1].split(" ");

            writer.append("NORTH EASTERN" + ",");
            writer.append(year[0] + ",");
            writer.append(month[0] + ",");
            writer.append(nethermal2[4] + ",");
            writer.append(" " + ",");
            writer.append(nehydro2[4] + ",");
            writer.append(netotal2[4] + "\n");
            writer.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    }

java - Parsing PDF files (especially with tables) with PDFBox - Stack ...

java parsing pdf pdfbox tabular