Mesh89
12-10-2008, 16:51
Ecco il codice. Il problema è che, nel metodo initClientePanel, quando cerco di fare add, mi lancia un NullPointerException. Investigando, ho scoperto che clientePanel, al momento della add, è a null.
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class NewGUI extends JFrame {
private static NewGUI inst;
private Core coreProgramma = new Core();
private JPanel clientiPanel;
private JPanel progettiPanel;
private JPanel preventiviPanel;
private JPanel fatturePanel;
private DefaultTableModel clientiListData;
private JTable clientiList;
private JButton apriAggiungiClienteFrameButton;
private NewGUI(int width, int height) {
setTitle("Gestione fatture");
setSize(new Dimension(width, height));
setLocation(100,100);
this.setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private static void createAndShowGUI() {
inst = new NewGUI(1024,768);
inst.initMainPanels();
inst.pack();
inst.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private void initMainPanels() {
initMainPanel(clientiPanel);
initClientiPanel();
initMainPanel(progettiPanel);
initMainPanel(preventiviPanel);
initMainPanel(fatturePanel);
}
private void initMainPanel(JPanel panel) {
panel = new JPanel(new GridBagLayout());
panel.setPreferredSize(new Dimension(this.getWidth()/4, this.getHeight()));
panel.setBorder(new LineBorder(Color.GRAY));
this.add(panel);
panel.setVisible(true);
}
private void initClientiPanel() {
GridBagConstraints c = new GridBagConstraints();
clientiListData = new DefaultTableModel(0,1);
clientiList = new JTable(clientiListData);
/*clientiList.setBorder(new LineBorder(Color.GRAY));
clientiList.setPreferredSize(new Dimension(clientiPanel.getWidth(), (int)((3.0/4.0)*clientiPanel.getHeight())));
clientiList.getColumnModel().getColumn(0).setCellRenderer(MyCellRenderer.MY_RENDERER);
clientiList.setVisible(true);*/
//clientiPanel.add(clientiList, c);
apriAggiungiClienteFrameButton = new JButton("Aggiungi cliente");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
if (clientiPanel == null) System.out.println("Errore");
clientiPanel.add(apriAggiungiClienteFrameButton);
}
private void fillClientiListData()
{
for (int i = clientiListData.getRowCount()-1; i >= 0; i--)
clientiListData.removeRow(i);
for (Cliente c : coreProgramma.getClientiList())
clientiListData.addRow(new Object[] {
"<HTML>"+c.getSocieta()+"<br>"+
c.getAnagrafica().getNome()+" "+c.getAnagrafica().getCognome()+"<br>"+
"</HTML>"
});
}
static class MultilineCellRenderer extends DefaultTableCellRenderer {
public static final MultilineCellRenderer MY_RENDERER = new MultilineCellRenderer();
public Component getTableCellRendererComponent (JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
Component c = super.getTableCellRendererComponent (table, value,
isSelected, hasFocus, row, column);
int height = c.getPreferredSize ().height;
if (height > table.getRowHeight (row)) {
table.setRowHeight (row, height);
}
return c;
}
}
}
Eppure nel metodo initMainPanel(clientePanel) avrebbe dovuto inizializzarla, no?
Purtroppo in Java sono abbastanza niubbo, è praticamente la prima volta che ci lavoro, per cui probabilmente è una cavolata o una sfumatura del linguaggio che non conosco, e che qualcuno più esperto di me troverà sicuramente
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class NewGUI extends JFrame {
private static NewGUI inst;
private Core coreProgramma = new Core();
private JPanel clientiPanel;
private JPanel progettiPanel;
private JPanel preventiviPanel;
private JPanel fatturePanel;
private DefaultTableModel clientiListData;
private JTable clientiList;
private JButton apriAggiungiClienteFrameButton;
private NewGUI(int width, int height) {
setTitle("Gestione fatture");
setSize(new Dimension(width, height));
setLocation(100,100);
this.setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private static void createAndShowGUI() {
inst = new NewGUI(1024,768);
inst.initMainPanels();
inst.pack();
inst.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private void initMainPanels() {
initMainPanel(clientiPanel);
initClientiPanel();
initMainPanel(progettiPanel);
initMainPanel(preventiviPanel);
initMainPanel(fatturePanel);
}
private void initMainPanel(JPanel panel) {
panel = new JPanel(new GridBagLayout());
panel.setPreferredSize(new Dimension(this.getWidth()/4, this.getHeight()));
panel.setBorder(new LineBorder(Color.GRAY));
this.add(panel);
panel.setVisible(true);
}
private void initClientiPanel() {
GridBagConstraints c = new GridBagConstraints();
clientiListData = new DefaultTableModel(0,1);
clientiList = new JTable(clientiListData);
/*clientiList.setBorder(new LineBorder(Color.GRAY));
clientiList.setPreferredSize(new Dimension(clientiPanel.getWidth(), (int)((3.0/4.0)*clientiPanel.getHeight())));
clientiList.getColumnModel().getColumn(0).setCellRenderer(MyCellRenderer.MY_RENDERER);
clientiList.setVisible(true);*/
//clientiPanel.add(clientiList, c);
apriAggiungiClienteFrameButton = new JButton("Aggiungi cliente");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
if (clientiPanel == null) System.out.println("Errore");
clientiPanel.add(apriAggiungiClienteFrameButton);
}
private void fillClientiListData()
{
for (int i = clientiListData.getRowCount()-1; i >= 0; i--)
clientiListData.removeRow(i);
for (Cliente c : coreProgramma.getClientiList())
clientiListData.addRow(new Object[] {
"<HTML>"+c.getSocieta()+"<br>"+
c.getAnagrafica().getNome()+" "+c.getAnagrafica().getCognome()+"<br>"+
"</HTML>"
});
}
static class MultilineCellRenderer extends DefaultTableCellRenderer {
public static final MultilineCellRenderer MY_RENDERER = new MultilineCellRenderer();
public Component getTableCellRendererComponent (JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
Component c = super.getTableCellRendererComponent (table, value,
isSelected, hasFocus, row, column);
int height = c.getPreferredSize ().height;
if (height > table.getRowHeight (row)) {
table.setRowHeight (row, height);
}
return c;
}
}
}
Eppure nel metodo initMainPanel(clientePanel) avrebbe dovuto inizializzarla, no?
Purtroppo in Java sono abbastanza niubbo, è praticamente la prima volta che ci lavoro, per cui probabilmente è una cavolata o una sfumatura del linguaggio che non conosco, e che qualcuno più esperto di me troverà sicuramente