Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

display final result

 
oghenekaro EFEKODO
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
need help making the following in my code work:
1.how do i display the whole table in my code using a dialog box or JOptionPane.
2. i want to display a message beneath the table show the total price of items selected from the JComboBox .
3.is there any way i could add a method or sommething that display detail of each item seleted from the list in a seperate column of the JTable in my code.say for instance,if i selects beans,1 cup, 100 and it display protein in a seperate column in the JTable.

Thanks in advance.

my code:

'\n'
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

import java.util.*;

public class BreakFast extends JFrame implements ActionListener{

private JList ingredient;
private JTable table;
private DefaultTableModel model;
private JButton move;
private String[] food;
private JComboBox box,box1;
private String[] units;
private double[] price={100,150,200,250,300,350,400};
private JButton finish;


public BreakFast(){
Container c=getContentPane();
c.setLayout(new FlowLayout());

food = new String[] {"Corn Flakes","Beans","Shredded Bread","Mushroom",
"eggs","Milks","Butter","Sugar","water","Oil"};

ingredient = new JList(food);
ingredient.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
ingredient.setVisibleRowCount(4);

JPanel p = new JPanel();
p.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));

move = new JButton(">>>");
move.addActionListener(this);

//meal = new JTextArea(5,20);
//meal.setEditable(false);

p.add(new JScrollPane(ingredient),"Wast");

JPanel p2 = new JPanel();
p2.setBorder(new BevelBorder(BevelBorder.RAISED));

units = new String[]{"2 cups","3 cups","4 cups","5 cups",
"1 mudu","2 mudu","3 mudu","4 mudu","5 mudu","6 mudu",
"7 mudu","8 mudu","9 mudu","1 bag"};
box = new JComboBox(units);
box.addActionListener(this);

box1 = new JComboBox();
//box1.setEditable(true);
box1.addActionListener(this);
for(int i=0;i<price.length;i++){
box1.addItem(price[i]);
}



model = new DefaultTableModel();

//model.addColumn("No.");
model.addColumn("Food Items");
model.addColumn("Units");
model.addColumn("Price");



table = new JTable(model);

JScrollPane pane = new JScrollPane(table);
pane.setPreferredSize(new Dimension(350,100));

finish = new JButton("Finish");
finish.addActionListener(this);

p.add(box,"West");
p.add(box1,"Center");
p.add(move,"East");
p2.add(pane,"North");
p2.add(finish,"South");

c.add(p);
c.add(p2);

setSize(450,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e){
Object[] value = ingredient.getSelectedValues();
if(e.getSource() == move){
for(int i=0;i< value.length;i++){
String word = (String)value[i];
Vector<Object> data = new Vector<Object>();
data.addElement( word );
data.addElement( box.getSelectedItem() );
data.addElement( box1.getSelectedItem());

model.addRow( data );
}
}
if(e.getSource() == finish){
JOptionPane.showMessageDialog( BreakFast.this,table,"Selection Summary",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}

public static void main(String[] arg){
new BreakFast();
}
}
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some observations:

1.how do i display the whole table in my code using a dialog box or JOptionPane.
As you do in the finish button event code block? You can make up a new JTable using the same TableModel as the JTable in your JFrame. Or you could make up a new JTable and copy the values of the original table/model into it.

2. i want to display a message beneath the table show the total price of items selected from the JComboBox .
Are you referring to the JTable in the JFrame or the JTable in the dialog? I will assume you mean the JFrame table.

3.is there any way i could add a method or sommething that display detail of each item seleted from the list in a seperate column of the JTable in my code.say for instance,if i selects beans,1 cup, 100 and it display protein in a seperate column in the JTable.
Yes, I think so. Again, are you referring to the original JTable? And will all item values of this extra column be protein values? If so the column header could be labeled "Protein." You can have as many columns as you like in your JTable. Or you could have an extra column for miscellaneous notes; maybe protein for beans and calories for sugar. You could add a note for some/all items. In either case you will need a way to store this extra data and correlate/keep it in synch with your food items.
Here's a way (one of many) you could implement some of these things.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic