0

I am doing several choice box in JFrame.But i have some problems.Can anyone help me.
1)Currently doing a loop to add no1 until 50 into choice box.But it doesn't work.

int [] number = new int[50];
for(int i=0;i<50;i++)
{
   number[i] = i;

for(int i = 0;i<50;i++)
{
   c1.add(number[i]);
}

2)I am doing several choice box, but then all in one row.How can i make it row by row?

4
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by mKorbel
Featured Replies
  • 1

    [QUOTE]But it doesn't work.[/QUOTE] Please explain what happens. [QUOTE]How can i make it row by row?[/QUOTE] Use a layout manager that will do that. Perhaps the GridLayout Read More

1

But it doesn't work.

Please explain what happens.

How can i make it row by row?

Use a layout manager that will do that. Perhaps the GridLayout

Comments
easiest is JTable, Gridlayout +1
0

What is a "choice box"?
Do you mean a JComboBox?
If so, try...

int Choice_Count = 10;
String Choices [] = new String [Choice_Count];
for (int I = 0; I < Choice_Count; I++)
   Choices [I] = Integer.toString (I + 1);  // This will give me 1-10 instead of 0-9
JComboBox Choice_ComboBox = new JComboBox (Choices);
0

@hfx642

Choise is pre-dinosaurus form of JComboBox that comning from Java AWT and silently died in the last centaury

0

What is a "choice box"?
Do you mean a JComboBox?
If so, try...

int Choice_Count = 10;
String Choices [] = new String [Choice_Count];
for (int I = 0; I < Choice_Count; I++)
   Choices [I] = Integer.toString (I + 1);  // This will give me 1-10 instead of 0-9
JComboBox Choice_ComboBox = new JComboBox (Choices);

Thanks for your reply:)this is wat i want.

0

Please explain what happens.


Use a layout manager that will do that. Perhaps the GridLayout

Thanks, Layout manager work:)

0

Thanks, Layout manager work:)

import java.awt.event.*;
import java.awt.*; //for graphics
import java.util.*;
import java.text.*; // for decimal
import java.applet.*;
import javax.swing.*;

public class bill extends JFrame
{
	public bill()
	{
		super("Receipt");
		Container a = getContentPane();
		a.setLayout(new GridLayout(5,1,10,5));
		
		Label L1 = new Label("Receipt Restaurant ABC");
		Font f1 = new Font("TimesNewRoman",Font.BOLD,40);
		L1.setFont(f1);
		L1.setForeground(Color.blue);
		L1.setAlignment(Label.CENTER);
		a.add(L1,"North");

		Label L4 = new Label();
		Font f3 = new Font("ARIEL",Font.BOLD,13);
		L4.setFont(f3);
		L4.setForeground(Color.black); 
		L4.setText("Date: " + new Date());
		a.add(L4);

		Panel p = new Panel(new GridLayout(3,2,10,5));
		//Panel p2 = new Panel(new GridLayout(1,1));
		p.setBackground(Color.pink);
		//p2.setBackground(Color.yellow);
		a.add(p,"Center");
		
		Label L5 = new Label("Reciept No:");
		L5.setFont(f3);
		L5.setForeground(Color.black);
		p.add(L5);
		
		Label L6 = new Label();
		L6.setFont(f3);
		L6.setForeground(Color.black);
		L6.setText("1123");
		p.add(L6);
		
		Label L2 = new Label("Table No:");
		Font f2 = new Font("ARIEL",Font.BOLD,13);
		L2.setFont(f2);
		L2.setForeground(Color.black);
		p.add(L2);

		int Choice_Count = 50;
		String Choices [] = new String [Choice_Count];
		for (int I = 0; I < Choice_Count; I++)
		Choices [I] = Integer.toString (I + 1); 
		JComboBox Choice_ComboBox = new JComboBox (Choices);
		p.add(Choice_ComboBox);
	
		System.out.println();
		Label L3 = new Label("Staff In Charged:");
		L3.setFont(f2);
		L3.setForeground(Color.black);
		p.add(L3);
		//p2.add(L3);
		//a.add(p2);
		
		String name[] = {"Amy","Wendy","Angel"};
		JComboBox Choice_ComboBox2 = new JComboBox (name);
		p.add(Choice_ComboBox2);
		
		String[] columnNames = {"No", "Items"};
		Object[][] cellData = {{"1)", "row1-col2"},{"2)", "row2-col2"}};
		JTable table = new JTable(cellData, columnNames);
		a.add(table);

		TableColumn column = null;   //Error here: Cannot find the symbol
		for (int i = 0; i <= 1; i++) 
		{
		column = table.getColumnModel().getColumn(i);
		if (i == 1) 
		{
			column.setPreferredWidth(100); //third column is bigger
		} 
		else 
		{
			column.setPreferredWidth(50);
		}
		}
		/*table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		int vColIndex = 0;
		TableColumn col = table.getColumnModel().getColumn(vColIndex);
		int width = 100;
		col.setPreferredWidth(width);*/
		
		setSize(500,500);
		show();	
	}
	
	
	
	public static void main(String a[])
	{
		bill application = new bill();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	};
}

When the JFrame window open, the table do appears, but then i cant see the column name, can anyone explain to me why?

I have error in this line,TableColumn column = null;
Can i know why?

Edited by yt0729: n/a

0

don't mix Java AWT Components with Swing JCompoents, since that were official allowed here and here, but I still to think that that would be just the road to the hell

Label would be JLabel

Panel would be JPanel

Edited by mKorbel: n/a

0

you have to learn how to LayoutManager works, good idea with correct direction, just check that

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.TableColumn;

public class Bill extends JFrame {

    private static final long serialVersionUID = 1L;

    public Bill() {
        super("Receipt");

        JLabel L1 = new JLabel("Receipt Restaurant ABC");
        Font f1 = new Font("TimesNewRoman", Font.BOLD, 40);
        L1.setFont(f1);
        L1.setForeground(Color.blue);
        L1.setHorizontalAlignment(JLabel.CENTER);

        JLabel L4 = new JLabel();
        Font f3 = new Font("ARIEL", Font.BOLD, 13);
        L4.setFont(f3);
        L4.setForeground(Color.black);
        L4.setText("Date: " + new Date());

        JPanel p = new JPanel(new GridLayout(3, 2, 10, 5));
        //JPanel p2 = new JPanel(new GridLayout(1,1));
        p.setBackground(Color.pink);
        //p2.setBackground(Color.yellow);

        JLabel L5 = new JLabel("Reciept No:");
        L5.setFont(f3);
        L5.setForeground(Color.black);
        p.add(L5);

        JLabel L6 = new JLabel();
        L6.setFont(f3);
        L6.setForeground(Color.black);
        L6.setText("1123");
        p.add(L6);

        JLabel L2 = new JLabel("Table No:");
        Font f2 = new Font("ARIEL", Font.BOLD, 13);
        L2.setFont(f2);
        L2.setForeground(Color.black);
        p.add(L2);

        int Choice_Count = 50;
        String Choices[] = new String[Choice_Count];
        for (int I = 0; I < Choice_Count; I++) {
            Choices[I] = Integer.toString(I + 1);
        }
        JComboBox Choice_ComboBox = new JComboBox(Choices);
        p.add(Choice_ComboBox);

        System.out.println();
        JLabel L3 = new JLabel("Staff In Charged:");
        L3.setFont(f2);
        L3.setForeground(Color.black);
        p.add(L3);
        //p2.add(L3);
        //a.add(p2);

        String name[] = {"Amy", "Wendy", "Angel"};
        JComboBox Choice_ComboBox2 = new JComboBox(name);
        p.add(Choice_ComboBox2);

        String[] columnNames = {"No", "Items"};
        Object[][] cellData = {{"1)", "row1-col2"}, {"2)", "row2-col2"}};
        JTable table = new JTable(cellData, columnNames);

        TableColumn column; 
        for (int i = 0; i <= 1; i++) {
            column = table.getColumnModel().getColumn(i);
            if (i == 1) {
                column.setPreferredWidth(100); //third column is bigger
            } else {
                column.setPreferredWidth(50);
            }
        }
        
        setLayout(new GridLayout(5, 1, 10, 5));
        add(L1, "North");
        add(L4);
        add(p, "Center");
        add(table);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }

    public static void main(String a[]) {
        
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Bill application = new Bill();;
            }
        });
    }
}
0

you have to learn how to LayoutManager works, good idea with correct direction, just check that

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.TableColumn;

public class Bill extends JFrame {

    private static final long serialVersionUID = 1L;

    public Bill() {
        super("Receipt");

        JLabel L1 = new JLabel("Receipt Restaurant ABC");
        Font f1 = new Font("TimesNewRoman", Font.BOLD, 40);
        L1.setFont(f1);
        L1.setForeground(Color.blue);
        L1.setHorizontalAlignment(JLabel.CENTER);

        JLabel L4 = new JLabel();
        Font f3 = new Font("ARIEL", Font.BOLD, 13);
        L4.setFont(f3);
        L4.setForeground(Color.black);
        L4.setText("Date: " + new Date());

        JPanel p = new JPanel(new GridLayout(3, 2, 10, 5));
        //JPanel p2 = new JPanel(new GridLayout(1,1));
        p.setBackground(Color.pink);
        //p2.setBackground(Color.yellow);

        JLabel L5 = new JLabel("Reciept No:");
        L5.setFont(f3);
        L5.setForeground(Color.black);
        p.add(L5);

        JLabel L6 = new JLabel();
        L6.setFont(f3);
        L6.setForeground(Color.black);
        L6.setText("1123");
        p.add(L6);

        JLabel L2 = new JLabel("Table No:");
        Font f2 = new Font("ARIEL", Font.BOLD, 13);
        L2.setFont(f2);
        L2.setForeground(Color.black);
        p.add(L2);

        int Choice_Count = 50;
        String Choices[] = new String[Choice_Count];
        for (int I = 0; I < Choice_Count; I++) {
            Choices[I] = Integer.toString(I + 1);
        }
        JComboBox Choice_ComboBox = new JComboBox(Choices);
        p.add(Choice_ComboBox);

        System.out.println();
        JLabel L3 = new JLabel("Staff In Charged:");
        L3.setFont(f2);
        L3.setForeground(Color.black);
        p.add(L3);
        //p2.add(L3);
        //a.add(p2);

        String name[] = {"Amy", "Wendy", "Angel"};
        JComboBox Choice_ComboBox2 = new JComboBox(name);
        p.add(Choice_ComboBox2);

        String[] columnNames = {"No", "Items"};
        Object[][] cellData = {{"1)", "row1-col2"}, {"2)", "row2-col2"}};
        JTable table = new JTable(cellData, columnNames);

        TableColumn column; 
        for (int i = 0; i <= 1; i++) {
            column = table.getColumnModel().getColumn(i);
            if (i == 1) {
                column.setPreferredWidth(100); //third column is bigger
            } else {
                column.setPreferredWidth(50);
            }
        }
        
        setLayout(new GridLayout(5, 1, 10, 5));
        add(L1, "North");
        add(L4);
        add(p, "Center");
        add(table);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }

    public static void main(String a[]) {
        
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Bill application = new Bill();;
            }
        });
    }
}

I appreciate your reply:) but cant run your code, error said filename error.

0

no idea about your side effect File Error,

code that you post and my minor changes are pretty runnable and compilable

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.