From 6a7ded104eddd5e8d4b4e83204cf1fa625ad47bb Mon Sep 17 00:00:00 2001 From: Robin Cheney Date: Thu, 20 Nov 2025 15:26:52 +0100 Subject: [PATCH] fixed insta-crash bug --- .../tankstelle/FuelStationUI.java | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java b/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java index 02a8676..54c1f26 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java @@ -17,7 +17,7 @@ public class FuelStationUI extends JFrame { private JComboBox comboFuelStations; private JComboBox comboFuelTypes; private JTextArea outputArea; - private JPanel contentPane; + private JPanel contentPane = new JPanel(); private JList listFuelStations; private JTable tableFuelTypesPerStation; private javax.swing.table.DefaultTableModel fuelTableModel; @@ -184,7 +184,8 @@ public class FuelStationUI extends JFrame { } float total = Main.getTotalStockLevelOfFuel(type, selectedStations); - appendOutput(String.format("Total stock of %s across %d chosen fuel stations: %.2f L", type, indices.length, total)); + appendOutput(String.format("Total stock of %s across %d chosen fuel stations: %.2f L", type, indices.length, + total)); }); buttonPanel.add(btnTotalPrice); @@ -217,7 +218,7 @@ public class FuelStationUI extends JFrame { private JPanel createPricePanel() { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); - gbc.insets = new Insets(5,5,5,5); + gbc.insets = new Insets(5, 5, 5, 5); gbc.fill = GridBagConstraints.HORIZONTAL; JLabel lblPrice = new JLabel("New price per litre (€):"); @@ -236,8 +237,7 @@ public class FuelStationUI extends JFrame { station.set_price(type, newPrice); appendOutput(String.format( "Price changed of %s at %s to %.3f €/L.", - type, getDisplayName(station), newPrice - )); + type, getDisplayName(station), newPrice)); txtPrice.setText(""); } catch (NumberFormatException ex) { showError("Please enter a valid number for the price."); @@ -246,12 +246,15 @@ public class FuelStationUI extends JFrame { } }); - gbc.gridx = 0; gbc.gridy = 0; + gbc.gridx = 0; + gbc.gridy = 0; panel.add(lblPrice, gbc); gbc.gridx = 1; panel.add(txtPrice, gbc); - gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 2; + gbc.gridx = 0; + gbc.gridy = 1; + gbc.gridwidth = 2; panel.add(btnSave, gbc); return panel; @@ -265,7 +268,7 @@ public class FuelStationUI extends JFrame { private JPanel createStockPanel() { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); - gbc.insets = new Insets(5,5,5,5); + gbc.insets = new Insets(5, 5, 5, 5); gbc.fill = GridBagConstraints.HORIZONTAL; JLabel lblAmount = new JLabel("Amount (litre):"); @@ -302,8 +305,7 @@ public class FuelStationUI extends JFrame { station.set_stored_amount(value, type); appendOutput(String.format( "Changed stock of %s at %s to %.2f litre.", - type, getDisplayName(station), value - )); + type, getDisplayName(station), value)); txtAmount.setText(""); } else { float updated = current + value; @@ -319,8 +321,7 @@ public class FuelStationUI extends JFrame { station.add_stored_amount(value, type); appendOutput(String.format( "Stock changed of %s at %s by %.2f litre.", - type, getDisplayName(station), value - )); + type, getDisplayName(station), value)); txtAmount.setText(""); } } catch (NumberFormatException ex) { @@ -333,13 +334,16 @@ public class FuelStationUI extends JFrame { }); int y = 0; - gbc.gridx = 0; gbc.gridy = y; + gbc.gridx = 0; + gbc.gridy = y; panel.add(lblAmount, gbc); gbc.gridx = 1; panel.add(txtAmount, gbc); y++; - gbc.gridx = 0; gbc.gridy = y; gbc.gridwidth = 2; + gbc.gridx = 0; + gbc.gridy = y; + gbc.gridwidth = 2; panel.add(rbAbsolute, gbc); y++; @@ -353,7 +357,7 @@ public class FuelStationUI extends JFrame { return panel; } - private int getCapacity(FuelStation station, FuelType type) throws NoSuchFuelTypeError { + private int getCapacity(FuelStation station, FuelType type) throws NoSuchFuelTypeError { for (Fuel f : station.fuels) { if (f.FUEL_TYPE == type) { return f.CAPACITY; @@ -414,7 +418,7 @@ public class FuelStationUI extends JFrame { JTextField txtAtrributes = new JTextField(15); JLabel lblHint = new JLabel("Small: amount of vending machines, Medium: m^2, Large: supermarket-company"); - JButton btnAdd = new JButton("Add fuel station"); + JButton btnAdd = new JButton("Add fuel station"); btnAdd.addActionListener(e -> { try { if (rbSmall.isSelected()) { @@ -428,7 +432,7 @@ public class FuelStationUI extends JFrame { } else if (rbLarge.isSelected()) { String supermarketCompany = txtAtrributes.getText().trim(); Main.addNewFuelStation(supermarketCompany); - appendOutput("Fuel station with supermarket-company" + supermarketCompany + " has been added."); + appendOutput("Fuel station with supermarket-company" + supermarketCompany + " has been added."); } refreshStationNames(); txtAtrributes.setText(""); @@ -440,7 +444,8 @@ public class FuelStationUI extends JFrame { }); int y = 0; - gbc.gridx = 0; gbc.gridy = y; + gbc.gridx = 0; + gbc.gridy = y; gbc.gridwidth = 3; panel.add(new JLabel("Type of new fuel station:"), gbc); @@ -454,13 +459,16 @@ public class FuelStationUI extends JFrame { panel.add(rbLarge, gbc); y++; - gbc.gridx = 0; gbc.gridy = y; + gbc.gridx = 0; + gbc.gridy = y; panel.add(lblAtrributes, gbc); - gbc.gridx = 1; gbc.gridwidth = 2; + gbc.gridx = 1; + gbc.gridwidth = 2; panel.add(txtAtrributes, gbc); y++; - gbc.gridx = 0; gbc.gridy = y; + gbc.gridx = 0; + gbc.gridy = y; gbc.gridwidth = 3; panel.add(lblHint, gbc); @@ -481,7 +489,9 @@ public class FuelStationUI extends JFrame { private JPanel createOverviewPanel() { JPanel panel = new JPanel(new BorderLayout()); - fuelTableModel = new DefaultTableModel(new Object[]{"Fuel station", "Station type", "Fuel", "Amount (L)", "Capacity (L)", "Price (€/L)"}, 0) { + fuelTableModel = new DefaultTableModel( + new Object[] { "Fuel station", "Station type", "Fuel", "Amount (L)", "Capacity (L)", "Price (€/L)" }, + 0) { @Override public boolean isCellEditable(int row, int column) { return false; -- 2.43.0