2011-08-29 27 views
7

मेरे पास चेकबॉक्स के साथ एक टेबल है। जब मैं तीसरे या चौथे कॉलम में चेकबॉक्स पर क्लिक करता हूं तो मैं पहले कॉलम में चेकबॉक्स का चयन बदलना चाहता हूं। मैं एक ही पंक्ति पर अन्य कोशिकाओं को बदलने में सक्षम होना चाहता हूँ। मेरे पास पहले से ही कॉलम हैं इसलिए मैं जानना चाहता हूं कि सेल किस पंक्ति में है। मैं भी अनिश्चित हूं कि मेरे पास अभी तक सही है या नहीं।जावाएफएक्स 2: टेबलसेल पंक्ति सूचकांक प्राप्त करें

मैं तो क्या किया जहाँ तक मुझे

से ज्यादातर लगा

enter image description here

यहाँ मेरी SSCCE है (लघु Sel एफ युक्त संकलित उदाहरण)

अगर नीचे दिए गए कोड में कुछ गड़बड़ है तो कृपया मुझे सही करें।

package javafxapplication5; 

import javafx.application.Application; 
import javafx.beans.property.StringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.CheckBox; 
import javafx.scene.control.TableCell; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 
import javafx.util.Callback; 

public class JavaFXApplication extends Application { 

    private static final ObservableList<ContactOptions> addContactOption = FXCollections.observableArrayList(
      new ContactOptions("Yes", "John Doe", "No", "Yes"), 
      new ContactOptions("Yes", "Jane Doe", "No", null), 
      new ContactOptions("Yes", "John Smith", "Yes", "Yes"), 
      new ContactOptions("Yes", "Patty Smith", "Yes", "No"), 
      new ContactOptions("Yes", "Jo Johnson", "Yes", "Yes"), 
      new ContactOptions("No", "Mary Johnson", "No", "No"), 
      new ContactOptions("Yes", "Clint Doe", "No", null), 
      new ContactOptions("Yes", "Sally Sue", "No", "Yes"), 
      new ContactOptions("Yes", "Bob Ryan", null, "Yes"), 
      new ContactOptions("No", "Mary Sue", "No", "No"), 
      new ContactOptions("Yes", "Bob Smith", "No", "Yes")); 
    private static TableView<ContactOptions> contactOptions = new TableView<ContactOptions>(); 

    public static void main(String[] args) { 
     Application.launch(JavaFXApplication.class, args); 
    } 

    @Override 
    public void start(Stage primaryStage) { 
     primaryStage.setTitle("Hello World"); 
     Group root = new Group(); 
     Scene scene = new Scene(root, 400, 200, Color.LIGHTGREEN); 

     Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() { 

      @Override 
      public TableCell call(final TableColumn param) { 
       final CheckBox checkBox = new CheckBox(); 
       final TableCell cell = new TableCell() { 

        @Override 
        public void updateItem(Object item, boolean empty) { 
         super.updateItem(item, empty); 
         if (item == null) { 
          checkBox.setDisable(true); 
          checkBox.setSelected(false); 
         } else { 
          checkBox.setDisable(false); 
          checkBox.setSelected(item.toString().equals("Yes") ? true : false); 
          commitEdit(checkBox.isSelected() ? "Yes" : "No"); 
         } 
        } 
       }; 
       cell.setNode(checkBox); 
       return cell; 
      } 
     }; 

     TableColumn firstCol = new TableColumn("Contact?"); 
     firstCol.setPrefWidth(60); 
     firstCol.setProperty("one"); 
     firstCol.setCellFactory(cellFactory); 

     TableColumn secondCol = new TableColumn("Name"); 
     secondCol.setPrefWidth(200); 
     secondCol.setSortAscending(true); 
     secondCol.setProperty("two"); 

     TableColumn thirdCol = new TableColumn("Call"); 
     thirdCol.setPrefWidth(60); 
     thirdCol.setProperty("three"); 
     thirdCol.setCellFactory(cellFactory); 

     TableColumn fourthCol = new TableColumn("Email"); 
     fourthCol.setPrefWidth(60); 
     fourthCol.setProperty("four"); 
     fourthCol.setCellFactory(cellFactory); 

     contactOptions.setItems(addContactOption); 
     contactOptions.getColumns().addAll(firstCol, secondCol, thirdCol, fourthCol); 
     contactOptions.setPrefSize(400, 200); 

     root.getChildren().add(contactOptions); 
     primaryStage.setScene(scene); 
     primaryStage.setVisible(true); 
    } 

    public static class ContactOptions { 

     private final StringProperty one; 
     private final StringProperty two; 
     private final StringProperty three; 
     private final StringProperty four; 

     ContactOptions(String col1, String col2, String col3, String col4) { 
      this.one = new StringProperty(col1); 
      this.two = new StringProperty(col2); 
      this.three = new StringProperty(col3); 
      this.four = new StringProperty(col4); 
     } 

     public String getOne() { 
      return one.get(); 
     } 

     public String getTwo() { 
      return two.get(); 
     } 

     public String getThree() { 
      return three.get(); 
     } 

     public String getFour() { 
      return four.get(); 
     } 
    } 
} 

उत्तर

6

लगभग वहाँ

commitEdit कॉल करने से पहले, यह आवश्यक getTableView().edit(getTableRow().getIndex(), param) कॉल करने के लिए है। यह सेल को "संपादन मोड" में डाल देता है। चूंकि startEdit विधि नहीं है, इसलिए संपादन मोड में प्रवेश करने में बहुत कम शामिल है, लेकिन यह अभी भी आवश्यक है।

उसके बाद, यहाँ वर्णित के रूप में: http://download.oracle.com/javafx/2.0/ui_controls/table-view.htm

यह कॉल करने के लिए

firstCol.setOnEditCommit(new EventHandler<EditEvent<String>>() { 
    @Override 
    public void handle(EditEvent<String> event) { 
     String newValue = event.getNewValue(); 
     ContactOptions data = (ContactOptions) event.getTableView().getItems().get(event.getTablePosition().getRow()); 
     data.one.set(newValue) 
     if(newValue.equals("No")) { 
      data.three.set("No"); 
      data.four.set("No"); 
     } 
    } 
} 

अब सब मैं पता करने की जरूरत how to update the table's display once the data is updated है आवश्यक है।

1

ऑब्जर्वेबल का उपयोग करने का एक लाभ यह है कि जावाएफएक्स यूआई तत्व आपके लिए "दृश्यों के पीछे" बाइंडिंग कर सकते हैं। दूसरे शब्दों में, यदि आप जावाफैक्स बीन के रूप में अपना डेटा मॉडल क्लास लागू करते हैं, तो आपका यूआई स्वचालित रूप से अपडेट होने पर स्वचालित रूप से अपडेट हो जाएगा। ऐसा इसलिए होता है क्योंकि आपके मॉडल में देखने योग्य डेटा के लिए बाइंडिंग स्वचालित रूप से स्वचालित रूप से जेनरेट की गई अधिसूचनाओं को स्वचालित रूप से असाइन और परिवर्तित कर देती हैं।

लेकिन ऐसा होने के लिए आपको जावाएफएक्स बीन प्रतिमान के अनुसार अपने डेटा मॉडल को परिभाषित करना होगा, अन्यथा आपका यूआई अपडेट होने के साथ अपडेट नहीं होगा।

आपका डाटा मॉडल इस तरह परिभाषित किया गया है:

public static class ContactOptions { 

    private final StringProperty one; 
    private final StringProperty two; 
    private final StringProperty three; 
    private final StringProperty four; 

    ContactOptions(String col1, String col2, String col3, String col4) { 
     this.one = new StringProperty(col1); 
     this.two = new StringProperty(col2); 
     this.three = new StringProperty(col3); 
     this.four = new StringProperty(col4); 
    } 

    public String getOne() { 
     return one.get(); 
    } 

    public String getTwo() { 
     return two.get(); 
    } 

    public String getThree() { 
     return three.get(); 
    } 

    public String getFour() { 
     return four.get(); 
    } 
} 

इस उत्तर के लिए, मैं अपने 1 उदाहरण क्षेत्र, एक पर ही ध्यान दिया जाएगा। उदाहरण के लिए इस को बदलने के लिए तो यह एक JavaFX संपत्ति के लिए JavaFX सेम प्रतिमान का अनुपालन करता है, अपने कोड इस तरह से लिखते हैं,:

public static class ContactOptions { 

    private final StringProperty one = new SimpleStringProperty(); 

    public final String getOne() { return this.one.get(); } 
    public final void setOne(String v) { this.one.set(v); } 
    public final StringProperty oneProperty() { return this.one; } 

यह संभव है एक JavaFX सेम है कि एक lazier के लिए उपलब्ध कराने के लिए संपत्ति परिभाषाओं लिखने के लिए प्रारंभिकरण, लेकिन यह काम करेगा। जावा बीन और जावाएफएक्स बीन के बीच का अंतर यह है कि आपको संपत्ति (उपरोक्त अंतिम पंक्ति) के लिए एक एक्सेसर भी प्रदान करना होगा।

यदि आप उपरोक्त के समान गुणों में अपने सभी फ़ील्ड बनाते हैं, तो आप पाएंगे कि आपके यूआई अपडेट परिवर्तनों को प्रतिबिंबित करने के लिए अपडेट करते हैं।