Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JDialog not working properly

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JDialog not working properly

    I am a newbie at Java and am in the process of writing my first project, a multiple-file one, that processes information on race horses. It is so far working well, but the JDialog window that pops up by clicking the enter-horse-names button on the frame freezes and will not take new input for a new race. I am posting the code for the main class ( AlladinLamp ) along with some comments identifying some of the data. I suspect it is a theading issue, or just poor coding, but I don�t seem to be able to locate the origin of the problem. Any help would be greatly appreciated.

    package alladinlamp;



    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.Border;




    public class AlladinLamp {

    @SuppressWarnings("empty-statement")
    GridLayout gridLayout;
    JComboBox runners, raceDiv, days, months;
    JButton horseList, ok, clear;
    JLabel div, hNumLabel, rider;
    JTextField[] jtxt; // textfields appearing in the dialog to hold horse names
    JTabbedPane jtb;
    JTextField raceName;
    JTextArea message;
    static JFrame jfr;
    String status;
    JDialog dialog;
    JPanel table, data, jpDialog, middleBox, buttonPanel;
    String[] hNam;
    int[] hNum, hVal; // number of the horses and numerical values of the horses
    int reducedFNH, id, vp, day, month, fnh, dv; // fnh is the number of horses running
    GridBagLayout gbLayout;
    GridBagConstraints gbc;
    Dimension jfrSize, dialogSize;
    ImageIcon dataIcon, tableIcon, icon;
    Processes proc;
    JMenu jmFile, jmTasks;
    JMenuItem jmiNew, jmiOpen, jmiSave, jmiSaveAs,
    jmiClose, jmiDraw, jmiPrint, jmiProcess;
    JMenuBar jmBar;


    public String[] getHNam() { return hNam; }

    public int getID() { return id; }

    public int getFNH() { return fnh; }

    public int[] getHNum() { return hNum; }

    public int[] getHVal() { return hVal; }



    [ code... ]



    //----------------------------------------------------------------------------

    public AlladinLamp() {

    jfr = new JFrame();
    this.proc = new Processes();
    this.status = "";
    this.dialog = new JDialog( jfr, "List of Horse Names", false );
    this.message = new JTextArea( 10, 10 );
    this.gbLayout = new GridBagLayout();
    this.gbc = new GridBagConstraints();
    this.jpDialog = new JPanel();
    this.table = new JPanel();
    this.data = new JPanel() ;
    this.middleBox = new JPanel();

    // Main window

    jfr.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    jfr.setTitle("My Alladin Lamp");
    jfr.setSize( 355, 740 );

    //Window menus
    jmBar = new JMenuBar();
    jfr.add( jmBar, BorderLayout.NORTH );
    jfr.setJMenuBar( jmBar );

    jmFile = new JMenu( "File" );
    jmFile.setMnemonic( KeyEvent.VK_F );
    jmiNew = new JMenuItem ( "New" );
    jmiOpen = new JMenuItem ( "Open" );
    jmiSave = new JMenuItem ( "Save" );
    jmiSaveAs = new JMenuItem ( "Save As" );
    jmiClose = new JMenuItem ( "Close" );

    jmFile.add( jmiNew );
    jmFile.add( jmiOpen);
    jmFile.add( jmiSave );
    jmFile.add( jmiSaveAs );
    jmFile.add( jmiClose );
    jmBar.add( jmFile );

    jmTasks = new JMenu( "Tasks" );
    jmTasks.setMnemonic( KeyEvent.VK_T );
    jmiDraw = new JMenuItem( "Draw a horse" );
    jmiProcess = new JMenuItem( "Show Results" );
    jmiPrint = new JMenuItem( "Print Table" );
    jmTasks.add( jmiProcess );
    jmTasks.add( jmiDraw );
    jmTasks.add( jmiPrint );
    jmBar.add( jmTasks );

    jmiDraw.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed( ActionEvent e ) {
    String firstName = JOptionPane.showInputDialog(
    "Enter name of first jockey" );

    String secondName = JOptionPane.showInputDialog(
    "Enter name of second jockey" );

    int value1 = nameValue( firstName );
    int value2 = nameValue( secondName );

    String result = "Name value of " + firstName + ": " + value1 +
    "\nName value of " + secondName + ": " + value2;

    JOptionPane.showMessageDialog( null ,
    result,
    "Result of the Draw",
    JOptionPane.INFORMATION_MESSAGE );
    }
    });

    jmiPrint.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed( ActionEvent e ) {
    try {
    proc.getTable().print();
    }
    catch ( java.awt.print.PrinterException exc ) {
    JOptionPane.showMessageDialog( null,
    "Printing error: " + exc ,
    "Warning",
    JOptionPane.ERROR_MESSAGE,
    new ImageIcon("icons/printer.png"));
    }
    }
    });


    jmiProcess.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed( ActionEvent e ) {
    status += "The numbers of the race are:\n" + "DV = " + getDV() +
    ", FNH = " + reducedFNH + ", ID = " + getID() +
    ", VP = " + getVP();

    status += proc.getStats();

    message.setText( status );
    }
    });


    // Setting up the data tab
    data.setOpaque( true );
    BorderLayout borderLayout = new BorderLayout();
    data.setLayout( borderLayout );

    // Setting up the table tab
    table.setOpaque( true );
    table.setLayout( borderLayout );

    //Adding top icon
    icon = new ImageIcon("icons/horse.png" );
    rider = new JLabel( icon );
    rider.setIcon( icon );
    data.add( rider, BorderLayout.NORTH );

    //Setting up panel for the race data
    middleBox.setOpaque( true );
    gridLayout = new GridLayout( 2, 4, 4, 4 );
    middleBox.setLayout( gridLayout );
    Border titled = BorderFactory.
    createTitledBorder( "Enter numbers of Race" );
    Border empty = BorderFactory.
    createEmptyBorder( 5, 40, 20, 40 );
    Border empty1 = BorderFactory.
    createEmptyBorder( 12, 4, 2, 2 );
    Border compound1 = BorderFactory.
    createCompoundBorder( empty, titled );
    Border compound = BorderFactory.
    createCompoundBorder( compound1, empty1 );
    middleBox.setBorder( compound );

    // Adding combobox for number of runners and setting FNH
    middleBox.add( new JLabel( "Runners:", SwingConstants.RIGHT ) );
    String[] numberOfRunners = { "8", "9", "10", "11", "12",
    "13", "14", "15", "16", "17",
    "18", "19", "20", "21", "22" };

    runners = new JComboBox( numberOfRunners );
    runners.setMaximumRowCount(5);
    runners.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed( ActionEvent e ) {
    String runnersNumber = ( String )runners.getSelectedItem();
    fnh = Integer.parseInt( runnersNumber );
    hNam = new String[ fnh ];
    hNum = new int[ fnh ];
    hVal = new int[ fnh ];
    jtxt = new JTextField[ fnh ];
    reducedFNH = reduce( fnh );
    }
    });
    middleBox.add( runners);


    // Adding combobox for division. Add listener later
    div = new JLabel( "Division:", SwingConstants.RIGHT );
    middleBox.add( div );
    String[] divisions = { "I", "II", "III" };
    raceDiv = new JComboBox( divisions );
    middleBox.add( raceDiv ) ;

    // Adding comboboxes for days of the month, month and setting DV
    middleBox.add ( new JLabel( "Day:", SwingConstants.RIGHT ) );
    String[] dayNumbers = { "1", "2", "3", "4", "5", "6",
    "7", "8", "9","10", "11", "12",
    "13", "14", "15", "16", "17",
    "18", "19", "20", "21", "22",
    "23", "24", "25", "26", "27",
    "28", "29", "30", "31" } ;

    days = new JComboBox( dayNumbers );
    days.setEditable( false );
    days.setMaximumRowCount(5);

    days.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed( ActionEvent e ) {
    day = days.getSelectedIndex();
    }
    });
    middleBox.add( days );


    // Adding combobox for the months
    middleBox.add( new JLabel( "Month:", SwingConstants.RIGHT ) );
    String[] theMonths = { "Jan", "Feb", "Mar", "Apr", "May",
    "Jun", "Jul", "Aug", "Sep", "Oct",
    "Nov", "Dec" };

    months = new JComboBox( theMonths );
    months.setEditable( false );
    months.setMaximumRowCount(5);

    months.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed( ActionEvent e ) {
    month = months.getSelectedIndex();
    }
    });
    middleBox.add( months ) ;

    data.add( middleBox, BorderLayout.CENTER );
    Box box = Box.createVerticalBox();
    Border boxBorder = BorderFactory.createEmptyBorder( 0, 40, 20, 40 );
    box.setBorder( boxBorder );


    // Adding textfield for the value of the plate
    box.add( new JLabel( "Name of the Race" ) );
    raceName = new JTextField();
    box.add( raceName );
    box.add( Box.createVerticalStrut(20) );


    // Adding button for the list of horses
    horseList = new JButton( "Enter Horse Names" );
    horseList.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed( ActionEvent e ) {
    dialog.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    dialog.setSize( 260, 400 );
    jpDialog.setLayout( gbLayout );
    JLabel label;
    String str;
    for( int i = 0; i < fnh; i++ )
    {
    gbc.gridx = 0;
    gbc.gridy = i;
    str = new Integer( i+1 ) + ".";
    label = new JLabel( str );
    jpDialog.add( label, gbc );

    gbc.gridx = 1;
    gbc.gridy = i;
    gbc.ipady = 4;
    gbc.insets = new Insets(4,0,0,0);
    jtxt[i] = new JTextField(15);
    jpDialog.add( jtxt[i], gbc );
    }
    buttonPanel = new JPanel();
    ok = new JButton( "OK" );
    ok.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed( ActionEvent e ) {
    for( int i = 0; i < fnh; i++ ) {
    hNam[i] = jtxt[i].getText();
    hNum[i] = reduce( i + 1 ); // Get the NH for each horse
    hVal[i] = nameValue( hNam[i] ); // Get the VH for each horse
    }
    setID( hNam ); // Get the race�s ID number
    dialog.setVisible( false );
    }
    });
    buttonPanel.add( ok );

    clear = new JButton ( "CLEAR" );
    clear.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed( ActionEvent e ) {
    for( int i = 0; i < fnh; i++ )
    if ( !"".equals( jtxt[i].getText() ) )
    jtxt[i].setText( "" );
    }
    });

    buttonPanel.add( clear );
    JScrollPane jscr = new JScrollPane( jpDialog );
    dialog.add( jscr, BorderLayout.CENTER );
    dialog.add( buttonPanel, BorderLayout.SOUTH );
    dialog.setVisible( true );
    }
    });
    box.add( horseList );
    box.add( Box.createVerticalStrut(20) );


    // Adding textfield to identify race
    box.add ( new JLabel( "Identify Race" ) );
    box.add( Box.createVerticalStrut( 4 ) );
    JTextField otherInfo = new JTextField();
    box.add( otherInfo );
    box.add( Box.createVerticalStrut(20) );


    // Adding textarea for race status
    box.add( new JLabel( "Messages" ) );
    box.add( Box.createVerticalStrut(4) );
    message.setBorder ( BorderFactory.createLineBorder(Color.blue, 1));
    message.setEditable( false );
    box.add( message );
    data.add( box, BorderLayout.SOUTH );


    // Setting up the jtabbedpanes
    jtb = new JTabbedPane();
    dataIcon = new ImageIcon( "icons/data.gif" );
    tableIcon = new ImageIcon( "icons/table.gif" );
    jtb.addTab( "Data Entry ", dataIcon , data );
    jtb.addTab( "Table ", tableIcon, table );
    jfr.add( jtb, BorderLayout.CENTER );
    }


    // -------------------------------------------------------------------------


    public static void main( String args[] ) {

    try {
    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    UIManager.setLookAndFeel(info.getClassName());
    break;
    }
    }
    }
    catch (UnsupportedLookAndFeelException e) {
    JOptionPane.showMessageDialog( null,
    "Printing error diagnosis: " + e,
    "Warning",
    JOptionPane.ERROR_MESSAGE,
    new ImageIcon( "icons/printer.jpg" ) );

    }
    catch (Exception e) {
    JOptionPane.showMessageDialog( null,
    "Diagnosis: " + e,
    "Warning",
    JOptionPane.ERROR_MESSAGE );
    }


    SwingUtilities.invokeLater( new Runnable() {
    @Override
    public void run() {
    AlladinLamp alladinlamp = new AlladinLamp();
    jfr.setVisible( true );
    }
    });



    }

    }


  2. #2
    Elite Member curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,132
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: JDialog not working properly

    That's a heck of a lot of code to ask us to go through. I suggest:
    1) please surround your posted code with code tags:
    [code]

    // ... your code goes here

    [/code]

    This way your code will retain its formatting and be more readable.

    2) Please isolate for us the code of interest so that we don't have to go through a ton of unrelated code. Please remember that we're volunteers doing this on our free time.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JDialog not working properly

    I have before been hesitant to post all this code online. I posted then what I judged to be the more relavant parts of it in other forums and was told I needed to post the entire source code in order to make the job of the volunteers easier. Now you tell me to do the opposite. So I am not sure how to proceed... I have left out some of it.

  4. #4
    Elite Member curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,132
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: JDialog not working properly

    Quote Originally Posted by agarta View Post
    I have before been hesitant to post all this code online. I posted the more relavant parts of it in other forums and was told I needed to focus on the relevant snippets in order to make the job of the volunteers easier. Now you tell me to do the opposite. So I am not sure how to proceed...
    Your best bit is to:
    • First and foremost, always use the formatting tools of whatever forum you're posting your code on so that the code retains its formatting. Different forums use different tools, and so it is a good idea to check out the forum's FAQ to see how to do it. I in fact show you how in my first post, and I urge you again to edit your original post here and add code tags.
    • Your other point is well taken as there's an art to asking a question in a forum, and it can be hard to know what is too much or too little code to post. You want to post enough code so that your question can be answered but not too much code so as to overwhelm the volunteers that help on the site.
    • Your best bet in fact is to write a new small program that compiles, runs, that illustrates your problem and that has no code unrelated to compiling, running, and illustrating the problem, a Short, Self Contained, Correct Example, or SSCCE.
    • In addition, it is sometimes a good idea to repost a small portion of your displayed code, that which you believe has the greatest likelihood of causing your problem, and then describing this section of code in detail so that we can understand it well.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JDialog not working properly

    OK, I am going to work further on it and repost if need be. Thank you kindly for taking the time to clarify things for me.
    Marcos

Similar Threads

  1. [SOLVED] System exit command is not working properly
    By LRWalton in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2012, 06:27 PM
  2. Applet not working properly
    By kookevk in forum AWT / Java Swing
    Replies: 1
    Last Post: February 3rd, 2012, 12:29 AM
  3. Scanner not working properly.
    By kookevk in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 25th, 2012, 09:14 PM
  4. Complete but not working properly
    By Noob101 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 02:17 PM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM