I'm working right now on a project, and I can't find a solution for my problem.
So here is my problem: I have a JFrame, then I add a container JPanel, and I add 2 other JPanel to this container panel , first panel (InputPanel) is for user input, second panel (Board) for displaying the specified algorithm based on the user input.
But the displayed algorithm is too large, so I thought I will add a JScrollPane to the DisplayPanel, but it didn't worked like I thought it would have. Here is a picture, the red rectangle is the area what changed after I added the JScollPane:
Notice that at my Board class I override the paintComponent(Graphics g) to draw the algorithm.
My code in the main frame:
container = new JPanel();
container.setLayout(new BorderLayout());
board = new Board();
container.add(board, BorderLayout.CENTER);
inputPanel = new InputPanel(board);
container.add(inputPanel, BorderLayout.SOUTH);
pane = new JScrollPane(board);
pane.setViewportView(board);
pane.setPreferredSize(new Dimension(700, 0));
container.add(pane, BorderLayout.WEST);
add(container);
My initial plan was to add a horizontal scrollpane to the Board panel. Can somebody post an example code, or point out for my problem pls?
You added the board twice to container. Add the JScrollPane to the center of container, and continue to pass board to the constructor of your JScrollPane. Don't add the board individually to the container if you want to add it via a JScrollPane.
Related
I recently found out that you can put a JPanel inside another JPanel, I tried it and it didn't quite do what I expected.I'm trying to have one button in the center and one button on the bottom right of the panel, I'm using a BorderLayout for all my panels.When I tried using two panels inside the center panel, the two panels were on the same line, but I've coded that one needed to be on the top and one on the bottom.Does anyone know how I could resolve this?
Sorry for not adding the code in the place, wasn't sure and I had already deleted it so I quickly made this, it is the same code I used in my project.
My code:
JFrame frame = new JFrame();
JButton btn = new JButton();
JPanel center = new JPanel();
JPanel top = new JPanel();
JPanel bot = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bot.add(btn);
center.add(top, BorderLayout.PAGE_START);
center.add(bot, BorderLayout.PAGE_END);
frame.add(center, BorderLayout.CENTER);
When I use this code, "bot" is going to be displayed on the right of "top".
I think it has to do with the layout of "center" but I'm not sure.
Set the LayoutManager of center to BorderLayout, with this method:
center.setLayout(new BorderLayout());
I want to add two jPanels to a JFrame side by side. the two boxes are jpanels and the outer box is a jframe
I have these lines of code. I have one class called seatinPanel that extends JPanel and inside this class I have a constructor and one method called utilityButtons that return a JPanel object. I want the utilityButtons JPanel to be on the right side. the code I have here only displays the utillityButtons JPanel when it runs.
public guiCreator()
{
setTitle("Passenger Seats");
//setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
seatingPanel seatingPanel1 = new seatingPanel();//need to declare it here separately so we can add the utilityButtons
contentPane.add(seatingPanel1); //adding the seats
contentPane.add(seatingPanel1.utilityButtons());//adding the utility buttons
pack();//Causes this Window to be sized to fit the preferred size and layouts of its subcomponents
setVisible(true);
}
The most flexible LayoutManager I would recommend is BoxLayout.
You can do the following :
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//panel1.set[Preferred/Maximum/Minimum]Size()
container.add(panel1);
container.add(panel2);
then add container to object to your frame component.
You need to read up on and learn about the layout managers that Swing has to offer. In your situation it will help to know that a JFrame's contentPane uses BorderLayout by default and you can add your larger center JPanel BorderLayout.CENTER and the other JPanel BorderLayout.EAST. More can be found here: Laying out Components in a Container
Edit 1
Andrew Thompson has already shown you a bit on layout managers in his code in your previous post here: why are my buttons not showing up?. Again, please read the tutorial to understand them better.
I am attempting to place two panels over the top of another two panels, so the positions match exactly. This will allow me to then paint over the top of the previous(bottom) panels. The reason for doing this, is that I have implemented a "drag a rectangle" zoom function, so on the panel the user will drag the mouse to draw a rectangle, and then the panel will become filled with the contents of that rectangle. However, I have now decided I would like include a visual representation of the rectangle as the user drags it.
Since I (assume) I cannot draw the rectangle on the bottom panel, since it already has a complicated paint on it anyway, I figured I would be able to draw it onto another panel that I would place on top. (If there is any easier way please shout!)
I am happy I have been able to get the panels into the correct position, using a BorderLayout/GridLayout combo, without the need for absolute positing, however the setOpaque method is not working on any of the three panels I have added. Does anyone know why this is?
Many thanks in advance, code below:
public void initiate() {
JPanel panel1 = new JPanel();
mandPanel mandDisplay = new mandPanel();
juliaPanel juliaDisplay = new juliaPanel();
JPanel detailsPanel = new JPanel();
JPanel bottomDetails = new JPanel();
JPanel panelForDisplays = new JPanel();
JPanel listenerPanelHolder = new JPanel();
JPanel listenerPanel = new JPanel();
JPanel blankPanel = new JPanel();
panelForDisplays.setLayout(new GridLayout(1,2));
detailsPanel.setLayout(new FlowLayout());
bottomDetails.setLayout(new FlowLayout());
panel1.setLayout(new BorderLayout());
panelForDisplays.add(mandDisplay);
panelForDisplays.add(juliaDisplay);
listenerPanelHolder.setOpaque(false);
listenerPanel.setOpaque(false);
blankPanel.setOpaque(false);
listenerPanelHolder.setLayout(new GridLayout(1,2));
listenerPanelHolder.add(listenerPanel);
listenerPanelHolder.add(blankPanel);
panel1.add(detailsPanel,BorderLayout.NORTH);
panel1.add(panelForDisplays,BorderLayout.CENTER);
panel1.add(listenerPanel,BorderLayout.CENTER);
panel1.add(bottomDetails, BorderLayout.SOUTH);
setContentPane(panel1);
setSize(width*2, height);
setResizable(false);
this.addWindowListener(new onClose(savedJulias));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
PS: A lot of code is omitted to avoid mess, but if anyone thinks they could solve with more code please say! Thanks :)
First off, adding two components at the BorderLayout.CENTER position replaces the first component with the 2nd, and so the 2nd doesn't cover the 1st, it displaces it. As per the BorderLayout API:
Each region may contain no more than one component,...
But having said that, as I understand your problem in greater detail, I think that your best solution is not to use JLayeredPane or to overlay two JPanels. The best solution is in fact to draw directly on the image-displaying JPanel. This is not hard to do, and shouldn't over complicate your code. For example.
You can use JLayeredPane to place one component to the top of the other. You can check this
I want to add two jPanels to a JFrame side by side. the two boxes are jpanels and the outer box is a jframe
I have these lines of code. I have one class called seatinPanel that extends JPanel and inside this class I have a constructor and one method called utilityButtons that return a JPanel object. I want the utilityButtons JPanel to be on the right side. the code I have here only displays the utillityButtons JPanel when it runs.
public guiCreator()
{
setTitle("Passenger Seats");
//setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
seatingPanel seatingPanel1 = new seatingPanel();//need to declare it here separately so we can add the utilityButtons
contentPane.add(seatingPanel1); //adding the seats
contentPane.add(seatingPanel1.utilityButtons());//adding the utility buttons
pack();//Causes this Window to be sized to fit the preferred size and layouts of its subcomponents
setVisible(true);
}
The most flexible LayoutManager I would recommend is BoxLayout.
You can do the following :
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//panel1.set[Preferred/Maximum/Minimum]Size()
container.add(panel1);
container.add(panel2);
then add container to object to your frame component.
You need to read up on and learn about the layout managers that Swing has to offer. In your situation it will help to know that a JFrame's contentPane uses BorderLayout by default and you can add your larger center JPanel BorderLayout.CENTER and the other JPanel BorderLayout.EAST. More can be found here: Laying out Components in a Container
Edit 1
Andrew Thompson has already shown you a bit on layout managers in his code in your previous post here: why are my buttons not showing up?. Again, please read the tutorial to understand them better.
I have a simple swing application which consists of a JLabel and three buttons. The three buttons are in their own JPanel which is in a JFrame along with the JLabel. The JPanel uses flowlayout manager to arrange the buttons horizontally and the JFrame uses the BorderLayout manager to arrange the JLabel and JPanel vertically.
My problem is when I launch the application, during the course of use the text on one of the buttons changes which increases its width. However, the window doesn't resize to accomdate this and one of the buttons disappears. I thought about calling pack() again, but the JFrame is a local variable in my constructor, also, I shouldn't have to tell my program to resize, right? I haven't been able to find anything on google or here to help me but there must be a simple solution, what am I missing? Code is below.
playButton = new JButton("Play");
pauseButton = new JButton("Pause");
stopButton = new JButton("Stop");
curTrackLabel = new JLabel("No Track Selected");
JFrame myFrame = new JFrame("MediaPlayer");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("MediaPlayer");
myFrame.setLocation(400,300);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
myFrame.add(topPanel);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(playButton);
buttonPanel.add(pauseButton);
buttonPanel.add(stopButton);
topPanel.add(buttonPanel, BorderLayout.CENTER);
topPanel.add(curTrackLabel, BorderLayout.NORTH);
playButton.addActionListener(new playButtonHandler());
pauseButton.addActionListener(new pauseButtonHandler());
stopButton.addActionListener(new stopButtonHandler());
myFrame.pack();
myFrame.setVisible(true);
Maybe try
((JFrame)myButton.getTopLevelAncestor()).pack();
Where myButton is the button whose text is modified during execution.
As with learning any GUI software, experimentation is best. Try messing with BorderLayouts with nested JPanels.
Ultimately, you use JPanel with a BorderLayout (Flow Layout is OK but really when resizing the window, it epically fails). See http://download.oracle.com/javase/tutorial/uiswing/layout/border.html to learn more about BorderLayouts.
Now for your layout scheme it should be something along the lines of:
Top Level Container: JFrame
JFrame contains a JPanel (Call this
JPanel 1) with a BorderLayout.
The three buttons should be in a
SEPARATE jPanel (JPanel 2). JPanel
1 should add the three buttons as
BorderLayout.CENTER. In this way,
the window will resize if the button
changes its width and/or hright.
The JLabel should be added as
BorderLayout.LINE_START.
The tutorial at: http://download.oracle.com/javase/tutorial/uiswing/layout/border.html should help you with this. But in general, use the following:
Use JPanel and nest JPanels as necessary
BorderLayout.CENTER will accomodate size changes---this is the key! (Experiment with this)
JFrame should only be used as a top level container (for more complex GUIs, this is true).
If you require more flexibility, check out JGoodies: http://www.jgoodies.com/ . This is more along the lines of creating forms.