SWINGS Plz help ME...plz

Dear Friends,
I created a application in the following manner;
class StudentUtilities{
//i want to keep a method to clear the JFrame components to clear;
public void clear(FeePayments feepayments){
//code; also tell me how to access the components of FeePayment's JFrame from this code...plz
class FeePayments extends JFrame implements ...{
//few JTextFields placed on the JFrame;
public void actionPerformed(ActionEvent ae){
String st=ae.getActionCommand();
if (st.equals("clear")){
StudentUtilities su;
su.clear(this);
i invoked this method to clear the contents of the JFrame; but the method clear() is in
StudentUtilities class. It is showing no erros, but not working properly....
Plz,plz plz i need help
thank u
yours brother....
bye

Please make the extra effort to write out words such as "please" and "your". The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership.
How To Use Code Tags (+With Extra Screen Shot Goodness+)
[Step One|http://img221.imageshack.us/my.php?image=steponela4.jpg]: Highlight the code in your text editor, and copy it.
[Step Two|http://img509.imageshack.us/my.php?image=steptwovk5.jpg]: Paste the code into the forum "Message Editor"
[Step Three|http://img123.imageshack.us/my.php?image=stepthreely8.jpg]: Highlight the code and press the "Code" button (highlighted in red)

Similar Messages

  • I need to control the position and size of each java swing component, help!

    I setup a GUI which include 2 panels, each includes some components, I want to setup the size and position of these components, I use setBonus or setSize, but doesn't work at all. please help me to fix it. thanks
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.FlowLayout;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.event.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import javax.swing.JPanel;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.DefaultListModel;
    public class weather extends JFrame {
    private String[] entries={"1","22","333","4444"} ;
    private JTextField country;
    private JList jl;
    private JTextField latitude;
    private JTextField currentTime;
    private JTextField wind;
    private JTextField visibilityField;
    private JTextField skycondition;
    private JTextField dewpoint;
    private JTextField relativehumidity;
    private JTextField presure;
    private JButton search;
    private DefaultListModel listModel;
    private JPanel p1,p2;
    public weather() {
         setUpUIComponent();
    setTitle("Weather Report ");
    setSize(600, 400);
    setResizable(false);
    setVisible(true);
    private void setUpUIComponent(){
         p1 = new JPanel();
    p2 = new JPanel();
         country=new JTextField(10);
         latitude=new JTextField(12);
    currentTime=new JTextField(12);
    wind=new JTextField(12);
    visibilityField=new JTextField(12);
    skycondition=new JTextField(12);
    dewpoint=new JTextField(12);
    relativehumidity=new JTextField(12);
    presure=new JTextField(12);
    search=new JButton("SEARCH");
    listModel = new DefaultListModel();
    jl = new JList(listModel);
    // jl=new JList(entries);
    JScrollPane jsp=new JScrollPane(jl);
    jl.setVisibleRowCount(8);
    jsp.setBounds(120,120,80,80);
    p1.add(country);
    p1.add(search);
    p1.add(jsp);
    p2.add(new JLabel("latitude"));
    p2.add(latitude);
    p2.add(new JLabel("time"));
    p2.add(currentTime);
    p2.add(new JLabel("wind"));
    p2.add(wind);
    p2.add(new JLabel("visibility"));
    p2.add(visibilityField);
    p2.add(new JLabel("skycondition"));
    p2.add(skycondition);
    p2.add(new JLabel("dewpoint"));
    p2.add(dewpoint);
    p2.add(new JLabel("relativehumidity"));
    p2.add(relativehumidity);
    p2.add(new JLabel("presure"));
    p2.add(presure);
    this.getContentPane().setLayout(new FlowLayout());
    this.setLayout(new GridLayout(1,2));
    p2.setLayout(new GridLayout(8, 2));
    this.add(p1);
    this.add(p2);
    public static void main(String[] args) {
    JFrame frame = new weather();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    Use code tags to post codes -- [code]CODE[/code] will display asCODEOr click the CODE button and paste your code between the {code} tags that appear.
    [http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html]
    db

  • HTML in Swing Tutorial/Help

    I'm looking for a tutorial or just help here on how to display an HTML file in a swing component.

    http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html
    These components won't be able to display anything but the most basic HTML page. I've seen better components out there (you will have to search).
    When I needed to do this, I used the browser itself. I had an applet in one frame which sent html to the other frame via javascript.

  • Swing ActionListener help?

    Hi there,
    I'm trying to get to grips with Swing and I'm reasonably comfortable with laying out the GUI now. However, I'm still trying to get to grips with ActionListeners. As I understand it, you can have any old class as an ActionListener as long as it implements the ActionListener interface.....then you can just add this ActionListener to a component using .addActionListener().
    Couple of questions though....
    1. Is it generally bad design to just have a component call <whatever>.addActionListener(this) and then just implement the actionPerformed() method within the same class?
    2. Do you have to define a seperate ActionListener class for each type of component, or can you use one ActionListener for, say, one whole GUI screen? How do you usually organise these things?
    3. Anyone point me towards some decent tutorials on Java Swing event-handling?....preferably not the Sun ones, although if they are regarded as the best, I'll take 'em. :)
    Thanks for your time.

    URgent help need.
    i need to link the page together : by clicking the button on the index page.
    it will show the revelant class file. I have try my ationPerformed method to the actionlistener, it cannot work.
    Thanks in advance.
    // class mtab
    // the tab menu where it display the gui
    import javax.swing.*;
    import java.awt.*;
    public class mtab {
    private static final int frame_height = 480;
    private static final int frame_width = 680;
    public static void main (String [] args){
    JFrame frame = new JFrame ("Mrt Timing System");
    frame.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE);
    frame.setSize(frame_width,frame_height);
    JTabbedPane tp = new JTabbedPane ();
    tp.addTab("Mrt Timing System", new sample());
    frame.setResizable(false);
    frame.getContentPane().add(tp);
    frame.setSize(680,480);
    frame.show();
    // index page
    // class sample
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Image;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    import java.text.*;
    import javax.swing.border.*;
    class sample extends JPanel implements ActionListener
    //button
    private JButton TimingButton;
    private JButton ViewButton;
    private JButton FrequencyButton;
    private JButton calculateButton;
    private CardLayout mycard;
    private JPanel SelectXPanel;
    private JPanel SelectYPanel;
    private JPanel SelectZPanel;
    private JPanel bigFrame, mainPane;
    // constructor
    public sample() {
    super(new BorderLayout());
    //create the object
    //create button and set it
    TimingButton = new JButton("MRT Timing Calculator");
    TimingButton.addActionListener(this);
    ViewButton = new JButton("View First and Last Train");
    ViewButton.addActionListener(this);
    FrequencyButton = new JButton("Show the Train Frequency");
    FrequencyButton.addActionListener(this);
    // Layout
    //Lay out the button in a panel.
    JPanel buttonPane = new JPanel(new GridLayout(3,0));
    buttonPane.add(TimingButton);
    buttonPane.add(ViewButton);
    buttonPane.add(FrequencyButton);
    // Layout the button panel into another panel.
    JPanel buttonPane2 = new JPanel(new GridLayout(0,1));
    buttonPane2.add(buttonPane);
    tfrequency x = new tfrequency();
    fviewl2 y = new fviewl2 ();
    timing z = new timing ();
    JPanel SelectXPanel = new JPanel(new GridLayout(0,1));
    SelectXPanel.add(x);
    JPanel SelectYPanel = new JPanel(new GridLayout(0,1));
    SelectYPanel.add(y);
    JPanel SelectZPanel = new JPanel(new GridLayout(0,1));
    SelectZPanel.add(z);
    // Layout the button by putting in between the rigid area
    JPanel mainPane = new JPanel(new GridLayout(3,0));
    mainPane.add(Box.createRigidArea(new Dimension(0,1)));
    mainPane.add(buttonPane2);
    mainPane.add(Box.createRigidArea(new Dimension(0,1)));
    mainPane.setBorder(new TitledBorder("MRT Timing System"));
    // x = new tfrequency();
    // The overall panel -- divide the frame into two parts: west and east.
    JPanel bigFrame = new JPanel(new GridLayout(0,2));
    bigFrame.add(mainPane, BorderLayout.WEST);
    //bigFrame.add(x,BorderLayout.EAST); // this is where i want to link the page
    // this page being the index page. there being nothing to display.
    add(bigFrame);
    //Create the GUI and show it. For thread safety,
    public void actionPerformed (ActionEvent e){
    if (e.getSource() == TimingButton ){
    JPanel bigFrame = new JPanel(new GridLayout(0,2));
    bigFrame.add(mainPane, BorderLayout.WEST);
    bigFrame.add(SelectZPanel,BorderLayout.EAST);
    add(bigFrame);
    else if (e.getSource() == ViewButton ){
    JPanel bigFrame = new JPanel(new GridLayout(0,2));
    bigFrame.add(mainPane, BorderLayout.WEST);
    bigFrame.add(SelectYPanel,BorderLayout.EAST);
    add(bigFrame);
    else if (e.getSource() == FrequencyButton ){
    JPanel bigFrame2 = new JPanel(new GridLayout(0,2));
    bigFrame.add(mainPane, BorderLayout.WEST);
    bigFrame.add(SelectXPanel,BorderLayout.EAST);
    add(bigFrame);
    // Train Frequency Page
    // class fviewl2
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Image;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    import java.text.*;
    import javax.swing.border.*;
    class fviewl2 extends JPanel implements ActionListener
    //Labels to identify the fields
    private JLabel stationLabel;
    private JLabel firstLabel;
    private JLabel lastLabel;
    //Strings for the labels
    private static String station = "MRT Station:";
    private static String first = "First Train Time:";
    private static String last = "Last Train Time:";
    //Fields for data entry
    private JFormattedTextField stationField;
    private JFormattedTextField firstField;
    private JFormattedTextField lastField;
    //button
    private JButton homeButton;
    private JButton cancelButton;
    private JButton calculateButton;
    public fviewl2()
    super(new BorderLayout());
    //create the object
    //Create the Labels
    stationLabel = new JLabel(station);
    firstLabel = new JLabel (first);
    lastLabel = new JLabel (last) ;
    //Create the text fields .// MRT Station:
    stationField = new JFormattedTextField();
    stationField.setColumns(10);
    stationField.setBounds(300,300,5,5);
    //Create the text fields // First Train Time:
    firstField = new JFormattedTextField();
    firstField.setColumns(10);
    firstField.setBounds(300,300,5,5);
    //Create the text fields //Last Train Time:
    lastField = new JFormattedTextField();
    lastField.setColumns(10);
    lastField.setBounds(300,300,5,5);
    //Tell accessibility tools about label/textfield pairs, matching label for the field
    stationLabel.setLabelFor(stationField);
    firstLabel.setLabelFor(firstField);
    lastLabel.setLabelFor(lastField);
    //create button and set it
    homeButton = new JButton("Home");
    //homeButton.addActionListener(this);
    cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(this);
    calculateButton = new JButton("Get Time");
    // Layout
    //MRT Station Label // insert into the panel
    JPanel StationPane = new JPanel(new GridLayout(0,1));
    StationPane.add(stationLabel);
    //MRT Station input field // insert into the panel
    JPanel StationInput = new JPanel(new GridLayout(0,1));
    StationInput.add(stationField);
    //Get Time Button // insert into the panel
    JPanel GetTime = new JPanel(new GridLayout(0,1));
    GetTime.add(calculateButton);
    //Lay out the labels in a panel.
    JPanel FlabelL = new JPanel(new GridLayout(0,1));
    FlabelL.add(firstLabel);
    FlabelL.add(lastLabel);
    // Layout the fields in a panel
    JPanel FFieldL = new JPanel(new GridLayout(0,1));
    FFieldL.add(firstField);
    FFieldL.add(lastField);
    //Lay out the button in a panel.
    JPanel buttonPane = new JPanel(new GridLayout(1,0));
    buttonPane.add(homeButton);
    buttonPane.add(cancelButton);
    // Layout all components in the main panel
    JPanel mainPane = new JPanel(new GridLayout(4,2));
    mainPane.add(StationPane);
    mainPane.add(StationInput);
    mainPane.add(Box.createRigidArea(new Dimension(0,1)));
    mainPane.add(GetTime);
    mainPane.add(FlabelL);
    mainPane.add(FFieldL);
    mainPane.add(Box.createRigidArea(new Dimension(0,1)));
    mainPane.add(buttonPane);
    mainPane.setBorder(new TitledBorder("View First and Last Train"));
    JPanel leftPane = new JPanel(new GridLayout(1,0));
    leftPane.add(Box.createRigidArea(new Dimension(0,1)));
    leftPane.add(mainPane);
    leftPane.add(Box.createRigidArea(new Dimension(0,1)));
    JPanel hahaFrame = new JPanel(new GridLayout(0,1));
    hahaFrame.add(Box.createRigidArea(new Dimension(0,1)));
    hahaFrame.setBorder(BorderFactory.createEmptyBorder(30, 10, 80, 150));
    hahaFrame.add(Box.createRigidArea(new Dimension(0,1)));
    JPanel bigFrame = new JPanel();
    bigFrame.add(hahaFrame, BorderLayout.NORTH);
    bigFrame.add(leftPane, BorderLayout.CENTER);
    add(bigFrame, BorderLayout.CENTER);
    //Create the GUI and show it. For thread safety,
    private void cancelButtonClicked()
    stationField.setText("");
    firstField.setText("");
    lastField.setText("");
    public void actionPerformed (ActionEvent e){
    if (e.getSource() == cancelButton){
    cancelButtonClicked();
    }

  • Swing window help

    does anybody knows how to hide the top bar with the 'close', minimize and 'maximize' options from a swing window???
    THANKS!!! I NEED HELP!!! =)

    THANKS A LOT!!!!!!...
    It worked very good!!!!!! =)

  • Reversing with swing? Help!

    Alright, before you guys flip out and say post it to the swing forum, why does the swing forum say read only? Now I have searched the forum but cannot find anything that deals with swing and the kind I am doing. So if you can provide some advice or some help, it would be greatly appreciated. This is my last hurdle to tackle in my school project.
    I tried a StringBuffer approach but it kept giving me an error saying it would not work with JTextArea? Maybe the code was wrong?
    This needs to take the text on the screen which I have and post it reversed when the "Reverse" button is clicked... I have been searching the web and these forums and all the different reverse methods and theories are confusing me, I try to copy them and get them to work but I just keep receiving errors. What is the best way to do this with using GUI?
    else if (actionCommand.equals("Reverse"))
              reverseMemo = theText.getText();
    /*Is this where I would put the reverse code?*/          
    theText.setText(reverseMemo);
         }Thanks,
    Jess

    Just write a method to reverse the text
    read the string in reverse and start plugging the values into a string buffer (inside a loop of course)
    then display results:
    textfield.setText(stringbuffer.toString());
    This is my last hurdle to tackle in my school project.I highly doubt it.
    The fact you are asking this question shows me you did not learn anything about Java much less computer science. Your question has nothing to do with swing.

  • Prob with Swing Refreshing --Help Me

    Hii
    I have created a GUI in Swings, i need to apply some changes with one button click, but
    the changes dont get implemnet till i minimise and again maximize the frame,
    I am using repaint() method to get it refresh ,but it wont work
    Pls Help me if u can
    Thanx
    Your Friend

    if ur changing components and similar this usually does the work:
    parentcomponent.validate(); //or revalidate()
    parentcomponent.repaint();
    where parentcomponent would be the container of the changed components.

  • A tiny font is used in many places across Java Swing applications (help!)

    In many places in Java but mostly in text boxes I'm seeing a very small font that is quite hard to read.
    I am seeing this all over the place in Java programs and it is getting on my nerves.
    I'm not sure what is causing this.
    I also encountered this issue when running Java on Windows XP.
    I really need help on how to solve this,
    Or maybe it is actually a bug with Java itself and Windows?
    An image of the problem in SwingSet2:
    [SwingSet 2 Tiny Font Example|http://img687.imageshack.us/img687/1373/swingset2tinyfont.png]
    System spec:
    Windows 7 Professional 32-bit (Aero is on)
    Java 6 Update 20
    English system locale and language
    No special dpi settings (default)
    Screen Resolution: 1280x1024
    Avast Antivirus 5.0
    An intel graphics card
    Edited by: Temp111 on May 9, 2010 7:38 AM

    camickr wrote:
    That is not a problem. Somewhere in that code the programmer uses setFont(....) to make it a smaller font. The default font of a text component does not behave like that.
    For more help create a [SSCCE (Short, Self Contained, Compilable and Executable, Example Program)|http://sscce.org/], that demonstrates the incorrect behaviour.
    Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.No, it's defiantly a problem. Probably in the default fonts that Swing uses in some places.
    I detected one place where I can easily trigger this issue, a JTextArea And the Windows Look and Feel, So here is a SSCCE for you:
    [Picture Of Tiny Font SSCCE With Tiny Font issue|http://img88.imageshack.us/img88/7876/tinyfont.png]
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    public class TinyFont {
         public static void main(String[] args) {
              try {
                   UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
              } catch (ClassNotFoundException e) {
                   e.printStackTrace();
              } catch (InstantiationException e) {
                   e.printStackTrace();
              } catch (IllegalAccessException e) {
                   e.printStackTrace();
              } catch (UnsupportedLookAndFeelException e) {
                   e.printStackTrace();
              JFrame frame = new JFrame("Tiny Font");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JTextArea textArea = new JTextArea("Tiny Font Test");
              frame.add(textArea);
              frame.setSize(150, 100);
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);
    }But I also encountered this issue in other places that use Swing.
    Try SwingSet2 Source Code Tab for example: [SwingSet2|http://java.sun.com/products/jfc/jws/SwingSet2.jnlp]

  • New to swing :( need help with simple text areas

    i'm trying to make a username and pasword GUI thingy (techinical word) but what i have now is this:-
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.URL;
    import java.io.IOException;
    public class swing1 extends JFrame implements ActionListener
         private String newline = "\n";
         protected static final String textFieldString = "JTextField";
         protected static final String passwordFieldString = "JPasswordField";
         protected JLabel actionLabel;
         private JTextField textField;
         private JPasswordField passwordField;
         private Container p; // make a panel to witch the components can be added
         public swing1()
              super("swing1");
              //Create a regular text field.
              textField = new JTextField(10);
              textField.setActionCommand(textFieldString);
              textField.addActionListener(this);
              //Create a password field.
              passwordField = new JPasswordField(10);
              passwordField.setActionCommand(passwordFieldString);
              passwordField.addActionListener(this);
              //Create some labels for the fields.
              JLabel textFieldLabel = new JLabel(textFieldString + ": ");
              textFieldLabel.setLabelFor(textField);
              JLabel passwordFieldLabel = new JLabel(passwordFieldString + ": ");
              passwordFieldLabel.setLabelFor(passwordField);
              //Create a label to put messages during an action event.
              actionLabel = new JLabel("Type text and then Return in a field.");
              actionLabel.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
              //Lay out the text controls and the labels.
              p=getContentPane(); //get te contant pane of this Swing1 to add the componets to
              p.add("West",textField); //add your fist component, add it west on the dafault borderLayout
              p.add("East",textFieldLabel);// add another component, add it east on the dafault borderLayout
              p.add("South",passwordField);// add it south on the dafault borderLayout
              p.add("North",actionLabel); // add it north on the dafault borderLayout
              setSize(400,100); //make it a bit bigger
              setVisible(true);
              public void actionPerformed(ActionEvent e)
                   Object o = e.getSource();// the component that fired this event
                   if (o == textField)
                        //action from the textField
                   else if (o == passwordField)
                        //action from passwordfield
              public static void main(String[] args)
                                  JFrame frame = new TextSamplerDemo();
                                  frame.addWindowListener(new WindowAdapter()
                                       public void windowClosing(WindowEvent e)
                                                 System.exit(0);
                                                                new swing1(); //make a new instance of your class
    [\code]
    why won't the label on my password field dislay?
    and can you take a look at the end of my code i got it off another program, i want to get rid of the HTML page its trying to access and i want it just to close when i click X
    any help would be briliant
    Ant                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Since you are new to Swing the first thing I would recommend is to read the tutorial "Creating a Gui with JFC/Swing". I think the Text Component section has a demo on creating a username/password GUI. It can be downloaded at:
    http://java.sun.com/docs/books/tutorial/
    Why doesn't the password field label display? You are not adding it to the container. You add textField, textFieldLabel, passwordField, actionLabel but no passwordFieldLabel.
    Instead of adding a WindowListener and implementing the windowClosing() method, there is an easier way to close the JFrame in JDK1.3. Simply use:
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    The class "TextSamplerDemo" doesn't belong in this class (it must be a demo class you downloaded from somewhere). All the code you need in the main method is:
    JFrame frame = new swing1();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    Also, by convention classes should be named with the first character of each word capitalized. So you class should be renamed to "Swing1" from "swing1". Also remember to rename the file as the filename and class name must be the same and case does matter.

  • Newby has problems with swing - please help

    hi guys I'm sure this is easy for someone. My Program myTimeCalc.java fails to run with a nullpointerexception. I realise I am very new to this so if I'm doing something really stupid then hey its about time I learnt. So thanks in advance and any help greatly appreciated.
    import java.awt.*;
    import javax.swing.*;
    public class myTimeCalc extends JFrame {
        public myTimeCalc() {
            super("Time Calc");
            setSize(400,400);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            timeField time1 =new timeField();
            timeField time2 =new timeField();
            JPanel panel = new JPanel();
            panel.add(time1);
            panel.add(time2);
            add(panel);
            setVisible(true);
        } // myTimeCalc
        public class timeField extends JPanel {
            public int hours;
            public int mins;
            public hoursField txtHours;
            public minsField txtMins;
            public JLabel lab;
            public timeField() {
                hoursField txtHours = new hoursField();
                minsField txtMins= new minsField();
                JLabel lab = new JLabel(":");
                //txtHours.setText("0");
                //txtMins.setText("0");
                setTime(0,0);
                add(txtHours);
                add(lab);
                add(txtMins);
            } // timeField
            public void setTime(int inHours, int inMins) {
                hours = inHours;
                mins = inMins;
                txtHours.setText("" + inHours );
                txtMins.setText("" + inMins);
            public class hoursField extends JTextField {
                public hoursField() {
                    super(4);
            } // hoursField
            public class minsField extends JTextField {
                public minsField() {
                    super(2);
            } // minsField
        } // timeField
        public static void main(String[] args) {
            myTimeCalc mtc = new myTimeCalc();
    } // myTimeCalc

    Hi Phill.,
    I have altered your code. Just a little bit. take a look.
    Please do changes accordingly. And better use NetBeans IDE for developing a swing application.
    -sps. :)
    import java.awt.*;
    import javax.swing.*;
    public class myTimeCalc extends JFrame
         private JPanel panel;
        public myTimeCalc()
            //super("Time Calc");
            initComponents();
        } // myTimeCalc
        private void initComponents()
            setSize(400,400);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            timeField time1 =new timeField();
            timeField time2 =new timeField();
            panel = new JPanel();
            panel.add(time1);
            panel.add(time2);
            getContentPane().add(panel);
            pack();
            setVisible(true);
        public class timeField extends JPanel
            public int hours;
            public int mins;
            public hoursField txtHours;
            public minsField txtMins;
            public JLabel lab;
            public timeField()
                txtHours = new hoursField();   // your code is hoursField txtHours = new hoursField();  there is no need for hoursField in front of the variable txtHours coz you have already declared this variable is of type hoursField.
                txtMins= new minsField();
                lab = new JLabel(":");
                //txtHours.setText("0");
                //txtMins.setText("0");
                setTime(0,0);
                add(txtHours);
                add(lab);
                add(txtMins);
            } // timeField
            public void setTime(int inHours, int inMins)
                hours = inHours;
                mins = inMins;
                txtHours.setText("" + inHours );
                txtMins.setText("" + inMins);
            public class hoursField extends JTextField
                public hoursField()
                    super(4);
            } // hoursField
            public class minsField extends JTextField
                public minsField()
                    super(2);
            } // minsField
        } // timeField
        public static void main(String[] args)
            new myTimeCalc().show();
    } // myTimeCalc

  • Problems with swing, please help!!!

    im trying to display an image made up of a set of squares.
    i have a JFrame class which has the main method placing a JComponent into its Container using the getContentPane().add method.
    the JComponent class sets up the image using the paintComponent() method
    When i try to run the program all i get is a grey frame.
    Ive looked at other threads in this forum to fix my problem but it doesnt seem to fix it.
    any help is much appreciated.

    import java.awt.*;
    import java.awt.event.*;
    import java.util.EventListener;
    import javax.swing.*;
    import java.applet.*;
    public class Display extends JFrame
         protected DisCanvas canvas;
         protected EventListener listener;
         protected boolean isApplet = false;
         public Display(boolean isApplet)
              this.isApplet = isApplet;
              //setLayout(new BorderLayout());
         //     add(makeCanvas(), BorderLayout.CENTER);
         public Display()
              //this(true);
    canvas = new DisCanvas();
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(canvas, BorderLayout.CENTER);
    this.addWindowListener(new AppCloser());
    this.setSize(300, 300);
    this.setVisible(true);
    this.repaint();
    public static void main(String[] args)
    new Display();
    static class AppCloser extends WindowAdapter
              public void windowClosing(WindowEvent e)
                   System.exit(0);
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.image.*;
    public class DisCanvas extends java.awt.Canvas
    Patch[] patchArray = new Patch[100];
    int width = 200;
    int height = 300;
    BufferedImage image;
    Graphics2D g2D;
    public DisCanvas()
    patchArray[0] = new Patch(0, 0, 20, 30); patchArray[10] = new Patch(0, 30, 20, 30);
    patchArray[1] = new Patch(20, 0, 20, 30); patchArray[11] = new Patch(20, 30, 20, 30);
    patchArray[2] = new Patch(40, 0, 20, 30); patchArray[12] = new Patch(40, 30, 20, 30);
    patchArray[3] = new Patch(60, 0, 20, 30); patchArray[13] = new Patch(60, 30, 20, 30);
    patchArray[4] = new Patch(80, 0, 20, 30); patchArray[14] = new Patch(80, 30, 20, 30);
    patchArray[5] = new Patch(100, 0, 20, 30); patchArray[15] = new Patch(100, 30, 20, 30);
    patchArray[6] = new Patch(120, 0, 20, 30); patchArray[16] = new Patch(120, 30, 20, 30);
    patchArray[7] = new Patch(140, 0, 20, 30); patchArray[17] = new Patch(140, 30, 20, 30);
    patchArray[8] = new Patch(160, 0, 20, 30); patchArray[18] = new Patch(160, 30, 20, 30);
    patchArray[9] = new Patch(180, 0, 20, 30); patchArray[19] = new Patch(180, 30, 20, 30);
    patchArray[20] = new Patch(0, 60, 20, 30); patchArray[30] = new Patch(0, 90, 20, 30);
    patchArray[21] = new Patch(20, 60, 20, 30); patchArray[31] = new Patch(20, 90, 20, 30);
    patchArray[22] = new Patch(40, 60, 20, 30); patchArray[32] = new Patch(40, 90, 20, 30);
    patchArray[23] = new Patch(60, 60, 20, 30); patchArray[33] = new Patch(60, 90, 20, 30);
    patchArray[24] = new Patch(80, 60, 20, 30); patchArray[34] = new Patch(80, 90, 20, 30);
    patchArray[25] = new Patch(100, 60, 20, 30); patchArray[35] = new Patch(100, 90, 20, 30);
    patchArray[26] = new Patch(120, 60, 20, 30); patchArray[36] = new Patch(120, 90, 20, 30);
    patchArray[27] = new Patch(140, 60, 20, 30); patchArray[37] = new Patch(140, 90, 20, 30);
    patchArray[28] = new Patch(160, 60, 20, 30); patchArray[38] = new Patch(160, 90, 20, 30);
    patchArray[29] = new Patch(180, 60, 20, 30); patchArray[39] = new Patch(180, 90, 20, 30);
    patchArray[40] = new Patch(0, 120, 20, 30); patchArray[50] = new Patch(0, 150, 20, 30);
    patchArray[41] = new Patch(20, 120, 20, 30); patchArray[51] = new Patch(20, 150, 20, 30);
    patchArray[42] = new Patch(40, 120, 20, 30); patchArray[52] = new Patch(40, 150, 20, 30);
    patchArray[43] = new Patch(60, 120, 20, 30); patchArray[53] = new Patch(60, 150, 20, 30);
    patchArray[44] = new Patch(80, 120, 20, 30); patchArray[54] = new Patch(80, 150, 20, 30);
    patchArray[45] = new Patch(100, 120, 20, 30); patchArray[55] = new Patch(100, 150, 20, 30);
    patchArray[46] = new Patch(120, 120, 20, 30); patchArray[56] = new Patch(120, 150, 20, 30);
    patchArray[47] = new Patch(140, 120, 20, 30); patchArray[57] = new Patch(140, 150, 20, 30);
    patchArray[48] = new Patch(160, 120, 20, 30); patchArray[58] = new Patch(160, 150, 20, 30);
    patchArray[49] = new Patch(180, 120, 20, 30); patchArray[59] = new Patch(180, 150, 20, 30);
    patchArray[60] = new Patch(0, 180, 20, 30); patchArray[70] = new Patch(0, 210, 20, 30);
    patchArray[61] = new Patch(20, 180, 20, 30); patchArray[71] = new Patch(20, 210, 20, 30);
    patchArray[62] = new Patch(40, 180, 20, 30); patchArray[72] = new Patch(40, 210, 20, 30);
    patchArray[63] = new Patch(60, 180, 20, 30); patchArray[73] = new Patch(60, 210, 20, 30);
    patchArray[64] = new Patch(80, 180, 20, 30); patchArray[74] = new Patch(80, 210, 20, 30);
    patchArray[65] = new Patch(100, 180, 20, 30); patchArray[75] = new Patch(100, 210, 20, 30);
    patchArray[66] = new Patch(120, 180, 20, 30); patchArray[76] = new Patch(120, 210, 20, 30);
    patchArray[67] = new Patch(140, 180, 20, 30); patchArray[77] = new Patch(140, 210, 20, 30);
    patchArray[68] = new Patch(160, 180, 20, 30); patchArray[78] = new Patch(160, 210, 20, 30);
    patchArray[69] = new Patch(180, 180, 20, 30); patchArray[79] = new Patch(180, 210, 20, 30);
    patchArray[80] = new Patch(0, 240, 20, 30); patchArray[90] = new Patch(0, 270, 20, 30);
    patchArray[81] = new Patch(20, 240, 20, 30); patchArray[91] = new Patch(20, 270, 20, 30);
    patchArray[82] = new Patch(40, 240, 20, 30); patchArray[92] = new Patch(40, 270, 20, 30);
    patchArray[83] = new Patch(60, 240, 20, 30); patchArray[93] = new Patch(60, 270, 20, 30);
    patchArray[84] = new Patch(80, 240, 20, 30); patchArray[94] = new Patch(80, 270, 20, 30);
    patchArray[85] = new Patch(100, 240, 20, 30); patchArray[95] = new Patch(100, 270, 20, 30);
    patchArray[86] = new Patch(120, 240, 20, 30); patchArray[96] = new Patch(120, 270, 20, 30);
    patchArray[87] = new Patch(140, 240, 20, 30); patchArray[97] = new Patch(140, 270, 20, 30);
    patchArray[88] = new Patch(160, 240, 20, 30); patchArray[98] = new Patch(160, 270, 20, 30);
    patchArray[89] = new Patch(180, 240, 20, 30); patchArray[99] = new Patch(180, 270, 20, 30);
    image = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2D = image.createGraphics();
    public void paint/*Component*/(Graphics g)
    g = (Graphics2D) g2D;
    super.paint/*Component*/(g);
    for (int i = 0; i == 99; i++)
    //for (int s = 0; s > 5; s++)
    // Color[] colours = new Color[6];
    // colours[0] = Color.blue;
    // colours[1] = Color.red;
    // colours[2] = Color.green;
    // colours[3] = Color.orange;
    // colours[4] = Color.black;
    // colours[5] = Color.white;
    // g2D.setColor(colours[s]);
    g2D.setColor(Color.red);
    g2D.fillRect(patchArray.getX(), patchArray[i].getY(),
    patchArray[i].getWidth(), patchArray[i].getHeight());
    g2D.drawImage(image,0, 0, this);

  • Swing guru help...

    Hello, everyone
    I am writing a swing application on unix. But I always have problem to set frame or dialog position on unix.
    No matter what, the fram or dialog always display in top left.
    ===code slip==========
    Frame1_AboutBox dlg = new Frame1_AboutBox(this);
    Dimension dlgSize = dlg.getPreferredSize();
    Dimension frmSize = getSize();
    Point loc = getLocation();
    dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
    dlg.setModal(true);
    dlg.show();
    ===code slip==========
    Please help!
    --tony                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

    have you tested the values of dlgSize.height and dlgSize.width? It is possible that they are returning 0...

  • Thread in Swing -Please help

    Hi
    I want to use thread in loading data in the JFrame / JInternalFrame and thus separating the initialization of the GUI.
    The sample is as follows-
    public class TestThreadFrame extends javax.swing.JFrame {
    public TestThreadFrame() {
    initComponents();
    loadData();
    private void initComponents() {
    jComboBox1 = new javax.swing.JComboBox();
    getContentPane().setLayout(new java.awt.GridBagLayout());
    addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent evt) {
    exitForm(evt);
    jComboBox1.setPreferredSize(new java.awt.Dimension(150, 25));
    getContentPane().add(jComboBox1, new java.awt.GridBagConstraints());
    pack();
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
    System.exit(0);
    * @param args the command line arguments
    public static void main(String args[]) {
    new TestThreadFrame().show();
    // variable declare
    private javax.swing.JComboBox jComboBox1;
    void loadData(){
    //code for loading data e.g. loading data in the JCombobox
    ---> Any help is appreciated.
    mortoza

    Do you mean something like this ?
    public class TestThreadFrame extends javax.swing.JFrame {
    public TestThreadFrame() {
    initComponents();
    // loadData();
    loadData l = new loadData();
    l.start();
    private void initComponents() {
    jComboBox1 = new javax.swing.JComboBox();
    getContentPane().setLayout(new java.awt.GridBagLayout());
    addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent evt) {
    exitForm(evt);
    jComboBox1.setPreferredSize(new java.awt.Dimension(150, 25));
    getContentPane().add(jComboBox1, new java.awt.GridBagConstraints());
    pack();
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
    System.exit(0);
    * @param args the command line arguments
    public static void main(String args[]) {
    new TestThreadFrame().show();
    // variable declare
    private javax.swing.JComboBox jComboBox1;
    class loadData extends Thread
    public void run()
    {   try  {
    //code for loading data e.g. loading data in the JCombobox
    jComboBox1.addItem("Hello");
    Thread.sleep(2000);
    jComboBox1.addItem("Hello2");
    Thread.sleep(2000);
    jComboBox1.addItem("Hello3");
    Thread.sleep(2000);
    jComboBox1.addItem("Hello3");
    catch( Exception x ) { System.out.println(" Ooops : "+x.toString() ); }

  • Launching Browser From Swing Application - Help Needed

    Hi,
    I am opening a HTML file in Internet Explorer from my swing application.
    I am using the code given below
    private final static String WIN_FLAG = "url.dll,FileProtocolHandler";
    private final static String WIN_PATH = "rundll32";
    String cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
    // url is HTML file Path
    Process p = Runtime.getRuntime().exec( cmd );
    The HTML file is opening up,but it always opens behind the swing application,that makes me every time to maximize the HTML file after it being opened up.
    I want to open it in front of the Swing Application.
    Any Help Please ?
    - Manikandan

    Browser Utility - Class to launch a URL in a web browser in
    a platform independent manner. Includes an optional swing
    GUI allowing user customization. [Open Source - GPL]
    http://ostermiller.org/utils/Browser.html
    BrowserLauncher - Open the system web browser with
    particular attention paid to various Macintosh systems.
    (Freeware)
    http://browserlauncher.sourceforge.net/
    Java World - With a little platform-specific Java code, you
    can easily use your system's default browser to display any
    URL in Windows or Unix.
    http://www.javaworld.com/javaworld/javatips/jw-javatip66.html
    JConfig - Class Libraries that allow a URL to be launched in
    a browser on Windows, Unix, or Macintosh. [Commercial]
    http://www.tolstoy.com/samizdat/jconfig.html
    Apple - MRJFileUtils.openURL() not implemented in Mac OS X.
    http://developer.apple.com/techpubs/macosx/ReleaseNotes/JavaGMWebReleaseNotes.html#MRJToolkit
    Apple - How one would open a URL in a web browser on a
    Macintosh.
    http://developer.apple.com/qa/java/java12.html

  • Swing browser help needed

    I am developing desktop application which has a functionality of web browser i have tried JEditorPanel which is used to load html pages.
    the problem with this is it doesnt load even google.com or yahoo.com , like Internet Explorer , Mozilla etc. Neither it loads the pages containing flash , please let me know how can i do this.
    I have a jpanel form on click of menu or button i want to call another jpanel form . i have tried with jFrameForm and it works but same doesnt work with jPanelform
    Please help . your help will be appreciated
    thanks

    Browser Utility - Class to launch a URL in a web browser in
    a platform independent manner. Includes an optional swing
    GUI allowing user customization. [Open Source - GPL]
    http://ostermiller.org/utils/Browser.html
    BrowserLauncher - Open the system web browser with
    particular attention paid to various Macintosh systems.
    (Freeware)
    http://browserlauncher.sourceforge.net/
    Java World - With a little platform-specific Java code, you
    can easily use your system's default browser to display any
    URL in Windows or Unix.
    http://www.javaworld.com/javaworld/javatips/jw-javatip66.html
    JConfig - Class Libraries that allow a URL to be launched in
    a browser on Windows, Unix, or Macintosh. [Commercial]
    http://www.tolstoy.com/samizdat/jconfig.html
    Apple - MRJFileUtils.openURL() not implemented in Mac OS X.
    http://developer.apple.com/techpubs/macosx/ReleaseNotes/JavaGMWebReleaseNotes.html#MRJToolkit
    Apple - How one would open a URL in a web browser on a
    Macintosh.
    http://developer.apple.com/qa/java/java12.html

Maybe you are looking for