duta
asked on
Java Swing: Custom-Size ComboBox?
Dear experts:
Hi again!
I have a simple code which include a combobox. The problem is that the combobox is displayed streched all the way from the left end of the window to the right end of the window. I would like the combobox displayed in a certain width (example: 50) and height (example: 10) at a location 100 px from the left end of the window.
I will truly appreciate if you experts may kindly teach me how to do that.
Thanks a lot!
duta
________________ Here is my latest code _________________
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.util.regex.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class PhoneBill3 extends JApplet
{
// private JList planList;
private JComboBox planList = new JComboBox(new Object[] { "Unlimited Plan", "300 Anytime Minutes" });
private JPanel Pname;
private JPanel Pusage;
private JPanel Ptotal;
private JPanel Pbutton;
/////////////////
private JRadioButton unlimited, limited;
private ButtonGroup radioGroup;
///////////////
private JTextField Fname;
private JTextField Fusage;
private JTextField Ftotal;
private Container c = getContentPane ( );
// private String plans [] = {"Unlimited Plan", "Limited Plan"};
JComboBox plans = new JComboBox(new Object[] { "Unlimited Plan", "300 Anytime Minutes" });
public void init ( )
{
// Methods to build the panels
buildPlan ();
buildName ();
buildUsage ();
buildTotal ();
buildButton ();
// Create a layout manager
setLayout ( new GridLayout (5, 1 ) );
// Add the panels to the content pane
add ( Pname);
add ( planList );
add ( Pusage);
add ( Ptotal );
add ( Pbutton );
}
/////////// ?????????????????????????? ?????????? ?????????? ?????????? ?
private void buildPlan ()
{
// select the first button by default
plans.setSelectedIndex (0);
}
///// ?????????????????????????? ?????????? ?????????? ?????????? ?????????
private void buildName ()
{
Pname = new JPanel ( );
JLabel message1 = new JLabel ("Your Name : " );
Fname = new JTextField ( 20 );
Pname.setLayout ( new FlowLayout ( FlowLayout.RIGHT ) );
Pname.add (message1);
Pname.add ( Fname );
}
private void buildUsage ()
{
Pusage = new JPanel ( );
JLabel message2 = new JLabel ("Your Anytime Usage : " );
Fusage = new JTextField ( 20 );
Pusage.setLayout ( new FlowLayout ( FlowLayout.RIGHT ) );
Pusage.add (message2);
Pusage.add ( Fusage );
}
private void buildTotal ()
{
Ptotal = new JPanel ( );
JLabel message3 = new JLabel ("Your Total Charge: " );
Ftotal = new JTextField ( 20 );
Ftotal.setEditable ( false );
Ptotal.setLayout ( new FlowLayout ( FlowLayout.RIGHT ) );
Ptotal.add (message3);
Ptotal.add ( Ftotal );
}
private void buildButton ()
{
Pbutton = new JPanel ( );
JButton calcButton = new JButton ( "Calculate");
calcButton.addActionListen er ( new ButtonListener ( ) );
Pbutton.add ( calcButton );
}
// Private inner class that andles the action event that is generated
// when the user clicks "Calculate" button.
private class ButtonListener implements ActionListener
{
public void actionPerformed ( ActionEvent e )
{
String name = " ";
int mins_used = 0;
int plan = 0;
float total = 0.0f;
name = Fname.getText ();
try
{
mins_used = Integer.parseInt (Fusage.getText () );
plan = planList.getSelectedIndex ();
/// ?????????????????????????? ?????????? ?????????? ?????????? ?????????? ????
if (plan==1 )
{
if (mins_used <= 300 )
{
total = 39.99f;
Ftotal.setText (""+total);
}
else
{
total = 39.99f + ((mins_used - 300) * 0.45f);
Ftotal.setText (""+total);
}
}
else
{
total = 69.99f;
Ftotal.setText (""+total);
}
///// ?????????????????????????? ?????????? ?????????? ?????????? ?????????? ??????????
}
catch(NumberFormatExceptio n nfe)
{
Ftotal.setText ("Please enter an integer by usage.");
}
}
}
}
Hi again!
I have a simple code which include a combobox. The problem is that the combobox is displayed streched all the way from the left end of the window to the right end of the window. I would like the combobox displayed in a certain width (example: 50) and height (example: 10) at a location 100 px from the left end of the window.
I will truly appreciate if you experts may kindly teach me how to do that.
Thanks a lot!
duta
________________ Here is my latest code _________________
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.util.regex.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class PhoneBill3 extends JApplet
{
// private JList planList;
private JComboBox planList = new JComboBox(new Object[] { "Unlimited Plan", "300 Anytime Minutes" });
private JPanel Pname;
private JPanel Pusage;
private JPanel Ptotal;
private JPanel Pbutton;
/////////////////
private JRadioButton unlimited, limited;
private ButtonGroup radioGroup;
///////////////
private JTextField Fname;
private JTextField Fusage;
private JTextField Ftotal;
private Container c = getContentPane ( );
// private String plans [] = {"Unlimited Plan", "Limited Plan"};
JComboBox plans = new JComboBox(new Object[] { "Unlimited Plan", "300 Anytime Minutes" });
public void init ( )
{
// Methods to build the panels
buildPlan ();
buildName ();
buildUsage ();
buildTotal ();
buildButton ();
// Create a layout manager
setLayout ( new GridLayout (5, 1 ) );
// Add the panels to the content pane
add ( Pname);
add ( planList );
add ( Pusage);
add ( Ptotal );
add ( Pbutton );
}
/////////// ??????????????????????????
private void buildPlan ()
{
// select the first button by default
plans.setSelectedIndex (0);
}
///// ??????????????????????????
private void buildName ()
{
Pname = new JPanel ( );
JLabel message1 = new JLabel ("Your Name : " );
Fname = new JTextField ( 20 );
Pname.setLayout ( new FlowLayout ( FlowLayout.RIGHT ) );
Pname.add (message1);
Pname.add ( Fname );
}
private void buildUsage ()
{
Pusage = new JPanel ( );
JLabel message2 = new JLabel ("Your Anytime Usage : " );
Fusage = new JTextField ( 20 );
Pusage.setLayout ( new FlowLayout ( FlowLayout.RIGHT ) );
Pusage.add (message2);
Pusage.add ( Fusage );
}
private void buildTotal ()
{
Ptotal = new JPanel ( );
JLabel message3 = new JLabel ("Your Total Charge: " );
Ftotal = new JTextField ( 20 );
Ftotal.setEditable ( false );
Ptotal.setLayout ( new FlowLayout ( FlowLayout.RIGHT ) );
Ptotal.add (message3);
Ptotal.add ( Ftotal );
}
private void buildButton ()
{
Pbutton = new JPanel ( );
JButton calcButton = new JButton ( "Calculate");
calcButton.addActionListen
Pbutton.add ( calcButton );
}
// Private inner class that andles the action event that is generated
// when the user clicks "Calculate" button.
private class ButtonListener implements ActionListener
{
public void actionPerformed ( ActionEvent e )
{
String name = " ";
int mins_used = 0;
int plan = 0;
float total = 0.0f;
name = Fname.getText ();
try
{
mins_used = Integer.parseInt (Fusage.getText () );
plan = planList.getSelectedIndex ();
/// ??????????????????????????
if (plan==1 )
{
if (mins_used <= 300 )
{
total = 39.99f;
Ftotal.setText (""+total);
}
else
{
total = 39.99f + ((mins_used - 300) * 0.45f);
Ftotal.setText (""+total);
}
}
else
{
total = 69.99f;
Ftotal.setText (""+total);
}
///// ??????????????????????????
}
catch(NumberFormatExceptio
{
Ftotal.setText ("Please enter an integer by usage.");
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dear Mark:
Hi again!
I just wonder whether I may try your code as an applet? I tried but, but nothing showed up.
Thanks again!
Hi again!
I just wonder whether I may try your code as an applet? I tried but, but nothing showed up.
Thanks again!
I am sorry, I always use JFrame :-)
Anyway.. not much has to be changed.
-- Applet version
-- LayoutExample.java
import java.awt.*;
import javax.swing.*;
public class LayoutExample extends JApplet
{
public void init()
{
Container content = getContentPane();
content.setLayout(new GridLayout(2,2));
content.add(this.makeTopLe ftPanel()) ;
content.add(this.makeTopRi ghtPanel() );
content.add(this.makeBotto mLeftPanel ());
content.add(this.makeBotto mRightPane l());
}
public JPanel makeTopLeftPanel()
{
JPanel pnl = new JPanel();
pnl.setLayout(new FlowLayout()); //default, line can be removed
pnl.add(new JTextField("Label A"));
pnl.add(new JTextField("Label B"));
pnl.add(new JTextField("Label C"));
pnl.add(new JTextField("Label D"));
return pnl;
}
public JPanel makeTopRightPanel()
{
JPanel pnl = new JPanel();
pnl.setLayout(new GridLayout(2,2));
pnl.add(new JTextField("Label A"));
pnl.add(new JTextField("Label B"));
pnl.add(new JTextField("Label C"));
pnl.add(new JTextField("Label D"));
return pnl;
}
public JPanel makeBottomLeftPanel()
{
JPanel pnl = new JPanel();
pnl.setLayout(new BorderLayout());
pnl.add(new JTextField("Label A"), BorderLayout.NORTH);
pnl.add(new JTextField("Label B"), BorderLayout.WEST);
pnl.add(new JTextField("Label C"), BorderLayout.EAST);
pnl.add(new JTextField("Label D"), BorderLayout.CENTER);
return pnl;
}
public JPanel makeBottomRightPanel()
{
JTextField lbl;
JPanel pnl = new JPanel();
pnl.setLayout(null);
lbl = new JTextField("Label A");
lbl.setBounds(10, 10, 50, 20);
pnl.add(lbl);
lbl = new JTextField("Label B");
lbl.setBounds(100, 110, 10, 50);
pnl.add(lbl);
lbl = new JTextField("Label C");
lbl.setBounds(200, 30, 22, 20);
pnl.add(lbl);
lbl = new JTextField("Label D");
lbl.setBounds(50, 240, 50, 50);
pnl.add(lbl);
return pnl;
}
}
HTML page should contain:
<applet code="LayoutExample" width="800" height="600"></applet>
Have fun.
Mark
Anyway.. not much has to be changed.
-- Applet version
-- LayoutExample.java
import java.awt.*;
import javax.swing.*;
public class LayoutExample extends JApplet
{
public void init()
{
Container content = getContentPane();
content.setLayout(new GridLayout(2,2));
content.add(this.makeTopLe
content.add(this.makeTopRi
content.add(this.makeBotto
content.add(this.makeBotto
}
public JPanel makeTopLeftPanel()
{
JPanel pnl = new JPanel();
pnl.setLayout(new FlowLayout()); //default, line can be removed
pnl.add(new JTextField("Label A"));
pnl.add(new JTextField("Label B"));
pnl.add(new JTextField("Label C"));
pnl.add(new JTextField("Label D"));
return pnl;
}
public JPanel makeTopRightPanel()
{
JPanel pnl = new JPanel();
pnl.setLayout(new GridLayout(2,2));
pnl.add(new JTextField("Label A"));
pnl.add(new JTextField("Label B"));
pnl.add(new JTextField("Label C"));
pnl.add(new JTextField("Label D"));
return pnl;
}
public JPanel makeBottomLeftPanel()
{
JPanel pnl = new JPanel();
pnl.setLayout(new BorderLayout());
pnl.add(new JTextField("Label A"), BorderLayout.NORTH);
pnl.add(new JTextField("Label B"), BorderLayout.WEST);
pnl.add(new JTextField("Label C"), BorderLayout.EAST);
pnl.add(new JTextField("Label D"), BorderLayout.CENTER);
return pnl;
}
public JPanel makeBottomRightPanel()
{
JTextField lbl;
JPanel pnl = new JPanel();
pnl.setLayout(null);
lbl = new JTextField("Label A");
lbl.setBounds(10, 10, 50, 20);
pnl.add(lbl);
lbl = new JTextField("Label B");
lbl.setBounds(100, 110, 10, 50);
pnl.add(lbl);
lbl = new JTextField("Label C");
lbl.setBounds(200, 30, 22, 20);
pnl.add(lbl);
lbl = new JTextField("Label D");
lbl.setBounds(50, 240, 50, 50);
pnl.add(lbl);
return pnl;
}
}
HTML page should contain:
<applet code="LayoutExample" width="800" height="600"></applet>
Have fun.
Mark
ASKER
Mark, you are great. It is working great. I am going to play with your code to learn a lot.
Thanks again!
I am going to accept your tip as acceptable solution.
My salute to you!
Thanks again!
I am going to accept your tip as acceptable solution.
My salute to you!
ASKER
I will take time to carefully every line of your tip and try them with my code.
Thanks again!
duta