The problem is that when I try to maximize the JFrame the frame does get maximized, but the content size is preserved.
http://i.imgur.com/7q5Yh9F.png
My main frame class:
private CardLayout layout;
private SettingsFrame settingsFrame;
private Container contentPane;
Frame frame = this;
private ApplicationFrame() {
contentPane = getContentPane();
layout = new CardLayout();
contentPane.setLayout(layout);
initializeFrame();
settingsFrame = new SettingsFrame(model);
initializeMenuBar();
initializePanels();
showPage(FIRST_PAGE);
this.addWindowStateListener(listener);
}
public SettingsFrame getSettingsFrame() {
return settingsFrame;
}
private void initializeFrame() {
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setTitle(APPLICATION_TITLE);
this.settingsFrame = new SettingsFrame(model);
this.setSize(1400, 900);
this.setLocationRelativeTo(null);
}
private void initializePanels() {
add(new Panel(), FIRST_PAGE);
add(new Panel(), SECOND_PAGE);
add(new Panel(), THIRD_PAGE);
}
I tried to make a listener, catch the maximize event and there i tried:
revalidate();
repaint();
also
invalidate();
validate();
but with no succes.
I found out that:
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
is corectly maximizing the Frame, but i cannot use that is the listener because the maximize event already happened.
You're using a CardLayout which unfortunately won't allow you to resize the JComponent. Because the CardLayout can hold/manage one or more components that share the same display space.
You should add a JPanel and set that panel's layout to the CardLayout if you wish to use it.
I strongly recommend reading the following documentation: A Visual Guide to Layout Managers
Related
When I run this program it appears as an empty window until you fullscreen, then it can be resized as you like, why is it doing this/how do I stop it?
the program is very basic just a menubar and two panels split.
public class SplitPane {
public static void main(String[] args) {
window view = new window();
}
private static class window extends JFrame {
public window() {
this.setSize(1000, 750);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//menubar is here, must lower code quantity for stack
//panels
//graph half
JPanel graphRep = new JPanel();
//Background colour - graphRep.setBackground(Color.RED);
graphRep.setVisible(true);
String graphTitle = "Textual Representation.";
Border graphBorder = BorderFactory.createTitledBorder(graphTitle);
graphRep.setBorder(graphBorder);
//text half
JPanel textRep = new JPanel();
textRep.setVisible(true);
String textTitle = "Graphical Representation.";
Border textBorder = BorderFactory.createTitledBorder(textTitle);
textRep.setBorder(textBorder);
//splitpane
JSplitPane splitPane = new JSplitPane();
splitPane.setSize(600, 750);
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerSize(10);
splitPane.setDividerLocation(250);
splitPane.setLeftComponent(graphRep);
splitPane.setRightComponent(textRep);
this.add(splitPane);
}
}
this.setVisible(true);
You are making the frame visible BEFORE you add components to the frame. The layout manager is never invoked so the size of all the components remains (0, 0) so there is nothing to paint.
The frame should be made visible AFTER all the components have been added to the frame.
And the code should be:
frame.pack();
frame.setVisible();
So each component is displayed at its proper size. Don't hardcode the size() because you don't know what the size of a users screen might be.
I have a window with a JFileChooser and a JTextArea.
The JFileChooser is in the NORTH part of the BorderLayout.
The JTextArea is in the CENTER part of the BorderLayout.
I would like to align to the left ALL my JFileChooser, but it won't move like I want and stay CENTERED.
Furthermore, I would like my JFileChooser take all the length of my window.
EDIT
Here is the main code
public class MainServer
{
public static void main(String[] args)
{
ServerBoard frame=new ServerBoard(1000, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Here is the window code
public class ServerBoard extends JFrame
{
private JButton startserver;
private JButton senddata;
private JButton sendgps;
private JTextArea messagearea;
public ServerBoard(int l, int h)
{
super("ServerBoard");
this.initialize();
this.setSize(l,h);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void initialize()
{
// Define a panel
Container c=this.getContentPane();
this.messagearea=new JTextArea(40,60);
c.add(this.createNorth(), BorderLayout.NORTH);
c.add(messagearea, BorderLayout.CENTER);
}
public JPanel createNorth()
{
JPanel panelnorth=new JPanel();
JToolBar toolbarnorth=new JToolBar();
panelnorth.add(toolbarnorth);
this.startserver=new JButton("START SERVER");
startserver.addActionListener(new ServerBoardListener());
toolbarnorth.add(startserver);
this.senddata=new JButton("SEND DATA");
senddata.addActionListener(new ServerBoardListener());
toolbarnorth.add(senddata);
this.sendgps=new JButton("SEND GPS FRAME");
sendgps.addActionListener(new ServerBoardListener());
toolbarnorth.add(sendgps);
return panelnorth;
}
}
Here is my window
I really really want to use this JFileChooser. Can you help me please ?
In advance thank you a lot for the answers.
Simply nest your JPanels. Create a new JPanel, say called northPanel, that uses a BorderLayout, and add it to the main window in the BorderLayout.NORTH position, and then add your JFIleChooser to this northPanel JPanel in its BorderLayout.WEST position.
Option 2: give the northPanel a BoxLayout that is oriented along the line axis, add the JFileChooser, and add glue.
I have tried several ways, but still havent found the solution. I have a jgraph in a frame and I want to add a Jbutton in that frame also in a specific location. However I only get one of them when i run the program, because they expand to the whole window. Any ideas how to fix this?
Thanks in advance.
public class GUIquery extends JFrame {
JFrame frame;
static JGraph jgraph;
final mxGraph graph = new mxGraph();
final mxGraphComponent graphComponent = new mxGraphComponent(graph);
public GUIquery() {
super("Test");
GraphD();
imgbtn();
}
public void GraphD() {
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
........
}catch {
........
} finally {
graph.getModel().endUpdate();
}
getContentPane().add(graphComponent);
}
public void imgbtn() {
JPanel jpanel = new JPanel();
jpanel.setSize(100, 100);
jpanel.setLocation(1200, 60);
JButton imgbtn = new JButton("Export as Image");
imgbtn.setSize(100, 100);
imgbtn.setLocation(1200, 60);
jpanel.add(imgbtn);
add(jpanel);
}
public static void main(String[] args) {
GUIquery frame = new GUIquery();
frame.setLayout(null);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
}
Don't use null layouts. They inevitably result in trouble.
From your code snippet it is impossible to tell where you want them to be relative to each other, the following puts the button below the graph.
The content pane uses BorderLayout by default. For BorderLayout, you need to use place components at different positions:
// the default position, but it does not hurt to be explicit
add(graph, BorderLayout.CENTER);
...
// and the panel
add(jpanel, BorderLayout.SOUTH);
If the positioning is not what you want, take a look at the visual guide to layout managers to pick the layout manager that suits your needs best.
In the button panel the setLocation() and setSize() calls are useless. The layout manager of the panel is responsible for setting the button's bounds. If the default FlowLayout is not what you want for it, use the guide to pick another for the panel too.
I am trying to place a JPanel on top of another JPanel which contains a JTextArea and a button and i want to the upper apnel to be transparent. I have tried it by making the setOpaque(false) of the upper panel. but it is not working. Can anyone help me to get through this? Thanks in advance!
public class JpanelTest extends JPanel
{
public JpanelTest()
{
super();
onInit();
}
private void onInit()
{
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JTextArea(100,100),BorderLayout.CENTER);
panel.add(new JButton("submit"),BorderLayout.SOUTH);
JPanel glass = new JPanel();
glass.setOpaque(false);
add(panel,BorderLayout.CENTER);
add(glass,BorderLayout.CENTER);
setVisible(true);
}
public static void main(String args[])
{
new JpanelTest();
}
}
Indeed, it would be useful to tell the reason why you want panels one over another.
Starting with your code, and changing it a lot, I got it to work, but it might not do what you expect...
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame
{
public Test()
{
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 200);
onInit();
setVisible(true);
}
private void onInit()
{
JLayeredPane lp = getLayeredPane();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JTextArea(), BorderLayout.CENTER);
panel.add(new JButton("Submit"), BorderLayout.SOUTH);
panel.setSize(300, 150); // Size is needed here, as there is no layout in lp
JPanel glass = new JPanel();
glass.setOpaque(false); // Set to true to see it
glass.setBackground(Color.GREEN);
glass.setSize(300, 150);
glass.setLocation(10, 10);
lp.add(panel, Integer.valueOf(1));
lp.add(glass, Integer.valueOf(2));
}
public static void main(String args[])
{
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Test();
}
});
}
}
If totally transparent, well, it is like it isn't here! When opaque, it just covers some of the GUI, but doesn't prevent mouse clicks, for example.
1) there are a few ways, there no issue to put JPanel, with covering full JFrames/JPanel area or only part of Rectangle / Dimension that returns JFrames/JPanel
use JLayer(Java7) based on JXLayer (Java6)
use GlassPane
use JViewport
use OverlayLayout
use transucent JDialog / JWindow
2) everything depends of if you want to protect against mouse and key events from the top layer to bottom, or not (to avoiding redispatch events from - to and vice versa)
Check out this tutorial on using Swing Root Panes.
The glass pane is useful when you want to be able to catch events or paint over an area that already contains one or more components. For example, you can deactivate mouse events for a multi-component region by having the glass pane intercept the events. Or you can display an image over multiple components using the glass pane.
I have a layout with a main panel whose content is decided by me throught buttons in this way:
public void actionPerformed(ActionEvent e) {
mainPanel.removeAll(); //removing all current panel components
if(e.getActionCommand().equals("content1")){
mainPanel = new Content1Panel();
add(mainPanel,BorderLayout.CENTER);
validate();
}else if(e.getActionCommand().equals("content2")){
mainPanel = new Content2Panel();
add(mainPanel,BorderLayout.CENTER);
validate();
}
}
now, if I minimize the frame and then bring it to front I find all main panel's components disappeared! Anyway, when I hover on them with the mouse components show again.
What is wrong?
for JFrame / JDialog / JWindow
to use
validate(); (revalidate in Java7 )
repaint();
example about standard rulles
use CardLayout instead of remove and then add the JPanel to the container