3

Why i am getting the errors in the abstract class.Earlier i thought there may be a class of the same so i have created a new package and created a class test but still i am getting the same error.Any Solutions to it.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newpackage;

/**
 *
 * @author SriHari
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author SriHari
 */
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

/**
 *
 * @author David
 */
public class Test {

    public Test() {
        createAndShowGui();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel mainHolderPanel = new JPanel(new GridLayout(2, 2));

        for (int i = 0; i < 4; i++) {
            final int num = i;
            OmniPanel op = new OmniPanel(mainHolderPanel, frame) {
                @Override
                public JPanel createPanel() {
                    JPanel p = createSimplePanelInterface();
                    p.add(new JLabel("Panel " + (num + 1)));
                    return p;
                }

                @Override
                void toPanel() {
                    super.toPanel();
                    System.out.println("Frame requested to be brought to panel");
                }
            };
            mainHolderPanel.add(op.getPanel());
        }

        frame.add(mainHolderPanel);

        frame.pack();
        frame.setVisible(true);
    }
}

abstract class OmniPanel {

    protected JFrame frame;
    protected JPanel panel;
    boolean maximized = false;
    private final JComponent owner;
    private final JFrame ownerFrame;

    public OmniPanel(JComponent owner, JFrame con) {
        this.owner = owner;
        initOmniPanel();
        this.ownerFrame = con;
    }

    private void initOmniPanel() {
        panel = createPanel();
        createFrame();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowDeiconified(WindowEvent we) {
                super.windowDeiconified(we);
                toPanel();
            }
        });
    }

    public JPanel getPanel() {
        return panel;
    }

    public JFrame getFrame() {
        return frame;
    }

    public boolean goFrame() {
        frame.add(panel);
        frame.pack();
        frame.setState(JFrame.ICONIFIED);
        frame.setVisible(true);
        return true;
    }

    protected void createFrame() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    void toPanel() {
        frame.remove(panel);
        frame.dispose();
        owner.add(panel);
        owner.revalidate();
        owner.repaint();
    }

    public JPanel createSimplePanelInterface() {
        JPanel p = new JPanel();
        JButton close = new JButton("X");
        JButton minimize = new JButton("_");
        JButton maximize = new JButton("|-|");
        p.add(close);
        p.add(minimize);
        p.add(maximize);
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    maximized = false;
                    ownerFrame.setGlassPane(new JComponent() {
                    });
                    ownerFrame.revalidate();
                    ownerFrame.repaint();
                } else {
                    removePanelFromOwner();
                    getFrame().dispose();
                }
            }
        });
        minimize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    maximized = false;
                    ownerFrame.setGlassPane(new JComponent() {
                    });
                    owner.add(panel);
                    owner.revalidate();
                    owner.repaint();
                    ownerFrame.revalidate();
                    ownerFrame.repaint();
                } else {
                    removePanelFromOwner();
                    goFrame();
                }
            }
        });
        maximize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    return;
                }
                maximized = true;
                removePanelFromOwner();
                ownerFrame.setGlassPane(panel);
                ownerFrame.revalidate();
                ownerFrame.repaint();
                panel.setVisible(true);//or glasspane wont h=show this has been a java glitch of sorts for a while
            }
        });
        p.setBorder(new LineBorder(Color.black));
        return p;
    }

    private void removePanelFromOwner() {
        owner.remove(getPanel());
        owner.revalidate();
        owner.repaint();
    }

    abstract JPanel createPanel();
}
  • 2
    SHow your curretn code. Do you have SSCCE? – StanislavL Feb 11 '13 at 06:51
  • 1
    `JPanel` is not like `JFrame` and thus it is nearly impossible to get a `JPanel` minimized to taskbar like a `JFrame` (i.e an icon is shown representing minimzed *window* in taskbar). You may need to use multiple `JWindow`s or `JFrame`s to achieve the correct effect... – David Kroukamp Feb 11 '13 at 07:37
  • 1
    Wait wait wait.... It just hit me... You can use a `JDesktopPane` and 4 `JInternalFrame`s which by default gives you what you want (minimized maximize and close features are built into the `JInteralFrame`):) See [here](http://stackoverflow.com/questions/13814704/how-to-change-jdesktoppane-default-backgroud-image/13815721#13815721) for an example. – David Kroukamp Feb 11 '13 at 07:44
  • I answered a similar question which needed the `JInternalFrame` to look like a `JPanel` see [here](http://stackoverflow.com/questions/14600332/how-to-make-jinternalframe-fill-the-container-and-disable-the-dragging-feature/14603306#14603306). Now you would simply add your own buttons which would be like wrappers for the `JInternalFrame`s minimized maximized and close buttons i.e see [here](http://stackoverflow.com/questions/6476525/programmatically-minimize-a-jinternalframe) which shows how to programatically minimize `JInternalFrame` +1 @trashgod. – David Kroukamp Feb 11 '13 at 07:53
  • @sukant not possible with JInternalFrame sorry forgot to mention it...You would need to use my initial idea (multiple JFrames/Jwindow), thinking more there are a few hiccups in my initial idea too. But why do you need this type of effect? I have never seen it before and cant think of a real advantage of it? The code created will most likely not follow the best practice of using a single JFrame per app. – David Kroukamp Feb 11 '13 at 09:01

1 Answers1

3

Hope this will help you with the idea:

Before anything:

enter image description here

After Panel 1 button _ is clicked:

enter image description here

When 2nd icon of JFrame in taskbar which contains Panel 1 is deiconified:

enter image description here

UPDATE

New code also has maximize function:

enter image description here

after pressing |-| button on panel 3:

enter image description here

after pressing _ button on maximized panel 3

enter image description here panel 3:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

/**
 *
 * @author David
 */
public class Test {

    public Test() {
        createAndShowGui();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel mainHolderPanel = new JPanel(new GridLayout(2, 2));

        for (int i = 0; i < 4; i++) {
            final int num = i;
            OmniPanel op = new OmniPanel(mainHolderPanel, frame) {
                @Override
                public JPanel createPanel() {
                    JPanel p = createSimplePanelInterface();
                    p.add(new JLabel("Panel " + (num + 1)));
                    return p;
                }

                @Override
                void toPanel() {
                    super.toPanel();
                    System.out.println("Frame requested to be brought to panel");
                }
            };
            mainHolderPanel.add(op.getPanel());
        }

        frame.add(mainHolderPanel);

        frame.pack();
        frame.setVisible(true);
    }
}

abstract class OmniPanel {

    protected JFrame frame;
    protected JPanel panel;
    boolean maximized = false;
    private final JComponent owner;
    private final JFrame ownerFrame;

    public OmniPanel(JComponent owner, JFrame con) {
        this.owner = owner;
        initOmniPanel();
        this.ownerFrame = con;
    }

    private void initOmniPanel() {
        panel = createPanel();
        createFrame();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowDeiconified(WindowEvent we) {
                super.windowDeiconified(we);
                toPanel();
            }
        });
    }

    public JPanel getPanel() {
        return panel;
    }

    public JFrame getFrame() {
        return frame;
    }

    public boolean goFrame() {
        frame.add(panel);
        frame.pack();
        frame.setState(JFrame.ICONIFIED);
        frame.setVisible(true);
        return true;
    }

    protected void createFrame() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    void toPanel() {
        frame.remove(panel);
        frame.dispose();
        owner.add(panel);
        owner.revalidate();
        owner.repaint();
    }

    public JPanel createSimplePanelInterface() {
        JPanel p = new JPanel();
        JButton close = new JButton("X");
        JButton minimize = new JButton("_");
        JButton maximize = new JButton("|-|");
        p.add(close);
        p.add(minimize);
        p.add(maximize);
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    maximized = false;
                    ownerFrame.setGlassPane(new JComponent() {
                    });
                    ownerFrame.revalidate();
                    ownerFrame.repaint();
                } else {
                    removePanelFromOwner();
                    getFrame().dispose();
                }
            }
        });
        minimize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    maximized = false;
                    ownerFrame.setGlassPane(new JComponent() {
                    });
                    owner.add(panel);
                    owner.revalidate();
                    owner.repaint();
                    ownerFrame.revalidate();
                    ownerFrame.repaint();
                } else {
                    removePanelFromOwner();
                    goFrame();
                }
            }
        });
        maximize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    return;
                }
                maximized = true;
                removePanelFromOwner();
                ownerFrame.setGlassPane(panel);
                ownerFrame.revalidate();
                ownerFrame.repaint();
                panel.setVisible(true);//or glasspane wont h=show this has been a java glitch of sorts for a while
            }
        });
        p.setBorder(new LineBorder(Color.black));
        return p;
    }

    private void removePanelFromOwner() {
        owner.remove(getPanel());
        owner.revalidate();
        owner.repaint();
    }

    abstract JPanel createPanel();
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 1
    thanks a lot.You are a awesome guy,highly talented.This is what i wanted.Thanks again. –  Feb 11 '13 at 10:50
  • Glad to be of help :). See my edited post. As for my email I am more often on this site than my emails ;) (but my email is in my profile) but I deleted that email so yeah...) and there are many here on SO as good and even better than me... But if you ever need remember use @trashgod to notify them directly through a comment for example. – David Kroukamp Feb 11 '13 at 10:56
  • lol just an example I made you could use @DavidKroukamp which will than notify me that a comment was directed at me regardless if I have posted a comment/answer on the question – David Kroukamp Feb 11 '13 at 11:01
  • why i can not create more panels? –  Feb 11 '13 at 12:02
  • 1
    Not sure what you mean. Works for me simply change the 4 in the for loop to something like 5 or 6.... though it would also be better to change layout of `mainHolderPanel` accordingly from `GridLayout` (or make more rows/cols for GridLayout) alternatively use default `JPanel` `FlowLayout` – David Kroukamp Feb 11 '13 at 12:51
  • i have added a Button max.If you hit it then it will maximize the panel and taking the size of the frame.I have checked with all the four panels and it works.But if you minimize once and after if you try to maximize it then it does not work public boolean maxFrame() { frame.add(panel); //frame.pack(); frame.setState(JFrame.MAXIMIZED_BOTH); frame.setSize(frame.getSize()); System.out.println(frame.getSize()); frame.setVisible(true); return true; } –  Feb 12 '13 at 08:49
  • this is listener method max.addActionListener(new ActionListener() { //max ActionListener @Override public void actionPerformed(ActionEvent ae) { removePanelFromOwner(); maxFrame(); } } ); –  Feb 12 '13 at 08:50
  • 1
    Okay you are on the right track... but instead of adding panel to frame etc simple maximize the frame, remove the panel you want and add it as a `glasspane` of `JFrame` (via `JFrame#setGlassPane(...)`), than remove `glasspane` (set a empty transparent `JPanel`) and re-add panel when minimized to its owner... – David Kroukamp Feb 12 '13 at 09:34
  • i tried before the way you said like not adding panel to frame but i am not getting the buttons in it , i am getting a maximized window without any buttons –  Feb 12 '13 at 09:39
  • @sukant updated code again as it wasnt working if I closed during maximized state... and also was missing some brackets on if statement in maximize button... and its a pleasure...Also note you do not have to maximize the JFrame, the user can do that the maximize button would than make glasspane fit the entire panel – David Kroukamp Feb 12 '13 at 09:55
  • the latest code is not compiling.In Abstrct class Omnipanel its showing duplicate class test.omnipanel.My package is test. –  Feb 12 '13 at 10:01
  • @sukant It compiles fine... so do you have another/duplicate class called `OmniPanel.java` in your package heirarchery? Make sure you dont, or replace the code in that class with the new code. I would never give give errornous code. – David Kroukamp Feb 12 '13 at 10:05
  • no,this was not the first time i am getting .earlier also when i tried to make 4 panels i got the same error.so i asked you yesterday why i cant add more panels –  Feb 12 '13 at 10:05
  • @sukant You are doing something wrong just create a new project and start a fresh... Further than that I cannot help as the code compiles fine thus nothing is wrong with the code, more likely your projects code/structure is going haywire – David Kroukamp Feb 12 '13 at 10:06
  • I am not blaming you neither i say you have posted errornous code.I am just asking for your help.Sorry if made you sad. –  Feb 12 '13 at 10:12
  • i have uploaded the code.Can you please tell me where i did a mistake.Thanks in advance. –  Feb 12 '13 at 10:24
  • It works for me... Go check your package (outside of the IDE) make sure no `OmniPanel.java` file is lying around in your package. BTW what is the exact full error – David Kroukamp Feb 12 '13 at 10:29
  • i will go mad,now my previous codes are not working(those codes without maxmize buttons) –  Feb 12 '13 at 10:43
  • @sukant Have you completely recreated a new project with a single class called Test.java? And than take my code copy and paste it so as to completely replace code generated in Test.java.. Obviously you'd need to keep the `package whateverpackagename;` statement in Test.java or else an error will occur – David Kroukamp Feb 12 '13 at 11:02
  • my eclipse is not running.When i double on eclipse it ,a window comes up and disappers.Do you have any solution.When i run a program in command promt it shows this message"Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/Object" –  Feb 12 '13 at 11:18
  • See a similar answer here which may help: http://stackoverflow.com/questions/11526948/whats-wrong-with-my-java-code/11527396#11527396 – David Kroukamp Feb 12 '13 at 12:46
  • thanks for givving reply but this does not help me.As i said earlier my eclipse was not working so i had some research and found that eclipse is looking for C:\Java\jdk1.6.0_33\jre\bin\javaw .But in my system this path was not same so i copied from C:\Program Files\Java\jdk1.6.0_33\jre\bin\javaw and pasted as C:\Java\jdk1.6.0_33\jre\bin .Now eclipse runs but i can not run a single program.Even it is showing error in public class su { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } } –  Feb 13 '13 at 04:33
  • HI ,in the above code what do i have to make changes so that if i hit the minimize button then it will be minimized to parent frame not to the task bar –  Feb 14 '13 at 06:53