Results 1 to 2 of 2
  1. #1
    mad_indian1 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default I am trying to use the enter button from the jscroll so that it can link to the jfram

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.Dimension;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.ButtonGroup;
    import javax.swing.JRadioButton;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.JComboBox;
    import javax.swing.*;
    import java.util.Vector;
    public class TextForm1 extends JPanel{

    public static JPanel createContentPane (){
    // As usual, we create our bottom-level panel.
    JPanel totalGUI = new JPanel();
    // This is the story we took from Wikipedia.
    String story = "**** Welcome to GOLDSMITHS Military Base ****\n\n"+
    "The following program will assess your trainee modules: \n\n"+
    " 1.SAM Detection \n\n 2.IR Beacon Maintenance\n\n 3.Forward Scanning Radar Display Repair"+
    " \n\n"
    +"*****Thank you for using Military Services******\n\n"+
    "**************Military Services******************\n\n.";
    // We create the TextArea and pass the story in as an argument.
    // We also set it to be non-editable, and the line and word wraps set to true.
    JTextArea storyArea = new JTextArea(story);
    storyArea.setEditable(false);
    storyArea.setLineWrap(true);
    storyArea.setWrapStyleWord(true);
    // We create the ScrollPane and instantiate it with the TextArea as an argument
    // along with two constants that define the behaviour of the scrollbars.
    JScrollPane area = new JScrollPane(storyArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    // We then set the preferred size of the scrollpane.
    area.setPreferredSize(new Dimension(300, 200));
    totalGUI.add(area);
    totalGUI.setOpaque(true);
    return totalGUI;
    }
    private static void createAndShowGUI(){
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame1=new JFrame("Welcome Trainees!!!!");
    ScrollPaneExample demo = new ScrollPaneExample();
    frame1.setContentPane(demo.createContentPane());
    JButton enter = new JButton("ENTER");
    enter.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    String[] labels = { "First Name", "Last Name", "Trainee ID"," How many Quizzes ?","Final Module Mark"};
    int[] widths = { 15, 15, 15,5,5,5,5};
    String[] descs = { "First Name","Last Name", "Trainee ID", "How many quizzes to calculate?",
    "Final Module Mark:"};
    TextForm1 form1 = new TextForm1(labels,widths, descs);
    form1.setVisible(true);
    }
    });
    frame1.add(enter);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    frame1.setSize(350, 300);
    frame1.setVisible(true);
    }
    private JTextField[] fields;
    // Create a form with the specified labels, tooltips, and sizes.
    public TextForm1(String[] labels,int[] widths, String[]tips) {
    super(new BorderLayout());

    JPanel labelPanel = new JPanel(new GridLayout(labels.length, 3));
    JPanel fieldPanel = new JPanel(new GridLayout(labels.length, 2));
    add(labelPanel, BorderLayout.WEST);
    add(fieldPanel, BorderLayout.CENTER);
    fields = new JTextField[labels.length];

    for (int i = 0; i< labels.length; i += 1) {
    fields[i] = new JTextField();
    if (i < tips.length)
    fields[i].setToolTipText(tips[i]);
    if (i < widths.length)
    fields[i].setColumns(widths[i]);
    JLabel lab = new JLabel(labels[i], JLabel.RIGHT);
    lab.setLabelFor(fields[i]);
    labelPanel.add(lab);
    JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
    p.add(fields[i]);
    fieldPanel.add(p);
    }
    }
    public String getText(int i) {
    return (fields[i].getText());
    }
    public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run(){
    createAndShowGUI();
    }
    /*public void run1() {
    createAndShowGUI();
    }*/
    });

    String[] labels = { "First Name", "Last Name", "Trainee ID"," How many Quizzes ?","Final Module Mark"};
    int[] widths = { 15, 15, 15,5,5,5,5};
    String[] descs = { "First Name","Last Name", "Trainee ID", "How many quizzes to calculate?",
    "Final Module Mark:"};
    final TextForm1 form = new TextForm1(labels, widths, descs);

    JFrame.setDefaultLookAndFeelDecorated(true);
    JRadioButton mod1 = new JRadioButton("Forward Scanning Radar Display Repair");
    JRadioButton mod2 = new JRadioButton("SAM Detection System Replacement");
    JRadioButton mod3 = new JRadioButton("IR Beacon Maintenance");

    ButtonGroup moduleButtonGroup = new ButtonGroup();
    moduleButtonGroup.add(mod1);
    moduleButtonGroup.add(mod2);
    moduleButtonGroup.add(mod3);
    mod1.setSelected(false);


    JButton submit = new JButton("Submit");
    submit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    System.out.println();
    System.out.println("First Name: " +form.getText(0));
    System.out.println();
    System.out.println("Last Name: " +form.getText(1));
    System.out.println();
    System.out.println("Trainee ID Number: " +form.getText(2));
    System.out.println();
    System.out.println("Quiz Mark: " +form.getText(3));
    System.out.println();
    }
    });
    ActionListener sliceActionListener = new ActionListener(){
    public void actionPerformed(ActionEvent actionEvent) {
    AbstractButton mod1 = (AbstractButton) actionEvent.getSource();
    System.out.println("Module selected: " + mod1.getText());
    }
    };

    JFrame f = new JFrame("Military Base");
    f.getContentPane().add(form, BorderLayout.NORTH);
    JPanel mod = new JPanel();
    mod.add(new JLabel(" Trainee Module:"));
    mod.add(mod1);
    mod.add(mod2);
    mod.add(mod3);

    f.getContentPane().add(mod, BorderLayout.WEST);
    f.pack();
    f.setVisible(true);

    JPanel p = new JPanel();
    p.add(submit);
    mod1.addActionListener(sliceActionListener);
    mod2.addActionListener(sliceActionListener);
    mod3.addActionListener(sliceActionListener);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(p, BorderLayout.SOUTH);
    f.pack();
    f.setVisible(true);
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    1. Use code tags to post codes -- [code]CODE[/code] will display as
    Java Code:
    CODE
    Or paste your code between a pair of {code} tags.

    2. Go through How to ask questions the smart way and learn to ask a question that can actually be answered.

    db

Similar Threads

  1. How to create Java Button Link
    By Louis in forum New To Java
    Replies: 3
    Last Post: 11-17-2010, 07:29 AM
  2. how to make Broswe button in jfram form to attache file
    By ibrahimyoussof in forum AWT / Swing
    Replies: 3
    Last Post: 04-12-2010, 08:40 PM
  3. Extend Jscroll
    By cowboy in forum New To Java
    Replies: 10
    Last Post: 12-13-2009, 08:07 PM
  4. Replies: 2
    Last Post: 04-12-2009, 10:39 AM
  5. Javascript and enter button
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-11-2007, 09:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •