How to use method from other Class


How to use method from other Class



I have a simple question for prof and hard for me :-)



I have tableView in 3 differ classes and I have 2 methods which should be initialized every time when App is started.



Is is possible to create that 2 methods without Class declaration /make it static and use it like in initialization. Example below.


public class Subscriber {

private StringProperty number;
private StringProperty addr;

public Subscriber() {
this(null, null);
}
....
}



This is example of my class which use Subscriber


FirstClass(){

@FXML
private void initialize() {
tableView.getSelectionModel().setCellSelectionEnabled(true);
tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
numberCol.setCellValueFactory(
cellData -> cellData.getValue().numberProperty());
addrCol.setCellValueFactory(
cellData -> cellData.getValue().addrProperty());
}



And that method I use in each of this and other 2 classes to get possibility to copy defferent Cell in TableView


public void copySelectionToClipboard(final TableView<Subscriber> table) {
final Set<Integer> rows = new TreeSet<>();
final StringBuilder strb = new StringBuilder();
boolean firstRow = true;
boolean firstCol = true;

for (final TablePosition tablePosition : table.getSelectionModel().getSelectedCells()) {
rows.add(tablePosition.getRow());

}

for (final TableColumn<Subscriber, ?> column : table.getColumns()) {
if (!firstCol) {
strb.append('n');

}
firstCol = false;

for (final Integer row : rows) {
if (!firstRow) {
strb.append('t');
}
firstRow = false;

if (table.getSelectionModel().isSelected(row, column)) {
final Object cellData = column.getCellData(row);
strb.append(cellData == null ? "" : cellData.toString());
}
}
}
final ClipboardContent clipboardContent = new ClipboardContent();
clipboardContent.putString(strb.toString());
Clipboard.getSystemClipboard().setContent(clipboardContent);
}



Is it chance to use that method in Subscriber class ?



P.S: Of course I know, I can create static copySelectionToClipboard(){}
or can create Subscriber subscriber in each class, but I`m looking for some any decision.





"Is is possible to create that 2 methods without Class declaration" you cannot define methods outside of types. Technically you could define a method outside of a class by using a enum, but I'm pretty sure that's not what you're asking for. "Of course I know, I can create static copySelectionToClipboard(){} or can create Subscriber subscriber in each class, but I'm looking for some any decision." That is your decision to make, but since it seems like you're not using any variables of the containing class, you should be fine with the first approach,you could even useTableView<?>as param
– fabian
Jul 2 at 8:17



enum


TableView<?>









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy