Java Swing Maximize button of JFrame disappearss - java

I have created a java frame. It contains a JSplitPane. In the right partition of the splitpane , I have put JTextPane. Now I have kept JButton somewhere on the frame. When I click the button, a new JTextArea is inserted in the JTextPane as follows
JTextArea newTextBox = new JTextArea(1, 1);
StyleConstants.setComponent(style, newTextBox);
My problem is as soon as I insert JTextArea in JTextPane, the maximize button of my JFrame disappears. Can anyone tell me what could be the reason of happning this.

Maxmize button is unavailable on two conditions:
For a Dialog, in which case only close(X) button is availabe.
If frame.setResizable(false) has been called(i.e diabled resize of window)
So applying this to your case, my best guess is that somewhere in your button's action you may have made it unresizable.

Related

Adding content to a JPanel

I'm new to Java and actually designing the GUI for an application.
My main is a JFrame with 5 buttons and 1 panel which will have the "content", for the first button for example, I've designed a Jframe which has a JTabbedPane.
Now I would like to know how can I incorporate the content from that frame to the "content" panel when clicking on the button ?
I tried to use .add but I get:
java.lang.IllegalArgumentException: adding a window to a container
(seems we can't add Jframe to Jpanel).
I also tried the setVisible way but it doesn't meet what I need since it will hide the panel completely and I will get a tiny window with the buttons.
![Jframe content][1]
![Main Jframe with buttons and Jpanel to show the jframe content][2]
The code is generated by netbeans, and I forgot to mention that I did research on adding a Jframe into another Jframe but here isn't my problem at all.
I tried by changing the Jframe by JInternalFrame but clicking on button doesn't do anything.
Button has
contentPanel.add(new GestionUtilisateur());
So basically when you click on the "Gestion Utilisateur" button for example, you get that JTabbedPane that has to appear in the content area (which is blank here)
You should not be putting JFrames inside JPanels. If you have multiple panels you would like to display, depending on something like a button, look in to LAYOUTS.
In particular, it sounds like a CardLayout would work well for your needs. CardLayouts allow you to swap which panel is displayed in a frame by bringing it to the "front" of a list of panels. This would let you display your JTabbedPane on one button click, then click another to change the content pane.
JFrame can not be added in a JPanel.
use JInternalFrame
Make and hold references to JPanels containing your content. A JFrame is really just that, it's a frame (though you can add a single component to it).
You can't add a JFrame to a JPanel. If you want multiple components to be visible use layouts such as BorderLayout, GridBag, etc. Check out some of the Swing layout tutorials here.
Content should be designed as JPanel (you can design it with drag&drop just like JFrame) but if you really have to put a JFrame to JPanel for some reason, you can do it by
myJPanel.add(myJFrame.getContentPane());
however i would suggest modification of your program.

JFrame window with sub components

So I have created this JFrame window which has a lot of items like JLabel, JComboBox JTextField etc… At the bottom it has a "next" JButton.
I want that when a user clicks the next button, everything on the screen should be removed and replaced with stuffs from other class that I have created.
I only manage to open a new JFrame window whenever I click the next button. Can somebody please tell me how to remove all items from the screen and replace them with items from another class.
Thanks. I am a newbie so please give me the easiest way possible.
This sounds like a job for CardLayout
You could create a base panel in the BorderLayout.SOUTH position of your JFrame that would have your navigation buttons and have a number of panels added to your main panel being managed by CardLayout.
See Creating Wizard Dialogs with Java Swing
While the systematic thing for it is using CardLayout, you can imitate it if you don't want to learn how to use it!!
Create a Panel, add all items except the next button to this panel. Use BorderLayout to put the panel on top of the next button in the frame.
Now when the user press the next button you remove the panel (jframe.remove(panel)). create a new JPanel and add it using the BorderLayout again on top of the next button.

Get focus on a JTextField from another panel when a CardLayout changes

hi need help on a Swing application that I am doing. I have a dialog with two panels, the first panel has a CardLayout and the second has a FlowLayout. The first layout has buttons that change the card layout and the buttonclick is entered to a specific textfield on the second panel. Every time the card layout changes, the textfield on the second panel loses its focus. How to get the focus of a specific textfield of the second panel?
when you are clicking on a button to go anohter panel
write the code inside the "actionPerformed" of that button
actionPerformed(ActionListenet al)
{
//code..
textField.requestFocus();//what the textfield u wnat
}
refer this link Setting the focus to a text field

Click through JTextArea?

I have a class that extends JPanel. On the panel I have added an JLabel for a image and a few JTextArea (setEditable, setCursor, setOpaque, setFocusable to false) for text. This class is intended to be put in a list-fashion, top to bottom. When the user clicks on one of the JPanels some actions should happen depending on the selected item (JPanel). I have added an addMouseListener which works, but it wont register clicks if the user clicks somewhere where there is text. It work if I change the JTextArea to JLabel but the reason I used JTexArea is because I want/need wordwrap.
Any suggestions? Thanks.
Consider adding same mouselistener to JTextArea Also.

JScrollPane in a popup window does not scroll up programmatically

I have a popup menu that displays dynamically created custom JPanel objects in a JPanel in a JScrollPane. The popup menu displays recommendations to the user and the topmost element is the most relevant recommendation. I am using JPopupMenu to display the window:
JPanelTemplatePopup jptep = new JPanelTemplatePopup();
JPopupMenu popup = new JPopupMenu();
popup.add(jptep);
popup.show(this, 500, 100);
The problem is, I can't make the JScrollPane scroll to the topmost element to display it first. I have tried:
.getViewPort().setViewPosition(new
Point(0,0));
.scrollRectToVisible(firstelement.getBounds());
before and after validate()s. No matter what I do, when the window pops up, the scroll pane always stays at the same place.
I have even suspected that the operations that took place before displaying the window were ignored, so I created and called a public method from the class to make the window scroll it up after being displayed. Nothing changed.
Please help,
Emre
Yeah, well, found a workaround for this.
I have narrowed the problem down to a JEditorPane that was in the custom JPanel objects. Its contents were dynamically updated by the program. While generating the objects, I was setting JEditorPanes' contents by their setText methods. Updating the caret position by setting the text to a string forced the scroll pane to scroll below.
I just inserted this to the constructor and the problem was fixed:
DefaultCaret caret = (DefaultCaret) jEditorPaneContents.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

Categories