My JTable doesn't show up

272 views Asked by At

I'm new to JTable, or GUI for that matter, but i had been given an assignment of building a receipt program inside a GUI. I manage to get the basic things work, but my table looks awful. I need help on how to display the table properly

public static void main (String[] args)
{       
    ArrayList <item> lol= new ArrayList <item>();
    item ayam = new item("ayam",5678);
    item kambing= new item("kambing",5014);
    item buaya= new item("buaya",3000);
    item bocoranquiz= new item("bocoranquiz",5000);
    lol.add(ayam);
    lol.add(kambing);
    lol.add(buaya);
    lol.add(bocoranquiz);
    JFrame frame= new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 0));
    frame.setSize(1000, 1000);
    SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
    String date = sdf.format(new Date()); 

    JComboBox combo1= new JComboBox();
    combo1.setPreferredSize(new Dimension(10, 10));
    combo1.addItem(ayam.getname());
    combo1.addItem(kambing.getname());
    combo1.addItem(buaya.getname());
    combo1.addItem(bocoranquiz.getname());
    JLabel label1= new JLabel("Invoice no: ");
    JLabel label2= new JLabel("Invoice Date : " + date);
    JLabel label3= new JLabel("Item name " );
    JLabel label4= new JLabel("Item Price ");
    JLabel label5= new JLabel("Item Quantity : ");
    JPanel panel1= new JPanel ();
    panel1.setLayout(new GridLayout(3,0));
    panel1.add(label1);
    panel1.add(label2);

    class inputListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {   
            JComboBox<String> combo= (JComboBox<String>) event.getSource();
            selected= (String) combo.getSelectedItem();             
            alpha=10;        
            if (selected.equals("ayam"))
            {           
                alpha=5678; 
            }       
            else if (selected.equals("kambing"))
            {           
                alpha=5014; 
            }       
            else if (selected.equals("buaya"))
            {           
                alpha=3000; 
            }       
            else if (selected.equals("bocoranquiz"))
            {           
                alpha=5000; 
            }   
            label3.setText("Item name " + selected);
            label4.setText("Item quantity " + alpha);
        }           
    }
    ActionListener inputAct = new inputListener();
    combo1.addActionListener(inputAct);
    panel1.add(combo1);     
    JTextField tf = new JTextField();
    JButton adda = new JButton("Add");
    String[] columnNames= {"Name","Price","Quantity","Total"};
    DefaultTableModel tablemodel= new DefaultTableModel(columnNames,0);
    JTable table = new JTable(tablemodel);      
    JScrollPane scrollPanel = new JScrollPane(table);
    class inputListener2 implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            quantity= Integer.parseInt(tf.getText());
            double total= alpha * quantity;
            Object[] data= {selected,alpha,quantity,total};
            tablemodel.addRow(data);
        }
    ActionListener inputAct2 = new inputListener2();
    adda.addActionListener(inputAct2);
    JPanel panel2= new JPanel();
    panel2.setLayout(new GridLayout(10,0));
    panel2.add(label3);
    panel2.add(label4);
    panel2.add(label5);
    panel2.add(tf);
    panel2.add(adda);
    panel2.add(scrollPanel);
    frame.add(panel1);
    frame.add(panel2);
    frame.setVisible(true);
}
1

There are 1 answers

0
MadProgrammer On

Your code is a bit of mess, but, the basic problem is your setting your GridLayout with to many rows, for example

frame.setLayout(new GridLayout(3, 0));

should be

frame.setLayout(new GridLayout(2, 0));

since you're only adding 2 components to the frame

Equally,

panel2.setLayout(new GridLayout(10, 0));

should probably be

panel2.setLayout(new GridLayout(6, 0));

Remember, a GridLayout will divide the container into equal sections, even if there is nothing contained within the row/column.

You may also want consider using a different layout manager, like GridBagLayout, or a combination of layouts depending on your basic needs.

See How to use GridBagLayout for more details.