I'm trying to reuse a JPanel inside a JDialog instead of replicating another JPanel that has the exact features. I tried removing and adding the component to the JDialog, but it's not working as I expected. What is the recommended approach to this issue?
write a class "myFeaturesJPanel" that extends jPanel having the needed features. then just add a own instance to your popUpPanel and your original panel
Related
I am switching on button click to new JPanel with the use of:
JPanel newP = new ProjectPage();
contentPane.revalidate();
setContentPane(newP);
Where ProjectPage is:
public class ProjectPage extends JPanel
But how I create button in my new ProjectPage class that will take me back to my original panel?
My main screen class declared like so:
public class MainScreen extends JFrame
Use a CardLayout, as shown here.
See How to Use CardLayout for details.
As more general advice, do not extend JPanel or JFrame, but simply keep references to them & build them as needed for each use. Part of this problem seems to be scope - the relevant panels are not 'visible' to the calling code. Keeping references to them in the main class is an easy solution to that.
If you want to go back to the original JPanel, you have to keep a reference of it somewhere in your class by adding a field private JPanel oldPanel;
When you create your new panel, get the old Panel and save it in that field like:
oldPanel = getContentPane();
JPanel newP = new ProjectPage();
contentPane.revalidate();
setContentPane(newP);
and when you want to go back to your original panel, you do:
setContentPane(oldPanel);
I have been working on a series of JPanel and I wanted to add a JFrame from another class to one of my panels. Can this be done.
No, you cannot add any Window to another component, doing so results in a RuntimeException. What you can do is add the contentPane from the JFrame to your JPanel.
I am creating a Java applet, and I am trying to add a JFrame to it. I am calling:
add(new MyJFrameSubclass());
But when I launch the application, my JFrame isn't shown until I resize the window.
This may be too elementary of a suggestion, but sticking in a validate() or repaint() can sometimes solve problems that seem complicated.
A simple fix is to add a frame.show(); after you add your JFrame, I had the same issue and this seemed to help, the frame being the name of your JFrame.
JFrame and JApplet are both top-level containers. Instead of trying to have both containers present at once, put your content in a lightweight container such as JPanel; then add that JPanel to either a JFrame (for local use) or a JApplet (for the browser). This example shows the approach.
first of all, this is more or less my first GUI and ive learned Java for not more then a week, so it might contain some serious programming errors.
What i have right now is:
Buttons and labels are part of OptionPanel and are on the left, DrawingPanel is about 5x5 px in size and is on the right.
What I am trying to do is a simple test, to get me more familiar with the GUI. The rectangle should be movable and re-sizeable by the user when clicking the respective buttons:
http://www.upload.ee/image/612005/JFrame2.jpg
Right now i have:
JFrame MainFrame - Makes JFrame (Not using the setSize function. using .pack() instead. not sure about it)
JPanel MergedPanel - FlowLayout - Adds JPanel OptionsPanel and JPanel DrawingPanel together and gets injected to JFrame MainFrame
JPanel DrawPanel - This JPanel is responsible of drawing the rectangle.
JPanel OptionPanel - FlowLayout - This JPanel is responsible of the buttons.
Help please.
You should never call setSize() in your code. In Java, you use layout managers to do the layout (read that tutorial).
Subclassing JPanel to implement different parts of which the UI is composed is a good practice, but should not be overdone (it's fine to have an UI class that adds 3 other plain JPanel instances to itself for layout purposes).
Check out MiG Layout : one can make pretty easily java layouts with that.
I'm programming my very first GUI app in Java using the Swing framework. I've coded a basic login system using the JTextField, JPasswordField, and JButton classes. Now, I'm writing the actionPerformed method for the button, which I want to remove these items as they are no longer necessary, but I am unsure as to the best way of achieving this.
I've considered using the setVisible method of these components, which seems to work, but I'm sure that's not the preferred way of doing it. Sorry if this is a bit of a dumb question..
Have your login dialog separated from your main window. When you finished with the login, just hide the login dialog.
You can also save your text fields and buttons into a class field, and later call remove(Component) for each one.
Generally, you would want to be able to do this in one line of code. So, you should consider wrapping the different things you'd like to show or hide in a JPanel. Then, you can dynamically show or hide the JPanels.
You could have a login JPanel, which is set up and displayed and once the user's password is verified, you can show your application JPanel. This can easily be done using a CardLayout.
It treats each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards.
Here is a tutorial.
Using a CardLayout, your code could look something like this:
instance variables:
static final String LOGINPANEL = "LOGINPANEL";
static final String MAINPANEL = "MAINPANEL";
JPanel cards;
where your panels are created:
JPanel loginPanel = new JPanel();
//add your stuff to the login panel
JPanel mainPanel = new JPanel();
//add your stuff to the main panel
cards = new JPanel(new CardLayout());
cards.add(loginPanel, LOGINPANEL);
cards.add(mainPanel, MAINPANEL);
then when your password is verified, in the AWT thread, you can do this:
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, MAINPANEL);
I agree with JPanel suggestion. Add the log-in components to a JPanel, then hide the JPanel once there is a log-in.
I will suggest you to use a JDialog for your login. After a successful login you just need to call “dialog.dispose()” and then load your interface.
If the application you are building need to display several windows you should consider to use a JDesktopPane (http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JDesktopPane.html)