I have the following problem: I created a code to separate each variable of Euclid's algorithm and store in a Array or object . I can not think of a way to retrieve the data and populate a tableView so that , depending on the MDC (a, b ) if fore very large, the columns are added automatically.
In fact , it is a representation of the resolution of the MDC by successive divisions. I did a test , but I can not run it. Follows the code .
MDC
public List<Inteiros_old> listMdc(Inteiros_old inteiros) {
//List<Integer> lista_resto = new ArrayList<Integer>();
System.out.println(" Dividendo" + "\t" + " Divisor " + "\t" + " Quociente" + "\t" + " Resto ");
System.out.println("----------" + "\t" + "----------" + "\t" + "----------" + "\t" + "----------");
int max_inteiros = Math.max(inteiros.getDividendo(), inteiros.getDivisor());
int min_inteiros = Math.min(inteiros.getDividendo(), inteiros.getDivisor());
//System.out.println(k + "\t" + m);
List<Inteiros_old> lista = new ArrayList<Inteiros_old>();
while( min_inteiros != 0) {
inteiros.setResto(max_inteiros % min_inteiros);
inteiros.setQuociente(max_inteiros/min_inteiros);
System.out.println(max_inteiros + "\t\t " + min_inteiros + "\t\t " + inteiros.getQuociente() + "\t\t" + inteiros.getResto());
max_inteiros = min_inteiros;
min_inteiros = inteiros.getResto();
//lista.add(inteiros.getResto());
}
return lista;
}
JAVAFX
public class TabelaDinamica extends Application {
private TableView<Inteiros> table = new TableView<Inteiros>();
private final ObservableList<Inteiros> data = FXCollections.observableArrayList();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
// -------------------------------------------------------------------------
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(300);
stage.setHeight(500);
final Label label = new Label("Dynamic Table with autoColumns");
label.setFont(new Font("Arial", 16));
table.setEditable(true);
// -------------------------------------------------------------------------
int sizeColumns = 4;
for (int j = 0; j < sizeColumns; j++) {
data.add(new Inteiros(1,2,3,4));
String nome = new String("col"+j);
TableColumn col = new TableColumn();
col.setMinWidth(100);
col.setCellValueFactory(
new PropertyValueFactory<Inteiros, String>(nome));
//table.getColumns().add(j, col);
table.setItems(data);
table.getColumns().addAll(col);
}
// -------------------------------------------------------------------------
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
// -------------------------------------------------------------------------
stage.setScene(scene);
stage.show();
}
}
public class Inteiros {
private SimpleIntegerProperty dividendo; //dividen
private SimpleIntegerProperty divisor; //divisor
private SimpleIntegerProperty quociente; //quotient
private SimpleIntegerProperty resto; //rest
Inteiros(Integer dividendo, Integer divisor, Integer quociente, Integer resto) {
this.dividendo = new SimpleIntegerProperty(dividendo);
this.divisor = new SimpleIntegerProperty(divisor);
this.quociente = new SimpleIntegerProperty(quociente);
this.resto = new SimpleIntegerProperty(resto);
}
public Integer getDividendo() {
return dividendo.get();
}
public void setDividendo(Integer int_num) {
dividendo.set(int_num);
}
public Integer getDivisor() {
return divisor.get();
}
public void setDivisor(Integer int_num) {
divisor.set(int_num);
}
public Integer getQuociente() {
return quociente.get();
}
public void setQuociente(Integer int_num) {
quociente.set(int_num);
}
public Integer resto() {
return resto.get();
}
public void setResto(Integer int_num) {
resto.set(int_num);
}
try this
String[] nome = {"dividendo", "divisor", "quociente", "resto"}
int sizeColumns = 4;
for (int j = 0; j < sizeColumns; j++) {
data.add(new Inteiros(1,2,3,4));
TableColumn col = new TableColumn();
col.setMinWidth(100);
col.setCellValueFactory(new PropertyValueFactory<Inteiros, String>(nome[j]));
table.getColumns().addAll(col);
}
table.setItems(data);
because the value of String nome has to match with the name of the variable
Related
I am trying to populate the tableView with the data that is in the observableList but it will not show up when i run the program.
Here are parts of my program:
private TableView<Crypto> tableView = new TableView<Crypto>();
private static ArrayList<Crypto> cryptoData = new ArrayList();
private static ObservableList<Crypto> data = FXCollections.observableArrayList(cryptoData);
//*******Crypto Class************
static class Crypto{
private SimpleStringProperty coinName,
coinsBought,
costPerCoin,
totalSpent,
currentPrice,
currentValue,
profit,
roi;
public String getcoinName() {
return coinName.get();
}
public String getCoinsBought() {
return coinsBought.get();
}
public String getCostPerCoin() {
return costPerCoin.get();
}
public String getTotalSpent() {
return totalSpent.get();
}
public String getCurrentPrice() {
return currentPrice.get();
}
public String getCurrentValue() {
return currentValue.get();
}
public String getProfit() {
return profit.get();
}
public String getRoi() {
return roi.get();
}
Crypto(String name, String numBought, String costPerCoin, String totalSpent, String curPrice, String curValue, String profit, String roi){
this.coinName = new SimpleStringProperty(name);
this.coinsBought = new SimpleStringProperty(numBought);
this.costPerCoin = new SimpleStringProperty(costPerCoin);
this.totalSpent = new SimpleStringProperty(totalSpent);
this.currentPrice = new SimpleStringProperty(curPrice);
this.currentValue = new SimpleStringProperty(curValue);
this.profit = new SimpleStringProperty(profit);
this.roi = new SimpleStringProperty(roi);
}
#Override
public String toString() {
return ("[" + coinName.get() + ", " + coinsBought.get() + ", " + costPerCoin.get() + ", " +
totalSpent.get() + ", " + currentPrice.get() + ", " + currentValue.get() + ", " +
profit.get() + ", " + roi.get() + "]");
}
}//*********END Crypto Class*************
#Override
public void start(Stage primaryStage) {
try {
GridPane root = new GridPane();
//title text
Text titleText = new Text();
titleText.setText("Crypto Portfolio");
titleText.setY(600);
titleText.setFont(Font.font("Veranda", FontWeight.BOLD, FontPosture.REGULAR,40));
//refresh button
Button refresh = new Button("Refresh Prices");
refresh.setOnAction(e -> {
//ADD button refresh
});
//total amount text
Text totalDollar = new Text();
totalDollar.setText(getTotalDollar());
//table columns
TableColumn coinColumn = new TableColumn("Coin");
coinColumn.setCellValueFactory(new PropertyValueFactory<>("coinName"));
TableColumn costColumn = new TableColumn("Cost");
costColumn.setCellValueFactory(new PropertyValueFactory<>("totalSpent"));
TableColumn coinBoughtColumn = new TableColumn("Coins Bought");
coinBoughtColumn.setCellValueFactory(new PropertyValueFactory<>("coinsBought"));
TableColumn costPerCoinColumn = new TableColumn("Cost per Coin");
costPerCoinColumn.setCellValueFactory(new PropertyValueFactory<>("costPerCoin"));
TableColumn currentPriceColumn = new TableColumn("Current Coin Price");
currentPriceColumn.setCellValueFactory(new PropertyValueFactory<>("currentPrice"));
TableColumn currentValueColumn = new TableColumn("Curren Value");
currentValueColumn.setCellValueFactory(new PropertyValueFactory<>("currentValue"));
TableColumn profitColumn = new TableColumn("Profit");
profitColumn.setCellValueFactory(new PropertyValueFactory<>("profit"));
TableColumn roiColumn = new TableColumn("ROI");
roiColumn.setCellValueFactory(new PropertyValueFactory<>("roi"));
tableView.setItems(data);
tableView.getColumns().addAll(coinColumn, costColumn, coinBoughtColumn, costPerCoinColumn, currentPriceColumn, currentValueColumn, profitColumn, roiColumn);
Scene scene = new Scene(root,1200,900);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
root.setHgap(10);
root.setVgap(10);
//sets gridLines visible for debug
root.setGridLinesVisible(true);
primaryStage.setScene(scene);
primaryStage.setTitle("Mike's Crypto Portfolio");
root.add(titleText, 0,0);
root.add(refresh, 3, 0);
root.add(tableView, 0, 1);
primaryStage.show();
new Thread () {
#Override
public void run() {
try {
readCSV("crypto.csv");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
} catch(Exception e) {
e.printStackTrace();
}
}//*****************END Start***************************
}
I thought if I added this it would work but I get a WARNING: Can not retrieve property 'coinName' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory#2cc2c23 with provided class type: class application.Main$Crypto java.lang.IllegalStateException: Cannot read from unreadable property coinName
but for everything besides coinName the error also adds :java.lang.RuntimeException: java.lang.IllegalAccessException: class com.sun.javafx.reflect.Trampoline cannot access a member of class application.Main$Crypto with modifiers "public"
for(Crypto coin : cryptoData) {
data.add(coin);
}
Treeitem is overwriting every time I add a new class. How to solve this?
While adding a new object treeitem should be dynamically increase tried:
adding without using the list
added using the list
tried to add using a loop
This is an example code sorry for naming errors.thanks in advance
marked the place of issue with --------
PanesClass.java
public class PanesClass extends Application {
ObservableList<Connections> cList = FXCollections.observableArrayList();
public static void main(String[] args) {
launch(args);
}
#SuppressWarnings("all")#Override
public void start(Stage primaryStage) throws Exception {
NewConnection newConnection = new NewConnection();
SplitPane root = new SplitPane();
AnchorPane first = new AnchorPane();
AnchorPane second = new AnchorPane();
TreeTableView activeConnections = new TreeTableView();
HBox buttonBox = new HBox();
BorderPane topBar = new BorderPane();
Button nConnection = new Button("+");
Button deleteConnection = new Button("X");
Button connect = new Button("Connect");
buttonBox.setSpacing(10);
buttonBox.getChildren().addAll(nConnection, deleteConnection, connect);
topBar.setTop(buttonBox);
TreeTableColumn<String, Connections > cNameColoumn = new TreeTableColumn<>("Name");
cNameColoumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("cname"));
TreeTableColumn<String, Connections> cStatusColoumn = new TreeTableColumn<>("Status");
cStatusColoumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("cstatus"));
activeConnections.getColumns().addAll(cNameColoumn, cStatusColoumn);
activeConnections.setLayoutX(20);
activeConnections.setLayoutY(40);
activeConnections.setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);
first.getChildren().addAll(topBar, activeConnections);
root.getItems().addAll(first, second);
Scene sc = new Scene(root, 600, 480);
primaryStage.setScene(sc);
primaryStage.show();
nConnection.setOnAction(new EventHandler<ActionEvent>() {#Override
public void handle(ActionEvent event) {
newConnection.getConnection(activeConnections);
}
});
}
}
NewConnection.java
public class NewConnection {
Connections connection = null;
ObservableList<Connections> cList = FXCollections.observableArrayList();
PanesClass panesClass = new PanesClass();
TreeItem cItem = null;
TreeItem nItem = null;
public void getConnection(TreeTableView<Connections> activeConnections) {
Stage secondaryStage = new Stage();
VBox root = new VBox();
GridPane cDetails = new GridPane();
HBox actionButtons = new HBox();
Button connect = new Button("Connect");
Button save = new Button("Save");
Button cancel = new Button("Cancel");
actionButtons.getChildren().addAll(connect, save, cancel);
actionButtons.setSpacing(10);
Label name = new Label("Username : ");
cDetails.add(name, 0, 0);
TextField uName = new TextField();
cDetails.setHgrow(uName, Priority.ALWAYS);
cDetails.add(uName, 1, 0);
Label password = new Label("Password : ");
cDetails.add(password, 0, 1);
TextField pwd = new TextField();
cDetails.add(pwd, 1, 1);
Label urllink = new Label("URL : ");
cDetails.add(urllink, 0, 2);
TextField url = new TextField();
cDetails.add(url, 1, 2);
cDetails.setVgap(10);
cDetails.setStyle("-fx-padding: 10;" + "-fx-border-style: solid inside;" + "-fx-border-width: 1;" + "-fx-border-insets: 5;" + "-fx-border-radius: 5;" + "-fx-border-color: black;");
root.getChildren().addAll(cDetails, actionButtons);
Scene sc = new Scene(root, 500, 200);
secondaryStage.setScene(sc);
secondaryStage.initModality(Modality.APPLICATION_MODAL);
secondaryStage.show();
save.setOnAction(new EventHandler<ActionEvent>() {
//*-----------------------------------------------------------------------*
#Override
public void handle(ActionEvent event) {
cItem = getitem(cItem);
activeConnections.setRoot(cItem);
activeConnections.setShowRoot(false);
secondaryStage.close();
}
private TreeItem getitem(TreeItem cItem) {
cList.add(new Connections(uName.getText()));
System.out.println(cList);
for (Connections temp: cList) {
System.out.println(temp);
nItem = new TreeItem<Connections>(temp);
System.out.println(nItem);
cItem.getChildren().add(nItem);
}
return cItem;
}
});
System.out.println(cList);
}
}
Connections.java
public class Connections {
private String cname = null;
private String cstatus = null;
private String cpwd = null;
private String curl = null;
public Connections() {
}
public Connections(String cname, String cpwd, String curl) {
super();
this.cname = cname;
this.cpwd = cpwd;
this.curl = curl;
}
public Connections(String cname, String cstatus) {
super();
this.cname = cname;
this.cstatus = cstatus;
}
public String getCpwd() {
return cpwd;
}
public void setCpwd(String cpwd) {
this.cpwd = cpwd;
}
public String getCurl() {
return curl;
}
public void setCurl(String curl) {
this.curl = curl;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCstatus() {
return cstatus;
}
public void setCstatus(String cstatus) {
this.cstatus = cstatus;
}
#Override
public String toString() {
return "Connections [cname=" + cname + ", cstatus=" + cstatus + ", cpwd=" + cpwd + ", curl=" + curl + "]";
}
}
You're not populating your tree, you're creating new items without adding them to your tree.
First thing, you need to create a root:
// Instead of this line
// TreeItem nItem = null;
TreeItem rootItem = new TreeItem();
Then:
activeConnections.setRoot(rootItem);
save.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
// clear old connections
rootItem.getChildren().clear();
// Add new connection
cList.add(new Connections(uName.getText(), pwd.getText(), url.getText()));
// create new items and add them to rootItem
for (Connections temp : cList) {
rootItem.getChildren().add(new TreeItem<Connections>(temp));
}
secondaryStage.close();
event.consume();
}
});
NOTE: If you don't have another reason to keep cList, you can remove it and add your new Items directly (no need to clear and regenerate items everytime):
save.setOnAction(event -> {
Connections newConnection = new Connections(uName.getText(), pwd.getText(), url.getText());
rootItem.getChildren().add(new TreeItem<>(newConnection));
secondaryStage.close();
event.consume();
});
I'm trying to add data in DefaultTableModel like the below code:
public class ConfigureCustomer extends JPanel {
private static final int CHECK_COL = 1;
private Object[][] DATA;
private final String[] COLUMNS = { "Customer", "Selected Customer" };
private DataModel dataModel;
private JTable table;
private DefaultListSelectionModel selectionModel;
public ConfigureCustomer() {
super(new BorderLayout());
ArrayList<String> items = new ArrayList<String>();
JSONParser parser = new JSONParser();
try {
Object obj = null;
try {
obj = parser.parse(new FileReader(new File("json/customer_list.json")));
} catch (org.json.simple.parser.ParseException e1) {
e1.printStackTrace();
}
JSONObject jsonObject = (JSONObject) obj;
JSONArray listOfBranches = (JSONArray) jsonObject.get("customers");
for (int i = 0; i < listOfBranches.size(); i++) {
JSONObject item = (JSONObject)listOfBranches.get(i);
Set keys = item.keySet();
Iterator a = keys.iterator();
while(a.hasNext()) {
String key = (String)a.next();
System.out.print("key : "+key);
items.add(key);
}
System.out.println("Customer :" + listOfBranches.get(i)+"\n item "+item);
}
System.out.println("items array size :" + items.size());
String[] customerArr = new String[items.size()];
customerArr = items.toArray(customerArr);
System.out.println("customerArr array size :" + customerArr.length);
DATA = new Object[customerArr.length][customerArr.length];
for (int i = 0; i < customerArr.length; i++) {
System.out.println("customerArr array size :" + customerArr.length);
DATA[i][i] = new Object[]{customerArr[i], Boolean.FALSE};
dataModel = new DataModel(DATA, COLUMNS);
table = new JTable(dataModel);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
add(new JScrollPane(table));
add(new ControlPanel(), BorderLayout.SOUTH);
table.setPreferredScrollableViewportSize(new Dimension(250, 175));
selectionModel = (DefaultListSelectionModel) table.getSelectionModel();
// add(new CheckABunch());
tableUIChanges();
}
private void tableUIChanges() {
table.getTableHeader().setFont(new Font("SansSerif", Font.BOLD, 18));
// Table header alignment, backgorund, text color
for (int i = 0; i < COLUMNS.length; i++) {
TableCellRenderer renderer = new DefaultTableCellRenderer();
((DefaultTableCellRenderer) renderer).setHorizontalAlignment(SwingConstants.CENTER);
table.getColumnModel().getColumn(i).setHeaderRenderer(renderer);
((DefaultTableCellRenderer) renderer).setBackground(new Color(219, 6, 6));
((DefaultTableCellRenderer) renderer).setForeground(Color.WHITE);
}
DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
table.getColumn(COLUMNS[0]).setCellRenderer( rightRenderer );
Font font = new Font("SansSerif", Font.PLAIN, 18);
table.setFont(font);
table.setRowHeight(30);
}
private class DataModel extends DefaultTableModel {
public DataModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}
#Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == CHECK_COL) {
return getValueAt(0, CHECK_COL).getClass();
}
return super.getColumnClass(columnIndex);
}
#Override
public boolean isCellEditable(int row, int column) {
return column == CHECK_COL;
}
}
private class ControlPanel extends JPanel {
public ControlPanel() {
this.add(new JLabel("Selection:"));
this.add(new JButton(new SelectionAction("Clear", false)));
this.add(new JButton(new SelectionAction("Finish", true)));
this.add(new JButton(new SelectionAction("Back", true)));
}
}
private class SelectionAction extends AbstractAction {
boolean value;
String name;
public SelectionAction(String name, boolean value) {
super(name);
this.name = name;
this.value = value;
}
#Override
public void actionPerformed(ActionEvent e) {
if (name.equalsIgnoreCase("Finish")) {
boolean isSelected = false;
for (int i = 0; i < dataModel.getRowCount(); i++) {
if (selectionModel.isSelectedIndex(i)) {
System.out.println("selected index " + " " + i + " " + name);
isSelected = true;
break;
}
}
if (isSelected) {
JPanel parent = (JPanel) getParent();
CardLayout cardLayout = (CardLayout) parent.getLayout();
cardLayout.next(parent);
} else {
JOptionPane.showMessageDialog(null, "Select customer to proceed", "InfoBox: " + "warning",
JOptionPane.INFORMATION_MESSAGE);
}
} else if (name.equalsIgnoreCase("Back")) {
JPanel parent = (JPanel) getParent();
CardLayout cardLayout = (CardLayout) parent.getLayout();
cardLayout.previous(parent);
} else {
for (int i = 0; i < dataModel.getRowCount(); i++) {
if (selectionModel.isSelectedIndex(i)) {
System.out.println("selected index " + " " + i + " " + name);
}
// if (selectionModel.isSelectedIndex(i)) {
System.out.println("actionPerformed value : " + value + " " + i + " " + CHECK_COL + " " + name);
dataModel.setValueAt(value, i, CHECK_COL);
// }
}
}
}
}
}
But it throws NullPointerException.
Before I had static Object[][] items in this same code.
It has worked for me, but after adding dynamic items it throws NullPointerException.
Could you please suggest to me an idea on how to fix this?
I have a table with on my SQL server with 16 rows, I am trying to print out the BasePT column into a bunch of TextFields but I cannot figure out how. Am I suppose to create a separate string for each row? How can I minimize code and be able to get each row to show up on each TextField?
//Table 100
// Button
public void loadButton(){
connection = SqlConnection.FormulaConnection();
try {
String SQL = "Select * FROM '100';";
ResultSet rs = connection.createStatement().executeQuery(SQL);
while (rs.next()) {
//insert BasePT from Row Yellow into YellowText TextField
String Yellow = rs.getString("BasePt");
YellowText.setText(Yellow);
//insert BasePT from Row 012 Yellow into TwoYellowText TextField
String TwoYellow = rs.getString("BasePT");
TwoYellowText.setText(TwoYellow);
}
} catch (Exception e) {
e.printStackTrace();
}
}
If you do NOT want to use a Tableview as yelliver suggested, you can try this:
public VBox loadButton(){
connection = SqlConnection.FormulaConnection();
VBox vBox = new VBox();
try {
String SQL = "Select * FROM '100';";
ResultSet rs = connection.createStatement().executeQuery(SQL);
while (rs.next()) {
String yellow = rs.getString("BasePt");
TextField textField = new TextField(yellow);
vBox.getChildren().add(textField);
}
} catch (Exception e) {
e.printStackTrace();
}
return vBox;
}
You can then add the VBox where you want to have all the textFields.
In the following examples I use a array of String[] arrays with elements for the row data. It should be easy enough to use the data from the database instead.
The standard way: TableView
Simply use a Item class containing a property for each table column:
public class Item {
public Item(String baseFormula, String basePt) {
this.baseFormula = new SimpleStringProperty(baseFormula);
this.basePt = new SimpleStringProperty(basePt);
}
private final StringProperty basePt;
private final StringProperty baseFormula;
public final String getBaseFormula() {
return this.baseFormula.get();
}
public final void setBaseFormula(String value) {
this.baseFormula.set(value);
}
public final StringProperty baseFormulaProperty() {
return this.baseFormula;
}
public final String getBasePt() {
return this.basePt.get();
}
public final void setBasePt(String value) {
this.basePt.set(value);
}
public final StringProperty basePtProperty() {
return this.basePt;
}
#Override
public String toString() {
return "Item{" + "basePt=" + basePt.get() + ", baseFormula=" + baseFormula.get() + '}';
}
}
And use a TableView with a column for each database column. Use a cellFactory that displays TextFields and modifies the property belonging to the column on a change of the TextField's text property:
#Override
public void start(Stage primaryStage) {
TableView<Item> table = new TableView<>();
Callback<TableColumn<Item, String>, TableCell<Item, String>> factory = column -> new TableCell<Item, String>() {
private final TextField textField;
{
textField = new TextField();
textField.textProperty().addListener((observable, oldValue, newValue) -> {
// write to property
WritableValue<String> property = (WritableValue<String>) getTableColumn().getCellObservableValue(getIndex());
property.setValue(newValue);
});
}
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(textField);
if (!Objects.equals(textField.getText(), item)) {
// only modify if TextField wasn't source of the change
// to prevent carret movement
textField.setText(item);
}
}
}
};
TableColumn<Item, String> formulaColumn = new TableColumn<>("baseFormula");
formulaColumn.setCellValueFactory(cd -> cd.getValue().baseFormulaProperty());
formulaColumn.setCellFactory(factory);
TableColumn<Item, String> ptColumn = new TableColumn<>("basePt");
ptColumn.setCellValueFactory(cd -> cd.getValue().basePtProperty());
ptColumn.setCellFactory(factory);
table.getColumns().addAll(formulaColumn, ptColumn);
String[][] data = {
{"Hello", "World"},
{"Hello2", "World2"},
{"Hello3", "World3"},
{"Hello4", "World4"},
{"Hello5", "World5"},
{"Hello6", "World6"}
};
for (String[] d : data) {
table.getItems().add(new Item(d[0], d[1]));
}
Button btn = new Button("print");
btn.setOnAction(evt -> System.out.println(table.getItems()));
Scene scene = new Scene(new VBox(table, btn));
primaryStage.setScene(scene);
primaryStage.show();
}
Alternative
If you don't want to use a TableView, GridPane would be a suitable Pane to produce a layout like this:
#Override
public void start(Stage primaryStage) {
String[][] data = {
{"Hello", "World"},
{"Hello2", "World2"},
{"Hello3", "World3"},
{"Hello4", "World4"},
{"Hello5", "World5"},
{"Hello6", "World6"}
};
Insets margin = new Insets(4);
int nextRow = 1;
GridPane gridPane = new GridPane();
Text heading1 = new Text("BaseFormula");
Text heading2 = new Text("BasePT");
GridPane.setMargin(heading1, margin);
GridPane.setMargin(heading2, margin);
gridPane.addRow(0, heading1, heading2);
for (String[] d : data) {
TextField tf = new TextField(d[0]);
TextField tf2 = new TextField(d[1]);
GridPane.setMargin(tf, margin);
GridPane.setMargin(tf2, margin);
gridPane.addRow(nextRow++, tf, tf2);
}
// add lines
// subtract stroke width
DoubleBinding height = gridPane.heightProperty().subtract(1);
// margin = 1/2 stroke width
Insets vMargin = new Insets(0.5, 0, 0.5, 0);
// add vertical lines
for (int i = 0; i < 3; i++) {
Line vLine = new Line();
GridPane.setMargin(vLine, vMargin);
System.out.println(vLine.getStrokeWidth());
vLine.endYProperty().bind(height);
gridPane.add(vLine, i, 0, 1, nextRow);
}
// procede accordingly with horizontal lines
DoubleBinding width = gridPane.widthProperty().subtract(1);
Insets hMargin = new Insets(0, 0.5, 0, 0.5);
for (int i = 0; i <= nextRow; i++) {
Line hLine = new Line();
GridPane.setMargin(hLine, hMargin);
hLine.setStartX(1);
hLine.endXProperty().bind(width);
// Insert at the top of the cell
GridPane.setValignment(hLine, VPos.TOP);
gridPane.add(hLine, 0, i, 2, 1);
}
Scene scene = new Scene(new StackPane(new Group(gridPane)), 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
you can replace this snippet with your ResultSet rs
ResultSet rs = null;//reference it to your rs
TableView<String[]> tv = new TableView<String[]>();
final int columnCount = rs.getMetaData().getColumnCount();
for(int i =1; i <= columnCount; i++){
TableColumn<String[], String> tc = new TableColumn<String[], String>();
tc.setText(rs.getMetaData().getColumnName(i));
final int k = i-1;
tc.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<
String[],String>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(CellDataFeatures<String[],
String> param) {
return new SimpleStringProperty(param.getValue()[k]);
}
});
tc.setCellFactory(new Callback<TableColumn<String[],String>,
TableCell<String[],String>>() {
#Override
public TableCell<String[], String> call(TableColumn<String[],
String> param) {
return new TableCell<String[], String>(){
#Override
protected void updateItem(String arg0, boolean arg1) {
super.updateItem(arg0, arg1);
if(arg1){
setText("");
return;
}else{
setText(arg0);
}
}
};
}
});
}
while(rs.next()){
String[] s = new String[columnCount];
for(int i = 1; i <= columnCount; i++){
s[i -1] = rs.getString(i);
}
tv.getItems().add(s);
}
This will put you in the right direction. Hope it helps/.
I have JFrame with a start button, which triggers the calculation of a Julia Set.
The code that is executed when the start button is clicked is as follows:
public void actionPerformed(ActionEvent aActionEvent)
{
String strCmd = aActionEvent.getActionCommand();
if (strCmd.equals("Start"))
{
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
this.handleCalculation();
}
else if (aActionEvent.getSource() == m_cTReal)
Which used to work fine, except that the application could not be closed anymore. So I tried to use m_bRunning in a separate method so that actionPerformed() isn't blocked all the time to see if that would help, and then set m_bRunning = false in the method stop() which is called when the window is closed:
public void run()
{
if(m_bRunning)
{
this.handleCalculation();
}
}
The method run() is called from the main class in a while(true) loop.
Yet unfortunately, neither did that solve the problem, nor do I now have any output to the canvas or any debug traces with System.out.println(). Could anyone point me in the right direction on this?
EDIT:
Here are the whole files:
// cMain.java
package juliaSet;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
public class cMain {
public static void main(String[] args)
{
int windowWidth = 1000;//(int)screenSize.getWidth() - 200;
int windowHeight = 800;//(int)screenSize.getHeight() - 50;
int plotWidth = 400;//(int)screenSize.getWidth() - 600;
int plotHeight = 400;//(int)screenSize.getHeight() - 150;
JuliaSet cJuliaSet = new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
cJuliaSet.setVisible(true);
while(true)
{
cJuliaSet.run();
}
}
}
// JuliaSet.java
package juliaSet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Random;
import java.io.*;
import java.lang.ref.*;
public class JuliaSet extends JFrame implements ActionListener
{
private JButton m_cBStart;
private JTextField m_cTReal;
private JTextField m_cTImag;
private JTextField m_cTDivergThresh;
private JLabel m_cLReal;
private JLabel m_cLImag;
private JLabel m_cLDivergThresh;
private int m_iDivergThresh = 10;
private String m_cMsgDivThresh = "Divergence threshold = " + m_iDivergThresh;
private JuliaCanvas m_cCanvas;
private int m_iPlotWidth; // number of cells
private int m_iPlotHeight; // number of cells
private Boolean m_bRunning = false;
private double m_dReal = 0.3;
private double m_dImag = -0.5;
private String m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
private String m_cMsgIter = "x = 0, y = 0";
private Complex m_cCoordPlane[][];
private double m_dAbsSqValues[][];
private int m_iIterations[][];
private Complex m_cSummand;
private BufferedImage m_cBackGroundImage = null;
private FileWriter m_cFileWriter;
private BufferedWriter m_cBufferedWriter;
private String m_sFileName = "log.txt";
private Boolean m_bWriteLog = false;
private static final double PLOTMAX = 2.0; // we'll have symmetric axes
// ((0,0) at the centre of the
// plot
private static final int MAXITER = 0xff;
JuliaSet(String aTitle, int aFrameWidth, int aFrameHeight, int aPlotWidth, int aPlotHeight)
{
super(aTitle);
this.setSize(aFrameWidth, aFrameHeight);
m_iPlotWidth = aPlotWidth;
m_iPlotHeight = aPlotHeight;
m_cSummand = new Complex(m_dReal, m_dImag);
m_cBackGroundImage = new BufferedImage(aFrameWidth, aFrameHeight, BufferedImage.TYPE_INT_RGB);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
stop();
super.windowClosing(e);
System.exit(0);
}
});
GridBagLayout cLayout = new GridBagLayout();
GridBagConstraints cConstraints = new GridBagConstraints();
this.setLayout(cLayout);
m_cCanvas = new JuliaCanvas(m_iPlotWidth, m_iPlotHeight);
m_cCanvas.setSize(m_iPlotWidth, m_iPlotHeight);
m_cBStart = new JButton("Start");
m_cBStart.addActionListener(this);
m_cTReal = new JTextField(5);
m_cTReal.addActionListener(this);
m_cTImag = new JTextField(5);
m_cTImag.addActionListener(this);
m_cTDivergThresh = new JTextField(5);
m_cTDivergThresh.addActionListener(this);
m_cLReal = new JLabel("Re(c):");
m_cLImag = new JLabel("Im(c):");
m_cLDivergThresh = new JLabel("Divergence Threshold:");
cConstraints.insets.top = 3;
cConstraints.insets.bottom = 3;
cConstraints.insets.right = 3;
cConstraints.insets.left = 3;
// cCanvas
cConstraints.gridx = 0;
cConstraints.gridy = 0;
cLayout.setConstraints(m_cCanvas, cConstraints);
this.add(m_cCanvas);
// m_cLReal
cConstraints.gridx = 0;
cConstraints.gridy = 1;
cLayout.setConstraints(m_cLReal, cConstraints);
this.add(m_cLReal);
// m_cTReal
cConstraints.gridx = 1;
cConstraints.gridy = 1;
cLayout.setConstraints(m_cTReal, cConstraints);
this.add(m_cTReal);
// m_cLImag
cConstraints.gridx = 0;
cConstraints.gridy = 2;
cLayout.setConstraints(m_cLImag, cConstraints);
this.add(m_cLImag);
// m_cTImag
cConstraints.gridx = 1;
cConstraints.gridy = 2;
cLayout.setConstraints(m_cTImag, cConstraints);
this.add(m_cTImag);
// m_cLDivergThresh
cConstraints.gridx = 0;
cConstraints.gridy = 3;
cLayout.setConstraints(m_cLDivergThresh, cConstraints);
this.add(m_cLDivergThresh);
// m_cTDivergThresh
cConstraints.gridx = 1;
cConstraints.gridy = 3;
cLayout.setConstraints(m_cTDivergThresh, cConstraints);
this.add(m_cTDivergThresh);
// m_cBStart
cConstraints.gridx = 0;
cConstraints.gridy = 4;
cLayout.setConstraints(m_cBStart, cConstraints);
this.add(m_cBStart);
if (m_bWriteLog)
{
try
{
m_cFileWriter = new FileWriter(m_sFileName, false);
m_cBufferedWriter = new BufferedWriter(m_cFileWriter);
} catch (IOException ex) {
System.out.println("Error opening file '" + m_sFileName + "'");
}
}
this.repaint();
this.transformCoordinates();
}
public synchronized void stop()
{
if (m_bRunning)
{
m_bRunning = false;
boolean bRetry = true;
}
if (m_bWriteLog)
{
try {
m_cBufferedWriter.close();
m_cFileWriter.close();
} catch (IOException ex) {
System.out.println("Error closing file '" + m_sFileName + "'");
}
}
}
public void collectGarbage()
{
Object cObj = new Object();
WeakReference ref = new WeakReference<Object>(cObj);
cObj = null;
while(ref.get() != null) {
System.gc();
}
}
public void setSummand(Complex aSummand)
{
m_cSummand.setIm(aSummand.getIm());
m_dImag = aSummand.getIm();
m_cSummand.setRe(aSummand.getRe());
m_dReal = aSummand.getRe();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
}
public void paint(Graphics aGraphics)
{
Graphics cScreenGraphics = aGraphics;
// render on background image
aGraphics = m_cBackGroundImage.getGraphics();
this.paintComponents(aGraphics);
// drawString() calls are debug code only....
aGraphics.setColor(Color.BLACK);
aGraphics.drawString(m_cSMsg, 10, 450);
aGraphics.drawString(m_cMsgIter, 10, 465);
aGraphics.drawString(m_cMsgDivThresh, 10, 480);
// rendering is done, draw background image to on screen graphics
cScreenGraphics.drawImage(m_cBackGroundImage, 0, 0, null);
}
public void actionPerformed(ActionEvent aActionEvent)
{
String strCmd = aActionEvent.getActionCommand();
if (strCmd.equals("Start"))
{
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
}
else if (aActionEvent.getSource() == m_cTReal)
{
m_dReal = Double.parseDouble(m_cTReal.getText());
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_cSummand.setRe(m_dReal);
}
else if (aActionEvent.getSource() == m_cTImag)
{
m_dImag = Double.parseDouble(m_cTImag.getText());
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_cSummand.setIm(m_dImag);
}
else if (aActionEvent.getSource() == m_cTDivergThresh)
{
m_iDivergThresh = Integer.parseInt(m_cTDivergThresh.getText());
m_cMsgDivThresh = "Divergence threshold = " + m_iDivergThresh;
}
this.update(this.getGraphics());
}
public void transformCoordinates()
{
double dCanvasHeight = (double) m_cCanvas.getHeight();
double dCanvasWidth = (double) m_cCanvas.getWidth();
// init matrix with same amount of elements as pixels in canvas
m_cCoordPlane = new Complex[(int) dCanvasHeight][(int) dCanvasWidth];
double iPlotRange = 2 * PLOTMAX;
for (int i = 0; i < dCanvasHeight; i++)
{
for (int j = 0; j < dCanvasWidth; j++)
{
m_cCoordPlane[i][j] = new Complex((i - (dCanvasWidth / 2)) * iPlotRange / dCanvasWidth,
(j - (dCanvasHeight / 2)) * iPlotRange / dCanvasHeight);
}
}
}
public void calcAbsSqValues()
{
int iCanvasHeight = m_cCanvas.getHeight();
int iCanvasWidth = m_cCanvas.getWidth();
// init matrix with same amount of elements as pixels in canvas
m_dAbsSqValues = new double[iCanvasHeight][iCanvasWidth];
m_iIterations = new int[iCanvasHeight][iCanvasWidth];
Complex cSum = new Complex();
if (m_bWriteLog) {
try
{
m_cBufferedWriter.write("m_iIterations[][] =");
m_cBufferedWriter.newLine();
}
catch (IOException ex)
{
System.out.println("Error opening file '" + m_sFileName + "'");
}
}
for (int i = 0; i < iCanvasHeight; i++)
{
for (int j = 0; j < iCanvasWidth; j++)
{
cSum.setRe(m_cCoordPlane[i][j].getRe());
cSum.setIm(m_cCoordPlane[i][j].getIm());
m_iIterations[i][j] = 0;
do
{
m_iIterations[i][j]++;
cSum.square();
cSum.add(m_cSummand);
m_dAbsSqValues[i][j] = cSum.getAbsSq();
} while ((m_iIterations[i][j] < MAXITER) && (m_dAbsSqValues[i][j] < m_iDivergThresh));
this.calcColour(i, j, m_iIterations[i][j]);
m_cMsgIter = "x = " + i + " , y = " + j;
if(m_bWriteLog)
{
System.out.println(m_cMsgIter);
System.out.flush();
}
if (m_bWriteLog) {
try
{
m_cBufferedWriter.write(Integer.toString(m_iIterations[i][j]));
m_cBufferedWriter.write(" ");
}
catch (IOException ex) {
System.out.println("Error writing to file '" + m_sFileName + "'");
}
}
}
if (m_bWriteLog) {
try
{
m_cBufferedWriter.newLine();
}
catch (IOException ex) {
System.out.println("Error writing to file '" + m_sFileName + "'");
}
}
}
m_dAbsSqValues = null;
m_iIterations = null;
cSum = null;
}
private void calcColour(int i, int j, int aIterations)
{
Color cColour = Color.getHSBColor((int) Math.pow(aIterations, 4), 0xff,
0xff * ((aIterations < MAXITER) ? 1 : 0));
m_cCanvas.setPixelColour(i, j, cColour);
cColour = null;
}
private void handleCalculation()
{
Complex cSummand = new Complex();
for(int i = -800; i <= 800; i++)
{
for(int j = -800; j <= 800; j++)
{
cSummand.setRe(((double)i)/1000.0);
cSummand.setIm(((double)j)/1000.0);
this.setSummand(cSummand);
this.calcAbsSqValues();
this.getCanvas().paint(m_cCanvas.getGraphics());
this.paint(this.getGraphics());
}
}
cSummand = null;
this.collectGarbage();
System.gc();
System.runFinalization();
}
public boolean isRunning()
{
return m_bRunning;
}
public void setRunning(boolean aRunning)
{
m_bRunning = aRunning;
}
public Canvas getCanvas()
{
return m_cCanvas;
}
public void run()
{
if(m_bRunning)
{
this.handleCalculation();
}
}
}
class JuliaCanvas extends Canvas
{
private int m_iWidth;
private int m_iHeight;
private Random m_cRnd;
private BufferedImage m_cBackGroundImage = null;
private int m_iRed[][];
private int m_iGreen[][];
private int m_iBlue[][];
JuliaCanvas(int aWidth, int aHeight)
{
m_iWidth = aWidth;
m_iHeight = aHeight;
m_cRnd = new Random();
m_cRnd.setSeed(m_cRnd.nextLong());
m_cBackGroundImage = new BufferedImage(m_iWidth, m_iHeight, BufferedImage.TYPE_INT_RGB);
m_iRed = new int[m_iHeight][m_iWidth];
m_iGreen = new int[m_iHeight][m_iWidth];
m_iBlue = new int[m_iHeight][m_iWidth];
}
public void init() {
}
public void setPixelColour(int i, int j, Color aColour)
{
m_iRed[i][j] = aColour.getRed();
m_iGreen[i][j] = aColour.getGreen();
m_iBlue[i][j] = aColour.getBlue();
}
private int getRandomInt(double aProbability)
{
return (m_cRnd.nextDouble() < aProbability) ? 1 : 0;
}
#Override
public void paint(Graphics aGraphics)
{
// store on screen graphics
Graphics cScreenGraphics = aGraphics;
// render on background image
aGraphics = m_cBackGroundImage.getGraphics();
for (int i = 0; i < m_iWidth; i++)
{
for (int j = 0; j < m_iHeight; j++)
{
Color cColor = new Color(m_iRed[i][j], m_iGreen[i][j], m_iBlue[i][j]);
aGraphics.setColor(cColor);
aGraphics.drawRect(i, j, 0, 0);
cColor = null;
}
}
// rendering is done, draw background image to on screen graphics
cScreenGraphics.drawImage(m_cBackGroundImage, 1, 1, null);
}
#Override
public void update(Graphics aGraphics)
{
paint(aGraphics);
}
}
class Complex {
private double m_dRe;
private double m_dIm;
public Complex()
{
m_dRe = 0;
m_dIm = 0;
}
public Complex(double aRe, double aIm)
{
m_dRe = aRe;
m_dIm = aIm;
}
public Complex(Complex aComplex)
{
m_dRe = aComplex.m_dRe;
m_dIm = aComplex.m_dIm;
}
public double getRe() {
return m_dRe;
}
public void setRe(double adRe)
{
m_dRe = adRe;
}
public double getIm() {
return m_dIm;
}
public void setIm(double adIm)
{
m_dIm = adIm;
}
public void add(Complex acComplex)
{
m_dRe += acComplex.getRe();
m_dIm += acComplex.getIm();
}
public void square()
{
double m_dReSave = m_dRe;
m_dRe = (m_dRe * m_dRe) - (m_dIm * m_dIm);
m_dIm = 2 * m_dReSave * m_dIm;
}
public double getAbsSq()
{
return ((m_dRe * m_dRe) + (m_dIm * m_dIm));
}
}
I'm quoting a recent comment from #MadProgrammer (including links)
"Swing is single threaded, nothing you can do to change that, all events are posted to the event queue and processed by the Event Dispatching Thread, see Concurrency in Swing for more details and have a look at Worker Threads and SwingWorker for at least one possible solution"
There is only one thread in your code. That thread is busy doing the calculation and can not respond to events located in the GUI. You have to separate the calculation in another thread that periodically updates the quantities that appears in the window. More info about that in the links, courtesy of #MadProgrammer, I insist.
UPDATED: As pointed by #Yusuf, the proper way of launching the JFrame is
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
}
});
Set the frame visible on construction and start calculation when the start button is pressed.
First;
Endless loop is not a proper way to do this. This part is loops and taking CPU and never give canvas to refresh screen. if you add below code your code will run as expected. but this is not the proper solution.
cMain.java:
while (true) {
cJuliaSet.run();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Second: you could call run method when start button clicked. But you should create a thread in run method to not freeze screen.
public static void main(String[] args) {
int windowWidth = 1000;// (int)screenSize.getWidth() - 200;
int windowHeight = 800;// (int)screenSize.getHeight() - 50;
int plotWidth = 400;// (int)screenSize.getWidth() - 600;
int plotHeight = 400;// (int)screenSize.getHeight() - 150;
JuliaSet cJuliaSet = new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
cJuliaSet.setVisible(true);
//While loop removed
}
actionPerformed:
if (strCmd.equals("Start")) {
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
this.run(); // added call run method.
} else if (aActionEvent.getSource() == m_cTReal) {
run method:
public void run()
{
if(m_bRunning)
{
new Thread(){ //Thread to release screen
#Override
public void run() {
JuliaSet.this.handleCalculation();
}
}.start(); //also thread must be started
}
}
As said by #RubioRic, SwingUtilities.invokeLater method is also a part of solution. But you need to check whole of your code and you should learn about Threads.