This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes A confused new user of Java that needs some help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "A confused new user of Java that needs some help" Watch "A confused new user of Java that needs some help" New topic
Author

A confused new user of Java that needs some help

John Charney
Greenhorn

Joined: Jul 23, 2001
Posts: 3
Hi there I am a fairly new person to the Java language ( I am currently enrolled in a Java programming class this summer at college) anyways I have to create a program that uses a JTabbedPane, providing one tab to input user data such as a name and thier address and another tab that inputs information about a pizza order. My teacher also wants us to be a little bit creative and once the user enters the information and it is visible on the screen we should be able to change the color of that information or make it bold or such. Her si the code that I have been able to come up with so far and when I compile it I have no errors the problem that I am haveing is how do I collect the information from the user and output is so that it is viewable. I am totally lost as to how to do that thanks in advance for any help.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Tabs extends JFrame {
public Tabs(String title) {
super(title);
JTabbedPane tabPane = new JTabbedPane();
Container c = getContentPane();
c.add(tabPane, "Center");
SelectName name = new SelectName();
tabPane.addTab("Name",name);
SelectOrder order = new SelectOrder();
tabPane.addTab("Order",order);
tabPane.setSelectedIndex(0);
addWindowListener(new CloseWindow());
}
public static void main(String [] args) {
Tabs t = new Tabs("Tab display");
t.pack();
t.show();
}
class CloseWindow extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
class SelectName extends JPanel {
private DrawOn canvas = new DrawOn();
private JComboBox color = new JComboBox();
private Color [] theColor = {Color.red,Color.green,Color.blue};
private String [] colorName = {"Red","Green","Blue"};

public SelectName() {
setLayout(new FlowLayout());
add(color);
add(canvas);
for (int i=0; i<colorName.length; i++) >
color.addItem(colorName);
canvas.setPreferredSize(new Dimension(150,150));
color.addItemListener(canvas);
}
class DrawOn extends JPanel implements ItemListener {
public void itemStateChanged(ItemEvent event) {
Object source = event.getItem();
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(theColor[color.getSelectedIndex()]);
}
}
}
class SelectOrder extends JPanel {
private DrawOn canvas = new DrawOn();
private String [] colorName = {"Black","Blue","Cyan","Dark Gray","Gray",
"Green","Light Gray","Magenta","Orange","Pink","Red","White","Yellow"};
private JList names = new JList(colorName);
private JScrollPane color = new JScrollPane(names);
private JCheckBox italic = new JCheckBox("Italic");
private JCheckBox bold = new JCheckBox("Bold");
private Color [] theColor = {Color.black,Color.blue,Color.cyan,Color.darkGray,
Color.gray,Color.green,Color.lightGray,Color.magenta,Color.orange,
Color.pink,Color.red,Color.white,Color.yellow};
private String message = "Hi there";
public SelectOrder() {
setLayout(new FlowLayout());
add(color);
add(italic);
add(bold);
add(canvas);
names.setSelectedIndex(0);
names.addListSelectionListener(canvas);
canvas.setPreferredSize(new Dimension(150,150));
italic.addItemListener(canvas);
bold.addItemListener(canvas);
}
class DrawOn extends JPanel implements
ItemListener, ListSelectionListener {
int style = Font.PLAIN;
public void itemStateChanged(ItemEvent event) {
Object source = event.getItem();
int change = event.getStateChange();
if (source == italic)
if (change == ItemEvent.SELECTED)
style += Font.ITALIC;
else
style -= Font.ITALIC;
if (source == bold)
if (change == ItemEvent.SELECTED)
style += Font.BOLD;
else
style -= Font.BOLD;
repaint();
}
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false)
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(new Font("Serif",style,24));
g.setColor(theColor[names.getSelectedIndex()]);
g.drawString(message, 50,50);
}
}
}
}
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
John this is not an appropriate post for the "Cattle Drive" forum, so I've moved it to "Swing / JFC / AWT"
Good luck with your question & Java class
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well John, I have to say that I am surprised that you are already creating GUI's in your JAVA class. Must be an advanced class.
Now, are you wanting to display the resualts of the Pizza Order or the resualts of the color and font schemes they have chosen to change?
If it is the pizza order you want to display, it depends on how you would like it displayed. The way I did something similar is on the last Panel of the JTabbedPane I had a Verify button that displayed a JFrame with the users choices summarized and then a final Submit button as well as a cancel or back button so the user could then edit what they have chosen.
Another way is to add an extra panel and dynamically update the contents to your choices entered on the other panels.
Either way it is really quite simple. But there are different methods for gathering different kinds of data. For instance, your textFields with the name, address, etc, would just require a getText() method upon some event. Checkboxes/radiobuttons are different in the fact that they immedialty send the data somewhere so a temp variable is nice to have.
Anyhow, that's how I did it. I'm sure there are other ways. If you want to talk a little more in depth you can E-mail me at
Gregg.Bolinger@intrustbank.com
Sorry for the long message.
Have fun


GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A confused new user of Java that needs some help