Hi Guys,
I ussually dont post anything here often, just get everything off of everyone elses questions,
but I'm having a strange problem that im scratching my head over,
I have a JTable where I can Add and remove rows... This is just for testing purposes, I'm about to make a ingrediants program.
The thing is whenever I change one of the Comboboxes they all seem to change.. its really wierd,
Could Someone run this and help me out, this is probably a stupid mistake?
See my Code Below,
My Main
package doughtracker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
*
* @Author Jonathan
*/
public class DoughTracker {
static Dimensions dimensions;
public static void main(String[] args) {
dimensions = new Dimensions();
try {
// Set System L&F
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
System.out.println(e);
}
Window window = new Window();
window.window();
}
}
Dimensions class,
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package doughtracker;
/**
*
* @author Jonathan Schep
*/
public class Dimensions {
int windowWidth;
int windowHieght;
int ingrediantPanelX;
int ingrediantPanelY;
int ingrediantPanelWidth;
int ingrediantPanelHieght;
int notesPanelX;
int notesPanelY;
int notesPanelWidth;
int notesPanelHieght;
int doughCreatorX;
int doughCreatorY;
int doughCreatorWidth;
int doughCreatorHieght;
public Dimensions() {
windowWidth = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
windowHieght = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
ingrediantPanelX = 50;
ingrediantPanelY = 50;
ingrediantPanelWidth = windowWidth / 3;
ingrediantPanelHieght = windowHieght - 125;
notesPanelWidth = windowWidth / 4;
notesPanelHieght = windowHieght / 3;
notesPanelX = windowWidth - notesPanelWidth - 50;
notesPanelY = windowHieght - notesPanelHieght - 75;
doughCreatorX = 50;
doughCreatorY= 50;
doughCreatorWidth = windowWidth - 100;
doughCreatorHieght = windowHieght-125;
}
public int getdoughCreatorX() {
return doughCreatorX;
}
public int getdoughCreatorY() {
return doughCreatorY;
}
public int getdoughCreatorWidth() {
return doughCreatorWidth;
}
public int getdoughCreatorHieght() {
return doughCreatorHieght;
}
public int getNotesPanelX() {
return notesPanelX;
}
public int getNotesPanelY() {
return notesPanelY;
}
public int getNotesPanelWidth() {
return notesPanelWidth;
}
public int getNotesPanelHieght() {
return notesPanelHieght;
}
public int getIngrediantPanelX() {
return ingrediantPanelX;
}
public int getIngrediantPanelY() {
return ingrediantPanelY;
}
public int getIngrediantPanelWidth() {
return ingrediantPanelWidth;
}
public int getIngrediantPanelHieght() {
return ingrediantPanelHieght;
}
public int getWindowWidth() {
return windowWidth;
}
public int getWindowHieght() {
return windowHieght;
}
}
my Dough Creator pane
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package doughtracker;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
/**
*
* @author Jonathan Schep
*/
public class DoughCreator extends JPanel {
public DoughCreator() {
Border GrayLine = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
super.setBounds(DoughTracker.dimensions.doughCreatorX, DoughTracker.dimensions.doughCreatorY, DoughTracker.dimensions.doughCreatorWidth, DoughTracker.dimensions.doughCreatorHieght);
super.setLayout(null);
super.setBorder(GrayLine);
JLabel NameLabel = new JLabel("Dough Name:");
NameLabel.setFont(new Font("Ariel", Font.BOLD + Font.ITALIC, 32));
NameLabel.setBounds(25, 25, 250, 50);
super.add(NameLabel);
JLabel doughName = new JLabel("Gluten Free testing label");
doughName.setFont(new Font("Ariel", Font.PLAIN + Font.ITALIC, 32));
doughName.setBounds(260, 25, DoughTracker.dimensions.doughCreatorWidth - 300, 50);
super.add(doughName);
JTextField doughTypeTextField = new JTextField("");
doughTypeTextField.setFont(new Font("Ariel", Font.PLAIN, 32));
doughTypeTextField.setOpaque(false);
doughTypeTextField.setToolTipText("The name of the Dough...");
doughTypeTextField.setBounds(260, 25, DoughTracker.dimensions.doughCreatorWidth - 300, 50);
doughTypeTextField.setBorder(GrayLine);
doughTypeTextField.setEditable(true);
doughTypeTextField.setVisible(false);
super.add(doughTypeTextField);
JTable ingrediantsTable = new JTable() {
@Override
public boolean isCellEditable(int row, int column) {
return true;
}
};
ingrediantsTable.setGridColor(Color.LIGHT_GRAY);
ingrediantsTable.getTableHeader().setFont(new Font("Arial", Font.BOLD, 16));
ingrediantsTable.setFont(new Font("Ariel", Font.PLAIN, 16));
// MainWindowTable.setDefaultRenderer(Object.class, new MyCellRenderer());
ingrediantsTable.setRowHeight(25);
ingrediantsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ingrediantsTable.getTableHeader().setPreferredSize(new Dimension(300, 40));
ingrediantsTable.setFocusable(false);
DefaultTableModel ingrediantsTableModel = new DefaultTableModel();
ingrediantsTableModel.addColumn("Ingrediant");
ingrediantsTableModel.addColumn("Amount");
ingrediantsTableModel.addColumn("Unit");
ingrediantsTable.setModel(ingrediantsTableModel);
String[] values = new String[] { "1", "2", "3" };
TableColumn col = ingrediantsTable.getColumnModel().getColumn(0);
col.setCellEditor(new MyComboBoxEditor(values));
col.setCellRenderer(new MyComboBoxRenderer(values));
JScrollPane ingrediantsTableScrollPane = new JScrollPane(ingrediantsTable);
ingrediantsTableScrollPane.setBounds(25, 100, DoughTracker.dimensions.doughCreatorWidth / 3, DoughTracker.dimensions.doughCreatorHieght - 100);
ingrediantsTableScrollPane.setVisible(true);
ingrediantsTableScrollPane.setBackground(Color.WHITE);
super.add(ingrediantsTableScrollPane);
JButton addIngrediantButton = new JButton("+");
addIngrediantButton.setFont(new Font("Arial", Font.BOLD, 18));
addIngrediantButton.setToolTipText("Add an Ingrediant");
addIngrediantButton.setOpaque(false);
addIngrediantButton.setContentAreaFilled(true);
addIngrediantButton.setBorderPainted(false);
addIngrediantButton.setFocusable(false);
addIngrediantButton.setBounds(DoughTracker.dimensions.doughCreatorWidth / 3 + 50, 100, 50, 50);
addIngrediantButton.setVisible(true);
addIngrediantButton.setEnabled(true);
addIngrediantButton.addActionListener((ActionEvent e) -> {
ingrediantsTableModel.addRow(new Object[]{"", ""});
});
super.add(addIngrediantButton);
JButton removeIngrediantButton = new JButton("-");
removeIngrediantButton.setFont(new Font("Arial", Font.BOLD, 18));
removeIngrediantButton.setToolTipText("Add an Ingrediant");
removeIngrediantButton.setOpaque(false);
removeIngrediantButton.setContentAreaFilled(true);
removeIngrediantButton.setBorderPainted(false);
removeIngrediantButton.setFocusable(false);
removeIngrediantButton.setBounds(DoughTracker.dimensions.doughCreatorWidth / 3 + 50, 160, 50, 50);
removeIngrediantButton.setVisible(true);
removeIngrediantButton.setEnabled(true);
removeIngrediantButton.addActionListener((ActionEvent e) -> {
if (ingrediantsTable.getSelectedRow() != -1) {
ingrediantsTableModel.removeRow(ingrediantsTable.getSelectedRow());
} else {
Toolkit.getDefaultToolkit().beep();
}
});
super.add(removeIngrediantButton);
}
class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
public MyComboBoxRenderer(String[] items) {
super(items);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
return this;
}
}
class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}
}