Scrollpane scrollbars to entire Jframe when resizing/adding components - java

I edited my project using only swing components and a layout (not null).So now i want to add scrollbars to all frame not only at a pictrure.Resizing and moving the scrollbars and showing components under when you scroll down.The difficult is that the frame has many components and users can add pictures and admin can add labels or other components,so i don't know what to redraw when the frame is resized.Redraw everything i can't see for example.I paste some code to tell me where i add scrollpane or scrollbar
public class Test extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new SpringLayout());
//Suppose that here we have many jlabels,jbuttons,jtextfields and other
}
}
I red some other examples and the problem is that i don't have only a picture to redraw or circles but stuff added by users.Its an online application.

I don't need a small program with a scrollbar example but help on my code how to add it in the entire frame and work dynamically.Resizing and moving the scrollbars and showing components under when you scroll down
Yes you do need a simple example. You should start with something that works and then modify it to meet your needs. You have many problems with your code:
Don't mix AWT and Swing components.
Don't use a null layout.
Don't randomly set the size of a component. Every Swing component is designed to have a preferred size at which is should be displayed.
Read the Swing tutorial. You will find plenty of examples that will show you the proper way to use Swing components and build the initial GUI on the EDT.

Related

New JFrame Being Created When I Add Content Pane

I don't know why but when I add a content pane it creates a new JFrame window instead of replacing the old content pane on the same window. Thank you, my code follows below: I have a public void (container pane). This is what I'm adding to the JFrame.
public void Start1(){
if (number==0){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
add(getContentPane());
//Display the window.
pack();
setVisible(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
}
else {
add(getContentPane());
//Display the window.
pack();
}
}
As discussed in the comments, the problem you're facing with updating the contents of your ContentPane a simple removeAll(); and then repaint(); would be enought. A Content Pane is always created with it's JFrame, and should not be removed if you have other options. In your case, you should only update your Content Pane's components. This is a solution I made up when I faced a similar problem:
I had a JPanel created in my Content Pane:
JPanel viewport = new JPanel();
viewport.setBackground(Color.WHITE);
viewport.setBounds(0, 302, 414, 420);
contentPane.add(viewport);
viewport.setLayout(null);
And then, whenever I wanted to update that screen, I added the following code to the method/event:
viewport.removeAll();
viewport.repaint();
This will remove every component you had and repaint it to your JPanel. So if you update the information (or whatever it is you want to update) and then run these two simple methods, you'll have your components properly updated.
If you have any other doubts, feel to check the class in which I implemented this solution in my GitHub.

Swing: Start second JPanel, when the first JPanel is clicked

I'm writing a simple java game and I'm facing this problem:
My different layouts are in different JPanels (1 JPanel for the welcoming page, where I have to press 'start game' and another one with the actuall functionallity)
I start the game from a JFrame
import javax.swing.JFrame;
public class RType extends JFrame {
public RType() {
add(new Welcome());//first panel
add(new Board());//panel with the game
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(100, 100);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new RType();
}
}
obviuosly, this launches the second panel right after the first, and I cant see the first one.
I've tried some stuff, trying to invoke the second panel in the main method, when the first panel is clicked that way:
RType rt=new RType();
rt.add(new Board()); //in this case add(new Board()); is removed from constructor
but it's doing nothing.
how can I solve it?
As #nachokk has said, you should be using a CardLayout instead. It lets you do things like tabs in a browser, but you don't need to make the tabs visible for your game if you don't want to. You make your welcome "card" visible at first, then when the user clicks you switch to the Board "card".
I don't recommend to add directly to the JFrame components, instead use another container as JPanel . JFrame default layout is BorderLayout, when you add in the way you are adding it always put in the center.
Make something like this:
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new CardLayout());
mainPanel.add(new Welcome(), "Welcome");
mainPanel.add(new Board(),"Board");
frame.add(mainPanel);
Here is a tutorial How to use CardLayout
on first panel of welcome add a button, and apply actionperformed like
JButton myButton = new JButton("Add Component ");
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.remove(welcome);
frame.add(Board, BorderLayout.CENTER);
frame.revalidate();
frame.repaint();
}
});
By default, both panels will fill up the entire Frame's area. To fix this, you will need to use another layout, such as a GridLayout to structure the areas in which the panels will take up the Frame's area.
You can also go with no layout to hard code the pixel values of where you want the panels to fit in your frame.
EDIT: Based on what you're looking to do, the CardLayout is probably what you'll want to use for your Frame's layout.

placing a transparent JPanel on top of another JPanel not working

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.

JFrame is very tiny when restore down is pressed

My question is why when I press to the restore down (on a Windows platform) the JFrame is very tiny (see the bellow screenshot).
I use this code regarding State of the JFrame:
this.setExtendedState(View.MAXIMIZED_BOTH);
I need to use setMinimumSize()?
Be sure that this.setExtendedState(JFrame.MAXIMIZED_BOTH) is executed AFTER setVisible(true). Also if you have a setResizable(false) call make sure it is executed AFTER the setExtendedState() one.
I think no one understood his question. He doesn't want to maximize it, he wants when he press "Restore Down", the window won't become so small. So what you need to do, "Restore Down" restores the window to its previous size before "Maximize" has been pressed.
So what I did was this
int Width = (int) java.awt.Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int Height = (int) java.awt.Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setSize(Width-100,Height-100);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
Therefore, every time you press Restore Down, the first time, the size will be the screen size - 100 pixels for width and height.
What class is View?
If you are trying to set your JFrame size to maximum do this (notice I used the static variable available in JFrame class and not View):
JFrame frame=...;
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
As this was the first and most accurate question to my issue, I will add what I found here. I know it is 3 years old but my answer might help someone in the future. My issue was the same as above. When I built the Jframe I used:
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
When I clicked the "restore down" button the window was really small and in the top left corner of the screen. I fixed this by calling:
frame.setBounds(100, 100, 450, 300);
After setVisible(true). Here is the code after my editing, the original code was created with Eclipse WindowBuilder. I am just editing it after. Here is the code complete:
public class MainWindow extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow frame = new MainWindow();
frame.setVisible(true);
frame.setBounds(100, 100, 450, 300);
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
//setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
Basically what is happening (as far as I can determine) after the Jframe becomes visible it is first set to a size of 450x300, then immediately changes the size to maximized. Now when you hit the restore down button it will set it to the 450x300 size. I am currently searching for a way to override the method used to set the window size to the previous size (before hitting max), but I'm not at a point that I need to ask yet.
appFrame = new JFrame("BufferedImage layers");
Dimension asRestoredDown = new Dimension(400,400);//first time restored
appFrame.setMinimumSize(asRestoredDown);
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
appFrame.setExtendedState(appFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);

java panel with png background

i found this link.. LINK what i want is there's a JPanel that has a background and another JPanel with half the size of the first JPanel but with an image that is transparent and with a face or a ball at the middle.. :) just like the screenshot from the link.. is that possible to code in java? :) im just thinking it like for web programming. just a sort of DIV's to have that but i dont know in java.. :) sorry for bad english.. :D i have this as a background..
package waterKing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
#SuppressWarnings("serial")
public class Main extends JFrame {
MainData data = new MainData();
public static void main(String[] args) {
Main frmMain = new Main();
frmMain.setExtendedState(Frame.MAXIMIZED_BOTH);
frmMain.setVisible(true);
}
public Main() {
data.tk = getToolkit();
data.d = data.tk.getScreenSize();
data.jP = new JPanel() {
protected void paintComponent(Graphics g) {
data.e = getSize();
data.iI = new ImageIcon("images/mainBG.png").getImage();
g.drawImage(data.iI,0, 0, data.d.width, data.d.height, null);
super.paintComponent(g);
}
};
data.jP.setOpaque(false);
data.jSp = new JScrollPane(data.jP);
data.jB = new JButton("EXIT");
data.jB.setBounds(10,10,200,40);
data.jB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
data.jP.setLayout(null);
data.jP.add(data.jB);
this.setTitle("Water King Inventory System");
this.setUndecorated(true);
this.getContentPane();
this.add(data.jSp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
}
}
i dont know how to add another JPanel to show in the middle with this background
i dont know how to add another JPanel to show in the middle with this background
Its just like adding components to a panel. You need to use a layout manager and then the component will be positioned properly based on the rules of the layout manager. In your case you can set the layout manager of the background panel to be a BorderLayout. Then you can add a JLabel with the appropriate Icon to the center of the BorderLayout.
You will need to set the preferred size (or override the getPreferredSize() method of your panel since you add it to a scroll pane. Scrollbars will only appear when the preferred size of the panel is greater than the size of the scroll pane.
You should not be reading the image in your paintComponent() method since this method is called multiple times.
You should not be using the "screen size" to determine the width/height of the image because the frame will contain a border. You need to use the size of the panel.
Get rid of all the setBounds() code. Learn to use layout managers.
For a general purpose background panel that takes into account most of the suggestions made here check out Background Panel.

Categories