Question on JFrame

Hi All,
I have a small doubt to be clarified.We have JFrame class inside javax.swing package as every one knows it and closing of frame is possible with EXIT_ON_CLOSE and some other operations like DISPOSE_ON_CLOSE,HIDE_ON_CLOSE etc.But how to disable a frame(in the sense frame should not be visible initially and later should be enabled after some time).Is there any way to do this..Please help me on this query.
Thanks in advance to all.

Darryl.Burke wrote:
Hmm. And did you read the API to see whether there were any mehods related to the visible property of JFrame?The method won't be easy to find in the JFrame API but is to be found in the API of one of JFrame's parents, namely the Window class. Please have a look here:
[http://java.sun.com/javase/6/docs/api/java/awt/Window.html]

Similar Messages

  • Question on JFrame, help me plzzzzzzz!!!

    Hi my friend:
    I need your help on JFrame urgently. As we know, we close a JFrame by clicking the close button on the window title pane. Now I want to change this default for my JFrame. So that when someone click the close button, an Joptionpane is pop out to confirm the closing action. The JFrame will only be closed by clicking the "OK" button on the Joptionpane. If click cancel, optionpane disappear. How to do this? Pls guide me.

    close a JFrame by clicking the close button on the
    window title pane. Now I want to change this default
    for my JFrame. 1) http://java.sun.com/j2se/1.3/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int)
    So that when someone click the close
    button, an Joptionpane is pop out to confirm the
    closing action. The JFrame will only be closed by
    clicking the "OK" button on the Joptionpane. If click
    cancel, optionpane disappear.2)
    if(JOptionPane.showConfirmDialog(JFrame.this,"Really close?","Confirm Window Closing", JOptionPane.YES_NO_CANCEL_OPTION)==?){
    //do something...
    }the ? means you can figure it out yourself with the documentation...
    http://java.sun.com/j2se/1.3/docs/api/javax/swing/JOptionPane.html

  • Beginner question using JFrame

    Hello, I'm just getting started using swing for a school project and decided that I wanted to write all the code myself without using the netbeans ide to make the interface.
    I made 2 java classes and tried to show a simple textfield.
    Unfortunately it doesn't show anything so I must be doing something wrong.
    Any suggesstions?
    package prog4gui;
    import javax.swing.*;
    public class Main {
        public static void main(String[] args) {
            FrmMain frmMain = new FrmMain();
            frmMain.setTitle("test");
            frmMain.pack();
            frmMain.setVisible(true);
    package prog4gui;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class FrmMain extends JFrame {
        private JPanel main;
        private JTextField tf;
        public void FrmMain() {
            initComponents();
            try {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                SwingUtilities.updateComponentTreeUI(this);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            this.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    exitForm();
        public void exitForm() {
            System.exit(0);
        private void initComponents() {
            main = new JPanel();
            main.setPreferredSize(new Dimension(300, 100));
            tf = new JTextField(20);
            getContentPane().add(main, BorderLayout.CENTER);
            main.add(tf);
    }Thanks in advance.

    your 'constructor' is not a constructor, just another method
    //public void FrmMain() {
    public FrmMain() {

  • Basic question about JFrame

    How to display a jframe maximized ?!!! so the user doesn't have to click on the maximize top right buttom.
    // this is not working out
    this_jframe.setMaximizedBounds(this_jframe.getMaximizedBounds());
    Thanks in advance
    Marla

    myFrame.setExtendedState(Frame.MAXIMIZED_BOTH);

  • Questions about JFrame

    Hi friend:
    It seems JAVA has two ways to make a JFrame. What is the difference between them? Pls have a look at the following code:
    1)
    public class MyFrame extends JFrame
    public static void main(String [] args)
    MyFrame f1 = new MyFrame();
    public MyFrame()
    JPanel p = new JPanel (new BorderLayout());
    setContentPane(p);
    setSize(700, 500);
    setVisible(true);
    2)
    public class MyFrame extends JFrame
    public static void main (String[] args)
    MyFrame f1 = new MyFrame();
    public MyFrame()
    Container c = getContentPane();
    c.setLayout(new BorderLayout());
    JPanel p = new JPanel();
    c.add(p);
    Can these two class get the same object? What is a Container? What is the difference between "Container c = getContentPane()" and "setContentPane(P)?
    They are so confusing>>>>>>>, can someone help me???

    Hi,
    A container is what the name suggests. Its a generic holder of other GUI objects and is also a component. Its implemented as a list, in the form of a stack. There is no differece between these two methods, you can use either to set the content pane for your frame. As getContentPane() returns a container and setContentPane(Component) , where container is a sub class of component.
    By the way the concept of a content pane was introduced by ajava in order to differenciate the way the componenst are painted in swing and AWT.

  • JComboBox addActionListener?

    Hi, I'm trying to write a program so that when the user makes a selection from the combobox, the selection will be inputted into a string.
    However, whenever I try to run the program it gives me this error:
    "Questions.java": addItemListener(java.awt.event.ItemListener) in javax.swing.JComboBox cannot be applied to (Questions) at line 120, column 15
    This is the part of my code which I think is relevent:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.io.*;
    public class Questions extends JFrame {
      private Container container;
      private GridBagLayout gbLayout;
      private GridBagConstraints gbConstraints;
      private ButtonGroup radioGroup;
      JLabel correctL, incorrectL, correctNumL, incorrectNumL,
          youSelectL, correctSelectL;
      JButton nextB, exitB, guessB;
      JCheckBox aCB, bCB, cCB, dCB;
      JTextArea questionTA;
      JComboBox selection = new JComboBox();
      public static int qNum[] = new int[20];
      public static String question[] = new String[20];
      public static String choiceA[] = new String[20];
      public static String choiceB[] = new String[20];
      public static String choiceC[] = new String[20];
      public static String choiceD[] = new String[20];
      public static String answer[] = new String[20];
      public static int recordNum = (int) (Math.random() * 20);
      public static int total = 0;
      public static int right = 0;
      public static int wrong = 0;
      public static String whichQselected;
      public static boolean qAnswered = false;
      nextButtonHandler nextHandler;
      guessButtonHandler guessHandler;
      exitButtonHandler exitHandler;
      public Questions() {
        super("Questions");
        setContentPane(new ContentPanel("bg.gif"));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        container = getContentPane();
        gbLayout = new GridBagLayout();
        container.setLayout(gbLayout);
        gbConstraints = new GridBagConstraints();
        questionTA = new JTextArea(question[recordNum], 3, 23);
        questionTA.setForeground(Color.blue);
        questionTA.setFont(new Font("Impact", Font.PLAIN, 15));
        questionTA.setWrapStyleWord(true);
        questionTA.setLineWrap(true);
        questionTA.setEditable(false);
        correctL = new JLabel("Correct Answers: ");
        correctL.setForeground(Color.magenta);
        correctL.setFont(new Font("Ariel", Font.PLAIN, 10));
        correctNumL = new JLabel("0/0");
        correctNumL.setForeground(Color.magenta);
        correctNumL.setFont(new Font("Ariel", Font.PLAIN, 10));
        incorrectL = new JLabel("Incorrect Answers: ");
        incorrectL.setForeground(Color.red);
        incorrectL.setFont(new Font("Ariel", Font.PLAIN, 10));
        incorrectNumL = new JLabel("0/0");
        incorrectNumL.setForeground(Color.red);
        incorrectNumL.setFont(new Font("Ariel", Font.PLAIN, 10));
        youSelectL = new JLabel("You've selected:");
        youSelectL.setForeground(Color.orange);
        youSelectL.setFont(new Font("Ariel", Font.BOLD, 13));
        correctSelectL = new JLabel("The Correct answer is: ");
        correctSelectL.setForeground(Color.ORANGE);
        correctSelectL.setFont(new Font("Ariel", Font.BOLD, 13));
        nextB = new JButton("Next Question");
        nextHandler = new nextButtonHandler();
        nextB.addActionListener(nextHandler);
        nextB.setBackground(Color.WHITE);
        guessB = new JButton("Guess Picture");
        guessHandler = new guessButtonHandler();
        guessB.addActionListener(guessHandler);
        guessB.setBackground(Color.WHITE);
        exitB = new JButton("Exit");
        exitHandler = new exitButtonHandler();
        exitB.addActionListener(exitHandler);
        exitB.setBackground(Color.WHITE);
        aCB = new JCheckBox(choiceA[recordNum]);
        aCB.setBackground(Color.white);
        bCB = new JCheckBox(choiceB[recordNum]);
        bCB.setBackground(Color.white);
        cCB = new JCheckBox(choiceC[recordNum]);
        cCB.setBackground(Color.white);
        dCB = new JCheckBox(choiceD[recordNum]);
        dCB.setBackground(Color.white);
        //register events
        CheckBoxHandler handler = new CheckBoxHandler();
        aCB.addItemListener(handler);
        bCB.addItemListener(handler);
        cCB.addItemListener(handler);
        dCB.addItemListener(handler);
        //PROBLEM HERE!!!
        selection.addItemListener(this);
        selection.addItem("Geography Questions");
        selection.addItem("Math Questions");
        selection.addItem("Mixed Questions");
        selection.addItem("Music Questions");
        selection.addItem("Sports Questions");
        selection.addItem("TV Questions");
        addComponent(selection, 0, 0, 3, 1);
        addComponent(questionTA, 1, 0, 3, 1);
        addComponent(aCB, 2, 0, 1, 1);
        addComponent(bCB, 3, 0, 1, 1);
        addComponent(cCB, 4, 0, 1, 1);
        addComponent(dCB, 5, 0, 1, 1);
        addComponent(correctL, 2, 1, 1, 1);
        addComponent(correctNumL, 2, 2, 1, 1);
        addComponent(incorrectL, 2, 1, 1, 1);
        addComponent(incorrectNumL, 2, 2, 1, 1);
        addComponent(nextB, 6, 0, 1, 1);
        addComponent(guessB, 6, 1, 1, 1);
        addComponent(exitB, 6, 2, 1, 1);
        addComponent(youSelectL, 7, 0, 2, 1);
        addComponent(correctSelectL, 8, 0, 2, 1);
        gbConstraints.fill = GridBagConstraints.BOTH;
        setSize(420, 350);
        show();
    //ACTION PERFORMED FOR JCOMBOBOX!!
      public void actionPerformed(ActionEvent e) {
        String whichQ[] = {"Geography", "Math",
                          "Mixed", "Music", "Sports", "TV"};
        int Selected = ((JComboBox) (e.getSource())).getSelectedIndex();
        whichQselected = whichQ[Selected];
      }Thanks in advance!

    Also, you have added an item listener and implemented the method from the action listener. Above all, you did not declare your class as implementing the ActionListener interface.

  • Somewhat new to Java -- Given the task to create a GUI for a test.

    Hello there, I am a freshman student, in a Java class; the semester is coming to an end. In our project, we were assigned three parts to complete a program, that basically hands out a test. [I believe someone else has asked questions about a similar problem before, but not towards my situation.]
    The topic I chose to work with was the GUI for the program,and I'm already hitting snags. The issue is..well, probably the fact I get sort of lost when it comes to calling other methods, or just stupid mistakes to begin with. Some of the options we're faced with that we can put into the program are, for example, creating back/next buttons so the user can scroll through given questions, a progress meter [which I have no idea how to go about], Add an answer sheet in a separate window, and some others, but if possible, I will get to those when the time is right.
    Also, a small note about the majority of the code; Most of it is from a mold that was given to us as an example in class, and the 'setUp' method came from another class. [QuizDriver] As of now, the code remains untouched, but the if loops that remain empty or rather messed up is my fault, as I hit a snag and..to be honest, I am quite lost.
    As of now, my code for the GUI is this.
    import javax.swing.*;     
    import java.awt.*;     
    import java.awt.event.*;
    public class ProjGUI implements ActionListener {     
         // class constants      
         private static final int WINDOW_WIDTH = 700; 
         private static final int WINDOW_HEIGHT = 200; 
         private static final int TEXT_WIDTH = 50;   
         private static final FlowLayout LAYOUT_STYLE =     
              new FlowLayout();          
         static Test exam = new Test("Sample Exam", "Select the correct answer for each question");
         private JFrame window = new JFrame("Project GUI: Test");     
         private JTextArea results = new JTextArea("");
         int counter;
         private JLabel QTag = new JLabel("The Current Question:");     
         private JTextField QText = new JTextField(exam.displayQuestion(0));     
         private JLabel AnsTag = new JLabel("Your Answer:");
         private JTextField AnsText = new JTextField(TEXT_WIDTH);     
         private JButton SubButton = new JButton("Submit");     
         private JButton backButton = new JButton("Back");
         private JButton advButton = new JButton("Next");
         private JButton clearButton = new JButton("Reset Answer Choice");
         // constructor      
         public ProjGUI()
              // configure GUI      
              window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);     
              window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
              QText.setEditable(false);     
              QText.setBackground(Color.WHITE);     
              // register event listener      
              SubButton.addActionListener(this);     
              clearButton.addActionListener(this);
              backButton.addActionListener(this);
              advButton.addActionListener(this);
              // arrange components in GUI      
              window.getContentPane().setLayout(LAYOUT_STYLE);     
              window.getContentPane().add(QTag);     
              window.getContentPane().add(QText);     
              window.getContentPane().add(AnsTag);     
              window.getContentPane().add(AnsText);     
              window.getContentPane().add(SubButton);     
              window.getContentPane().add(backButton);
              window.getContentPane().add(clearButton);
              window.getContentPane().add(advButton);
              // display GUI      
              window.setVisible(true);     
         // actionPerformed(): run button action event handler
         public void actionPerformed(ActionEvent e) {     
              if (e.getActionCommand().equals("Submit"))
                   if(QText.equals(""))
                        QText = null;
                   if(e.getActionCommand().equals("Back"))
                        // Code for going back through the questions.
                   if(e.getActionCommand().equals("Next"))
                        // Code for advancing through the test
              // exam.answer(counter, ans);
              QText.setText(exam.displayQuestion(0+1));
              if (counter == exam.length())
                 exam.scoreExam(true);
                 results.setText("Your final score was " + exam.getScore() + "/" + exam.getMaxScore() + " points.");
                 results.setVisible(true);
              counter++;
         // main(): application entry point     
         public static void main(String[] args)
              setUp(exam);
              ProjGUI gui = new ProjGUI();     
         private static void setUp (Test t)
              TrueFalseQuestion x = new TrueFalseQuestion("The sky is blue.", "true", 2);
              t.addQuestion(x);
              x = new TrueFalseQuestion("The first FORTRAN compiler debuted in 1957", "true", 5);
              t.addQuestion(x);
              MultipleChoiceQuestion m = new MultipleChoiceQuestion("Which of the following is NOT a disk-scheduling algorithm?", "b", 4);
              m.addAnswerChoice("SSTF");
              m.addAnswerChoice("SJF");
              m.addAnswerChoice("FCFS");
              m.addAnswerChoice("All of the above are disk-scheduling algorithms");
              t.addQuestion(m);
               x = new TrueFalseQuestion("Spock was a Vulcan", "false", 3);
              t.addQuestion(x);
    }Feel free to tear it apart, as I am doing trying to figure out how exactly the buttons should work. Any feedback is appreciated.
    Edited by: Ereki on May 15, 2008 11:03 PM
    Edited by: Ereki on May 15, 2008 11:07 PM

    You may find java.awt.CardLayout and javax.swing.JProgressBar helpful.
    Generally if you have Swing-specific questions, you should ask on the Swing forum.

  • Beginners Questions about Multiple JPanels in JFrame and event handling

    I am a newbie with SWING, and even a newerbie in Event Handling. So here goes.
    I am writing a maze program. I am placing a maze JPanel (MazePanel) at the center of a JFrame, and a JPanel of buttons (ButtonPanel) on the SOUTH pane. I want the buttons to be able to re-randomize the maze, solve the maze, and also ouput statistics (for percolation theory purposes). I have the backbone all done already, I am just creating the GUI now. I am just figuring out EventHandlers and such through the tutorials, but I have a question. I am adding an ActionListener to the buttons which are on the ButtonPanel which are on JFrame (SOUTH) Panel. But who do I make the ActionListener--Basically the one doing the work when the button is pressed. Do I make the JFrame the ActionListener or the MazePanel the ActionListener. I need something which has access to the maze data (the backbone), and which can call the Maze.randomize() function. I'm trying to make a good design and not just slop too.
    Also I was wondering if I do this
    JButton.addActionListener(MazePanel), and lets say public MazePanel implments ActionListenerdoesn't adding this whole big object to another object (namely the button actionlistener) seem really inefficient? And how does something that is nested in a JPanel on JFrame x get information from something nested in another JPanel on a JFrame x.
    Basically how is the Buttons going to talk to the maze when the maze is so far away?

    I'm not an expert, but here's what I'd do....
    You already have your business logic (the Maze classes), you said. I'm assuming you have some kind of public interface to this business logic. I would create a new class like "MazeGui" that extends JFrame, and then create the GUI using this class. Add buttons and panels as needed to get it to look the way you want. Then for each button that does a specific thing, add an anonymous ActionListener class to it and put whatever code you need inside the ActionListener that accesses the business logic classes and does what it needs to.
    This is the idea, though my code is totally unchecked and won't compile:
    import deadseasquirrels.mazestuff.*;
    public class MazeGui extends JFrame {
      JPanel buttonPanel = new JPanel();
      JPanel mazePanel = new JPanel();
      JButton randomizeB = new JButton();
      JButton solveB = new JButton();
      JButton statsB = new JButton();
      // create instanc(es) of your Maze business logic class(es)
      myMaze = new MazeClass();
      // add the components to the MazeGui content pane
      Component cp = getContentPane();
      cp.add(); // this doesn't do anything, but in your code you'd add
                // all of your components to the MazeGui's contentpane
      randomizeB.addActionListener(new ActionListener {
        void actionPerformed() {
          Maze newMaze = myMaze.getRandomMazeLayout();
          mazePanel.setContents(newMaze); // this is not a real method!
                                          // it's just to give you the idea
                                          // of how to manipulate the JPanel
                                          // representing your Maze diagram,
                                          // you will probably be changing a
                                          // subcomponent of the JPanel
      solveB.addActionListener(new ActionListener {
        void actionPerformed() {
          Solution mySolution = myMaze.getSolution();
          mazePanel.setContents(mySolution); // again, this is not a real
                                             // method but it shows you how
                                             // the ActionListener can
                                             // access your GUI
      // repeat with any other buttons you need
      public static void main(String[] args) {
        MazeGui mg = new MazeGui();
        mg.setVisible(true);
        // etc...
    }

  • Question to Swing Pros: Resizing of JFrame only with visible Rectangular  ?

    Hello folks,
    quite below is the quoted text message is from Java Substance Forum: http://www.nabble.com/Removing-top-left-substance-menu-and-deactivating-Background-painting-while-Resizing-a-Frame--to17775101.html
    Is it somehow possible to use substance or another LooknFeel with custom titelbar and still keeping a hidden JFrame background resizing? So if i resize a JFrame only a rectangular should be visible not the content of the window should dynamically resize.
    That would be a general Swing question to ask in Swing forums on java.net or on java.sun.com
    Thanks
    Kirill
    ----- Original Message ----
    From: PEL <liquidc4d@...>
    To: users@...
    Sent: Wednesday, June 11, 2008 9:07:36 AM
    Subject: Re: Removing top left substance menu and deactivating Background painting while Resizing a Frame?
    2. You would notice in your screenshots that you're running Substance
    under decorated mode, but Metal (and Windows) under undecorated mode
    (title pane coming from OS). Try running your app under decorated mode in
    Metal and see if the dynamic resizing is the same.
    Ok with decorated(false) the resizing of the frame shows only a border not
    the whole background of the frame, but then i have no stunning
    Glas3DDecoration painter :D
    Is it somehow possible to use substance with 3dglass titelbar and a hidden
    jframe background resizing?

    I've never seen a way to do that in Swing normally.
    Of course, if you could run something allowing transparent windows (some newer version of Java will support it, or Skin L&F, etc), I can imagine how you might fake it by using your own decorations, and on resize, create a transparent window with a border that is your resize window, and use that to resize, and then fix the final size to the real window when done.

  • GUI Screen(JFrame) question

    Hi, I'm not sure if this is ok but I asked a J2SE question about moving from one screen to the next. I have another question; if the screens are dependent on each other i.e. data on one screen has to be displayed and triggers events on the next screen. How do I pass the data from screen to screen and after completing updates on the second screen, make the first screen visible from the second i.e. move back to the first screen?
    Can I keep the screens visible but get focus on only one? So that the other visible screens cannot be modified.

    Im not sure you exactly what your trying to do here, referring to your previous post. In most cases you will only want a single JFrame and muiple panels for various display, however essentially the concept is the same in either case. Youll have a single data model, multiple views.. You should provide methods to handle transitions of data from model to view/ view to model etc...
    When a panel or frame is shown youll typically update that gui with the current data. when a panel or frame is closed youll again most likely want to update the data from the view being closed. You can do this in event handlers. When multiple guis are open, that access the same data, youll have a bit of an interesting problem...
    A good solution is Observer/Observable.
    Where a change in data is observed by guis and guis automatically update......
    Hope this helps/

  • JFrame Design Question

    Folks,
    I'm creating a Java App using swing and had a quick question. Does it make more sense when creating a frame to extend the JFrame class or to create a new JFrame object in the constructor of the class that will provide the gui? I don't need to extend any other classed by this gui class so I'm not worried about that but rather what makes sense from a strictly OOP design viewpoint? By extending the JFrame class I should be creating a more specific JFrame class. However, I'm not really adding any functionality (technically), I'm just painting some objects. Thanks for your input.
    Andreas

    I can think of a couple options:
    1) Let IconFrame's constructor accept a Test parameter, instead of creating an interface.
    2) Make IconFrame into a modal dialog & have it store the user's selection in a variable. Then when you show the dialog, your action listener will block until the user selects an icon. Then you can call "getSelectedIcon()" to retrieve his/her selection. This would be much like the JOptionPane.showXXX() methods, and probably the cleaner solution.

  • WindowEvent and JFrame -- Probally a stupid question.

    OK,
    I have an application that uses a JFrame as its main component. So I add a window listener to that object to terminate the application when that window is closed.
    addWindowListener(new WindowAdaptor() {
        public void windowClosing(WindowEvent event) {
             System.exit(0);
    });That is pretty simple. I can even add a dialog box to that to see if they want to save the data before it closes. Here is my question. How can I add a cancel operation, so that if someone accidently closes hits the close button at the corner of the window, they can cancel the event.
    I have tried simply consuming the event, but event.consume() is a protected method of the WindowEvent class. Does anyone know how to do this?
    SUMMARY
    I want to the user to be able to cancel a WindowEvent, but the WindowEvent's consume() method is protected.
    Does anyone know an easy way of doing this?

    You can call setDefaultCloseOperation with javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE

  • Design question: Link data between JFrames

    Dear all,
    I have a design question. I have this form in a JFrame where you can set an icon as a property. To achieve this, I have a "Set" button on the form which will open a new window with all the available icons. When you click an icon, I want the "icon" frame to be closed and the selected icon to be send to the original form. This is the way I do it now:
    Interface :
    public interface IconUpdate
    public void updateIcon(Icon icon);
    }Main frame with the "Set" button:
    public class Test extends JFrame implements IconUpdate, ActionListener
        private Icon icon = null;
        public Test()
            // add button and actionlistener and that kind of stuff
        public void actionPerformed(ActionEvent e)
            if(e.getSource() == btnSetIcon)
                new IconFrame(this);
        public void updateIcon(Icon icon)
            this.icon = icon;
    }The Icon frame:
    public class IconFrame extends JFrame implements ActionListener
        IconUpdate parent = null;
        public IconFrame(IconUpdate parent)
            this.parent = parent;
            // Initialize table,"Pick" button etc.
        public void actionPerformed(ActionEvent e)
            if(e.getSource() == btnPick)
                parent.updateIcon((Icon)list.getSelectedValue());
                this.dispose();
    }Maybe I made some spelling faults, but this is the way I implement it. Now my question is: is there another way to achieve my goal? If I have to create an interface for every "popup choose dialog" in my program, I have to create many of them. I know using a combobox in the main frame is an option, but I just want to use it this way. Can anyone tell me how I can rewrite this code to make it better / more professional?
    Tx in advance!!!
    Peter

    I can think of a couple options:
    1) Let IconFrame's constructor accept a Test parameter, instead of creating an interface.
    2) Make IconFrame into a modal dialog & have it store the user's selection in a variable. Then when you show the dialog, your action listener will block until the user selects an icon. Then you can call "getSelectedIcon()" to retrieve his/her selection. This would be much like the JOptionPane.showXXX() methods, and probably the cleaner solution.

  • Multiple Components in a JFrame (simple question)

    Hello everyone, I need to put two components in my JFrame, and I have been expieriencing some odd problems. I use this to add my components...
    MyComponent mc=new MyComponent();
    MyOtherComponent me = new MyOtherComponent();
    When I try to access methods they work fine. I can use repaint() in a method and it works. BUT when I try to draw on the component using anything besides paintComponent() It gives me a java.lang.NullPointerException
    here is what I tried.....
    me.draw(getGraphics());
    //later on in the component def..
    draw(Graphics g) {
    } // I get a java.lang.NullPointerException in main
    whenever i use getGraphics() it does'nt work what am I doing wrong?

    Yup I did that and repaint does jack all. When I run the code all I see the contents of the first component. I know that the methods of the second component are being executed, but the paintComponent method is simply not running. I put a System.out.println("hi"); in and it never showed in the console..
    Anyone who can help me gets a duke, here's my second component code..
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    class MyOtherComponent extends JComponent {
    public void paintComponent(Graphics gg) {
    System.out.println("paintComponent executed!!!");
    Graphics2D g2D = (Graphics2D)gg;
    ImageIcon icon = null;
    try {icon = new ImageIcon(new URL("http://images.neopets.com/m.gif")); }
    catch(MalformedURLException e) {
    System.out.println("no"); return;
    resize(icon.getIconWidth(),icon.getIconHeight());
    Image mark = icon.getImage();
    g2D.drawImage(mark,0,0,this);
    g2D.drawString("hello folk",40,40);
    drawe(g2D);
    public void drawe(Graphics2D g2D) {
    g2D.drawLine(50,50,500,500);
    g2D.fillRect(20,20,20,20);
    repaint();
    public void line() {
    System.out.println("Hello, from MyOtherComponent");
    public void re() {
    repaint();
    System.out.println("re");     
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  • JFRAME- JPANEL question

    I have a JFrame that holds a JPanel. If that JPanel needs to contact the JFrame how do I get access to parent? I tried panelname.getParent, when I did I got a
    java.lang.ClassCastException
    Any help would be great.
    Brock

    i think you have to buid your own JPanel class lyke this:
    public class MyJPanel extends JPanel{
    JFrame parent;
    public MyJPanel(JFrame p){
    parent = p;
    // here you have access to the parent JFrame in your JPanel
    public JFrame getParentFrame(){
    return parent;
    // here you return the parent of the JPanel
    so in your JFrame class you add the JPanel like this:
    getContentPane().add(new MyJpanel(this));
    i hope it helps you. :)

Maybe you are looking for

  • Open items sorting without +/- sign

    Hi SAP GURU's, i would like to display the open items without +/- sign when i will try to sort. it is possible in edit options in while clearing. but when i will try to display the open items i can not get that option. Please send the solution as ear

  • Grant access to sub-package via hdbsql

    Hi All, I am having trouble running an hdbsql command in a shell script and wondering if anyone could help. (I am running HANA DB SP6, Revision 60) Basically, I would like to give a user REPO.READ access to the Competition sub-package as shown below.

  • Message - Data hidden by screen modifications

    I want to check the data for IT0100- SI Declaration for Belgium via PA30. I am getting the above message. What this means and how can I see the hidden data? Also, Dutch "DL" language should default for the IT0100 for country Belgium when user update

  • IDSM-2 "redundancy" in a single chassis

    I understand how IDSM-2 redundancy could work having two 6500's...but what do you do when you are doing inline vlan pairs and the IDSM-2 fails and is no longer there to bridge the vlans together? How can the switch be setup to bridge the vlans in the

  • Partners determination in Header Sales Order

    Dear Experts, As per our biz process, we've maintained 10-12 Z Account Group to determine Partners in Header>Sales Order Label. 2/3 Partners out of 12 Account Group are very essential for our reporting purpose but our users are continuously missing t