This discussion is archived
4 Replies Latest reply: Mar 25, 2013 9:51 AM by biochemistry43 RSS

relate tables

biochemistry43 Newbie
Currently Being Moderated
hi all

I have two tables.

TableView <Lines> lines
TableView <coordinates> lineCoordinates

One filled with the coordinates for a line and the second line of the general data (name, color, thickness, etc.)

Each line has several coordinates.

We thank God I managed to fill the tables. The ObservableList filled with objects (which is not yet used too). However, I want to select a row in the table "lines", are deployed its coordinates in the table "lineCoordinates".

I've tried everything and the most I've done is to do that every time I select a record in the table "lines", appears the first record of its coordinates.

Anyone can guide me a bit

In advance thank you all.
  • 1. Re: relate tables
    James_D Guru
    Currently Being Moderated
    If your model (data representation) is right, is should be easy.

    You have a Line class, presumably. If it has a method
    public class Line {
      ...
      public ObservableList<Coordinate> getCoordinates() { ... }
      ...
    }
    then all you need is
    lines.getSelectionModel().selectedItemProperty().addListener( new ChangeListener<Line>() {
      @Override
      public void changed(ObservableValue<Line> obs, Line oldValue, Line newValue) {
        if (newValue == null) {
          lineCoordinates.getItems().clear();
        } else {
          lineCoordinates.setItems(newValue.getCoordinates());
        }
      }
    }
  • 2. Re: relate tables
    biochemistry43 Newbie
    Currently Being Moderated
    Well I guess something went pro did wrong.

    I'll put a little more of my code:
    //With this method I read coordinates from a map and populated a table
    public void readLineCoordinatesFromMap(String datos)
        {
            Pattern p = Pattern.compile("[,]+");
            String[] result = p.split(data);
            
            int item = 1;
            String latitude = "";
            String longitude  = "";
            
            // dataLineCoord is a ObservableList linked with a TableView. dataLineaCoord its a global variable
            dataLineCoord.clear();
            for (int i = 1; i < (result.length - 1); i = i + 2 )
            {         
                latitude = result[ i ];
                longitude = result [ i + 1 ];
                LineCoordinates lineCoordinates = new LineCoordinates(item, latitude, longitude);
                dataLineCoord.add(lineCoordinates );
                item++;
            }
            
                lineCoordinatesTable.setItems(dataCoordLinea);
               // idLineField.setText(result[result.length -  1]);
                
        }
    
    //then to add data to the table line ...
    public void addLine(MouseEvent event)
        {
            int item;
            String lineName;
            String lineWeight;
            String lineColor;
            
            item = Integer.parseInt(idLineField.getText());
            lineName = lineNameField.getText();
             lineWeight =  lineWeightField.getValue().toString();
            if ( lineWeight == null)
            {
                 lineWeight = "0";
            }        
            Color color = selectLineColor.getValue();
            lineColor = color.toString();
            
            //I tried to do this ...
            //Retrieve the value of the global variable ObservableList that containing the coordinates values 
            ObservableList<CoordenadasLinea> coordinatesThisLine = dataLineCoord;
    
           //I create an Lines object and pass the ObservableList object as a parameter
            Lines lines = new Lines(item, lineName, lineWeight, lineColor, coordinatesThisLine);
            
            //lineDates is a global ObservableList linked with a TableView
            lineDates.add(lines );
            linesTable.setItems(datosLinea);
    
            dataLineCoord.clear();
        }
    
    
    //this is my class "Lines"
    
    public class Lines
        {
            private final SimpleIntegerProperty itemLine = new SimpleIntegerProperty();
            private final SimpleStringProperty lineName = new SimpleStringProperty("");
            private final SimpleStringProperty lineWeight = new SimpleStringProperty("");
            private final SimpleStringProperty lineColor = new SimpleStringProperty("");
            private ObservableList<LineCoordinates> lineCoordinatesList;
            
            public Lines()
            {
                this(0, "", "", "",null);
            }
            
            public Lines(int item, String lineName, String lineWeight, String lineColor, ObservableList<LineCoordinates> coordinates)
            {
                setItem(item);
                setLineName(lineName);
                setlineWeight(lineWeight);
                setLineColor(lineColor);
                setlineCoordinatesList(coordinates);
            }
    
           .
           .
           .
           //Your suggestion
          public void setlineCoordinatesList(ObservableList<LineCoordinates> listCoord)
            {
                lineCoordinatesList= listCoord;
            }
            public ObservableList<LineCoordinates> getLineCoordinatesList()
            {
                return lineCoordinatesList;
            }
    }
    
    
    //Now ... I have put the listener in initialize
    
     @Override
        public void initialize(URL url, ResourceBundle rb) 
        {
            linesTable.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);  
    
           //   Your suggestion...
            linesTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Lines>() {
            @Override
                public void changed(ObservableValue<? extends Lines> ov, Lines oldValue, Lines newValue) {
                     if (newValue == null) {
                         tablaCoordenadasDeLinea.getItems().clear();
                     }
                     else
                     {
                          lineCoordinatesTable.setItems(newValue.getLineCoordinatesList());
                     }
                }
    
            }
            );
       }
    .
    .
    .
    Basically the idea is that the user draw a line on a map.

    Then the user will terminate the line and coordinate data populate a table (in that you helped me remember).

    The user must fill other line data and finally, when you are satisfied with all the data, press a button "AddLine". Then the basic data of the line will be added to another table to keep lines.

    Then each time you select a row in the table lines must deploy its coordinates in another table.


    This it did not work try here

    ... This time I put the code in English to make it more simple for others.

    thanks for all.

    Edited by: biochemistry43 on 25/03/2013 09:53 AM
  • 3. Re: relate tables
    James_D Guru
    Currently Being Moderated
    The line
    ObservableList<CoordenadasLinea> coordinatesThisLine = dataLineCoord;
    copies the reference to the list, so you just have one list, with two references to it. Then you do
    dataLineCoord.clear();
    which clears everything from your list.

    Instead of copying the reference, do
    ObservableList<CoordenadasLinea> coordinatesThisLine = FXCollections.observableArrayList(); // new list...
    coordinatesThisLine.addAll(dataLineCoord);
  • 4. Re: relate tables
    biochemistry43 Newbie
    Currently Being Moderated
    This was the problem ... thank you very much.

    There are some who know OOP concepts (such as copying object references) but inexperience sometimes I forget.

    Thank you very much and blessings

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points