I have been trying to implement a GlassPane for my games' in-game HUD, but right now I cant seem to get the JFrame to set my GlassPane as its own i;ve used setGlassPane() and Ive been reading up a few examples trying to find my mistake, but nothing. So I wrote a SSCCE that demonstrates my problem. I have a JFrame to which I add a Jpanel with a label "TESTIING" then I initiate my galss pane and call setGlassPane() on my frames instance. My GlassPane has a MouseListener, a JPanel and 2 JLabels, and an overriden paint() however the MouseListener wont work the paint() wont show and my labels are not there (so basically my GlassPane is not being set as my frames new GlassPane)-
/*Main.java*/
import java.awt.EventQueue;
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
TestGlassPane testGlassPane=new TestGlassPane();
testGlassPane.setVisible(true);
}
});
}
}
/*TestGlassPane.java*/
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class TestGlassPane extends JFrame{
private GlassGamePane m_glassPane;
private JPanel drawingPanel;
private JLabel testLabel;
public TestGlassPane() {
createUI();
}
private void createUI() {
setTitle("Test GlassGamePane");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(800, 700);
setResizable(false);
setLocationRelativeTo(null);
createComponents();
//add components to frames content pane
addComponentsToContentPane(getContentPane());
//setting glassPane
m_glassPane = new GlassGamePane();
//set opaque to false, i.e. make transparent
m_glassPane.setOpaque(false);
m_glassPane.setVisible(true);
getRootPane().setGlassPane(m_glassPane);
}
private void addComponentsToContentPane(Container contentPane) {
drawingPanel.add(testLabel);
contentPane.add(drawingPanel, BorderLayout.CENTER);
}
private void createComponents() {
drawingPanel=new JPanel(new BorderLayout());
testLabel=new JLabel("TESTIING");
}
}
/*GlassGamePane.java*/
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GlassGamePane extends JPanel implements MouseListener {
private JPanel statusPanel;
private JLabel healthLabel;
public GlassGamePane() {
createGlassPane();
}
private void createGlassPane() {
setLayout(new BorderLayout());
createComponents();
statusPanel.add(healthLabel);
add(statusPanel, BorderLayout.NORTH);
addMouseListener(this);
}
private void createComponents() {
statusPanel = new JPanel(new GridLayout(2, 6));
healthLabel = new JLabel("Player Health:");
healthLabel.setForeground(Color.RED);
healthLabel.setBackground(Color.BLUE);
}
#Override
public void paintComponent(Graphics g) {
g.setColor(Color.red);
//Draw an oval in the panel
g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
}
#Override
public void mouseClicked(MouseEvent me) {
Toolkit.getDefaultToolkit().beep();
}
#Override
public void mousePressed(MouseEvent me) {
Toolkit.getDefaultToolkit().beep();
}
#Override
public void mouseReleased(MouseEvent me) {
}
#Override
public void mouseEntered(MouseEvent me) {
}
#Override
public void mouseExited(MouseEvent me) {
}
}
Thanks you.
It seems that you have to first add the custom GlassPane as your frames GlassPane before making the GlassPane visible! This code here seemed to work:
//setting glassPane
m_glassPane = new GlassGamePane();
setGlassPane(m_glassPane);
//set opaque to false, i.e. make transparent
m_glassPane.setOpaque(false);
m_glassPane.setVisible(true);
From the JavaDoc: http://docs.oracle.com/javase/7/docs/api/javax/swing/JRootPane.html#setGlassPane(java.awt.Component)
You need to set the glasspane to visible after adding it the JRootPane.
seems like as Glasspane is correcty created, but Glasspane to overlay only lightweight JComponent otherwise is behind, hidden be heavyweight Swing or AWT J/Component
Glasspane has not implemented any LayoutManager, you have to set proper LayoutManager
for testing put non opaque JLabel with some Background
I m love Glasspane, but you can to use JLayer (Java7) based on JXLayer (Java6) or OverlayLayout, as easiest of ways
Related
I am programming a multiplication app for very large integers, I need to update every sigle step of the multiplication in a Swing component ( I created a JPane extended class with a JTextArea in it, then add it to the JFrame inside a ScrollPane). The issue is that this Swing component only updates once the multiplication algorithm is done. I tried using a Thread that would call repaint method of the Pane every 10 ms, but it did not work. The next is a sample of the problem.
This is the main Frame class:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame extends JFrame implements ActionListener{
private Console console;
private JButton calculate;
private Calculator calculator;
public Frame(){
console=new Console();
calculate=new JButton("Calculate");
calculate.addActionListener(this);
calculate.setActionCommand("");
calculator=new Calculator(this);
this.setLayout(new BorderLayout());
this.add(console,BorderLayout.CENTER);
this.add(calculate, BorderLayout.NORTH);
this.setTitle("Frame");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(new Dimension(500,500));
this.setLocationRelativeTo(null);
}
public void writeOnConsole(String txt){
console.write(txt);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("")){
console.clear();
calculator.calculate();
}
}
public static void main(String[] args) {
new Frame();
}
}
This is the Console Class
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
public class Console extends JPanel{
private JTextArea area;
public Console(){
this.setBorder(new TitledBorder("Console:"));
area=new JTextArea();
this.setLayout(new BorderLayout());
JScrollPane scroll=new JScrollPane(area);
this.add(scroll,BorderLayout.CENTER);
}
public void clear(){
area.setText("");
}
public void write(String txt){
area.append(txt+"\n");
}
}
Finally, this is the Calculator class (the one responsible for calling the writing)
public class Calculator {
private Frame parent;
public Calculator(Frame f){
parent=f;
}
public void calculate(){
for (int i = 0; i <= 1000000; i++) {
parent.writeOnConsole("Iteration "+i);
}
}
}
Note that if you run the program, the GUI will freeze until the Calculator class is done with the loop.
if you have a layout like a BorderLayout and you want to update it inside the JFrame do as bellow
JFrame frame = new JFrame();
BorderLayout layout = new BorderLayout();
layout.layoutContainer(frame.getContentPane());// use the frame as the border layout container
else you can use JFrame pack() method. The pack method packs the components within the window based on the component’s preferred sizes. it's not for updating but it updates the JFrame which is kind of a trick
JFrame frame = new JFrame();
//change the components dynamically
frame.pack();
or use Container methdod validate(). Validating a container means laying out its subcomponents. Layout-related changes, such as setting the bounds of a component, or adding a component to the container.
JFrame frame = new JFrame();
Container container = frame.getContentPane();
container.validate();
or if you want to update an specific component use
Component component = new JPanel();
component.repaint();
If this component is a lightweight component, repaint() method causes a call to this component's paint method as soon as possible .
or if you want for example numerous changes happen one by one dynamically then you could use the code below which is completely different from the things i said above. for that you could use platform.runlater() inside another thread which deals with everything that is about to change in realtime
new Thread(new Runnable()
{
#Override
public void run()
{
Platform.runLater(new Runnable()//use platform.runlater if you are using javafx
{
#Override
public void run()
{
try
{Thread.sleep(50);}catch(Exception e){}//use it in for loop where changes happen
//do some realtime change of components
}
});
}).start();
your Console class would be
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
public class Console extends JPanel{
private JTextArea area;
public Console(){
this.setBorder(new TitledBorder("Console:"));
area=new JTextArea();
this.setLayout(new BorderLayout());
JScrollPane scroll=new JScrollPane(area);
this.add(scroll,BorderLayout.CENTER);
}
public void clear(){
area.setText("");
}
public void write(String txt){
area.append(txt+" "+"\n");
}
}
and the Calculator class is
public class Calculator {
private Frame parent;
public Calculator(Frame f){
parent=f;
}
public void calculate(){
new Thread(new Runnable() {
#Override
public void run()
{
for (int i = 0; i <= 100; i++) {
try
{
Thread.sleep(50);
}
catch(Exception e)
{
e.printStackTrace();
}
parent.writeOnConsole("Iteration "+i);
}
}
}).start();
}
}
as you can see i used another thread to do the changes
try the update method to call paint method for maintain every change
I have a JPanel in a JScrollPane.
The JPanel contains multiple JTextAreas vertically.
I like to keep the scroll of the scrollpane to the top whenever the page is refreshed.
Currently, the scroll always starts from the bottom.
this is my current code and it doesn't work..
panel.invalidate();
panel.revalidate();
panel.repaint();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
((JPanel) panel).setLocation(new Point(0, 0));
}
});
I've also tried adding this code below to scrollpane, but it doesn't work..
scrollPanel.getViewport().setViewPosition( new Point(0, 0) );
I've looked into other stackoverflow questions and they use Jtextarea inside Jscrollpane (they solved it using setCaretPosition(0), however I can't use the same function to the panel). In my case, there is an extra layer.
How can I solve this..?
EDIT**
Based on advice from Pavlo Viazovskyy, I've also tried this below and it still doesn't work for me.. :(
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
scrollPane.getVerticalScrollBar().setValue(0);
}
});
Thank you very much for all the comments.
sorry I didn't give a full proper example in the question as there were too many different classes involved..
In my case, textareas inside Panel inside ScrollPane, I made the scroll to the top by default by using setViewPosition method to scrollPane in the invokelater method.
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
scrollPane.getViewport().setViewPosition( new Point(0, 0) );
}
});
For when you don't have direct access to the JScrollPane, you can simply use JComponent#scrollRectToVisible
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class ScrollTest {
public static void main(String[] args) {
new ScrollTest();
}
public ScrollTest() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test");
frame.add(new JScrollPane(new BigPane()));
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BigPane extends JPanel {
public BigPane() {
setLayout(new BorderLayout());
JButton scroll = new JButton("Scroll to top");
add(scroll, BorderLayout.SOUTH);
scroll.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
scrollRectToVisible(new Rectangle(0, 0, 1, 1));
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
}
Yes, you could walk the component hierarchy till you found a JViewport, but this method does it for you.
Just remember though, the Rectangle is relative to the component which called the method, so if I used the JButton instead, it would try and make the JButton visible, not the panel
I've created a JFrame.
Inside this JFrame, I've created a JPanel.
Inside this JPanel I've created another JPanel (lets call it "A").
I've drawn in "A" a rectangle, and wanted to create buttons using graphics.
There is no rectangle in my gui. I could see that the paintComponent() method inside "A" is not being invoked.
Code:
The JPanels: (the child JPanel is inner class)
public class MemoryPanel extends JPanel {
public MemoryPanel(){
setPreferredSize(new Dimension(350,448));
}
#Override
public void paintComponent(Graphics g) {
//POSITIONING
setLayout(new BorderLayout());
//CREATE MEMORY BUTTONS
MemButton a=new MemButton();
//Drawing Rectangles for Memory
add(a,BorderLayout.CENTER);
}
private class MemoryButton extends JPanel{
public MemoryButton(){
setLayout(null);
setPreferredSize(new Dimension(87,40));
}
#Override
public void paintComponent(Graphics g){
Graphics2D td= (Graphics2D)g;
td.drawRect(0, 0, 20, 20);
}
}
}
You should program the JButtons first in order for your graphics to work as buttons. I belive this post will help you with that:
Creating a custom button in Java
I you want a rectangle to be the background for your buttons you can draw it in your main panel and add the buttons on it. Try using different Layouts to mantain some order.
I've made a simple GUI to test your code and the rectangle appears correctly.
I made no relevant changes in the code that you posted.
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class SimpleJFrameProgram extends JFrame {
private static final long serialVersionUID = 1L;
public SimpleJFrameProgram() {
super("TEST");
initComponents();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private void initComponents() {
MemoryPanel memoryPanel = new MemoryPanel();
this.add(memoryPanel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
new SimpleJFrameProgram();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
I've applyed minor changes to your MemoryPanel: replaced MemButton by your MemoryButton and fill the rectangle in red to improve its visibility for the test. Without this last change, the rectangle appears too.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class MemoryPanel extends JPanel {
public MemoryPanel(){
setPreferredSize(new Dimension(350,448));
}
#Override
public void paintComponent(Graphics g) {
// POSITIONING
setLayout(new BorderLayout());
// CREATE MEMORY BUTTONS
MemoryButton a = new MemoryButton();
// Drawing Rectangles for Memory
add(a,BorderLayout.CENTER);
}
private class MemoryButton extends JPanel{
public MemoryButton(){
setLayout(null);
setPreferredSize(new Dimension(87,40));
}
#Override
public void paintComponent(Graphics g) {
Graphics2D td = (Graphics2D) g;
td.setColor(Color.red);
td.fillRect(0, 0, 20, 20);
}
}
}
This is the obtained result:
Maybe your problem is located on initializing the parent JFrame.
Changing the class name of MemoryButton fixed it.
I had another package with the same class name.
I'm trying to make a program that lets the user manually place and resize components within a JScrollPane, a bit of a special-case UI Builder. I managed to make a custom JPanel class that allows the user to move it around manually however when it's added to the JScrollPane and moved around, if it goes outside of the visual bounds of the JScrollPane the scrollbars don't appear or adjust.
Here is my main class that includes the JFrame and JScrollPane.
package quickscrolltest;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class QuickScrollTest {
JFrame wnd;
JScrollPane scroll;
JPanel pnl;
MovablePanel pnl1;
public QuickScrollTest() {
wnd = new JFrame();
scroll = new JScrollPane();
pnl = new JPanel();
pnl.setLayout(null);
scroll.setViewportView( pnl );
wnd.setContentPane(scroll);
wnd.pack();
pnl1 = new MovablePanel();
Dimension dim1 = new Dimension( 300, 400 );
pnl1.setSize( dim1 );
pnl1.setPreferredSize(dim1);
pnl1.setBackground( Color.CYAN );
pnl1.setLocation( 10, 10 );
/*scroll.getViewport().add(pnl1,null);*/
pnl.add(pnl1);
wnd.setSize( 800, 600 );
wnd.setLocationRelativeTo(null);
wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wnd.setVisible(true);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
new QuickScrollTest();
}
}
Here is the MovablePanel class
package quickscrolltest;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
public class MovablePanel extends JPanel implements MouseListener, MouseMotionListener {
Color bg = Color.GRAY;
Point clickPoint;
public MovablePanel() {
addMouseListener(this);
addMouseMotionListener(this);
}
#Override
public void setBackground( Color col ) {
super.setBackground(col);
bg = col;
}
#Override
public void mouseClicked(MouseEvent e) {}
#Override
public void mousePressed(MouseEvent e) {
clickPoint = e.getPoint();
}
#Override
public void mouseReleased(MouseEvent e) {
setCursor( Cursor.getDefaultCursor() );
}
#Override
public void mouseEntered(MouseEvent e) {
setCursor( Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) );
super.setBackground( Color.RED );
}
#Override
public void mouseExited(MouseEvent e) {
setCursor( Cursor.getDefaultCursor() );
super.setBackground( bg );
}
#Override
public void mouseDragged(MouseEvent e) {
setCursor( Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR) );
int lastX = getX() - (int) clickPoint.getX();
int lastY = getY() - (int) clickPoint.getY();
setLocation(lastX + e.getX(), lastY + e.getY() );
}
#Override
public void mouseMoved(MouseEvent e) {}
}
How can I make it so that users can freely move components around in the JScrollPane and have the scrollbars update as required?
My final code will have a standalone mouselistener/mousemotionlistener class that generically works on a given JComponent but I coded the listeners directly into the JPanel for simplicity.
Thanks
Scrollbars automatically appear/disappear when the preferred size of the component displayed in the viewport of the scroll panes changes.
You can use the Drag Layout. This class is a custom layout manager and will automatically recalculate the preferred size of the panel as components are dragged around the panel.
You will need to handle the mouseReleased event so you can revalidate() the panel, and invoke the DragLayout once you are finished dragging the component so the preferred size can be reset. Or you could use the ComponentMover class which is referenced in the blog article to do the dragging of the component for you. It supports an auto resize property which will do the revalidate() for you.
I'm sure someone has asked this question before, but my google-fu is not strong today.
I have a JFrame that uses a CardLayout as its manager. How do I run a "Start" method when I switch to each JPanel without using a switch?
The code I use to add the frames to the layout is:
/**
* Adds JPanels to the Card Layout.
* #param panel is the JPanel to add to the layout.
* #param windowName is the identifier used to recognise the Panel.
*/
public final void addToCards(final JPanel panel, final WindowNames windowName) {
view.getBasePanel().add(panel, windowName.getValue());
}
The code I use to switch the layout is:
/**
* Method to change the JPanel currently visible on the BasePanel.
* #param windowName is the name of the JPanel to change to.
*/
public final void changePanel(final WindowNames windowName) {
view.getCardLayout().show(view.getBasePanel(), windowName.getValue());
}
Currently I have an ActionListener set that will call the switch code, but I can't work out how to call the "Start" method within the screen that it will be switching to.
I have an interface setup for each of the JPanels so that the method name will be identical in each.
You can just use a ComponentListener for the panel(s). When the panel becomes the view of the CardLayout, it will fire a component event and handled by componentShown in your listener (as well as the panel taken out of view, handling the componentHidden). Call your start() method there. This way you don't have to explicitly call the start() when the panel changes, as it be called for you.
See How to Write Component Listeners for more details.
Here is a simple example.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main {
private static final String PANEL_A = "panelA";
private static final String PANEL_B = "panelB";
CardLayout layout = new CardLayout();
JPanel panel = new JPanel(layout);
ComponentListenerPanel p1 = new ComponentListenerPanel(PANEL_A);
ComponentListenerPanel p2 = new ComponentListenerPanel(PANEL_B);
JButton b1 = new JButton(PANEL_A);
JButton b2 = new JButton(PANEL_B);
public Main() {
panel.add(p1, PANEL_A);
panel.add(p2, PANEL_B);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
show(PANEL_A);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
show(PANEL_B);
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(b1);
buttonPanel.add(b2);
JFrame frame = new JFrame();
frame.add(panel);
frame.add(buttonPanel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void show(String panelName) {
layout.show(panel, panelName);
}
private class ComponentListenerPanel extends JPanel {
private String panelName;
public ComponentListenerPanel(String panelName) {
this.panelName = panelName;
addComponentListener(new ComponentAdapter() {
#Override
public void componentHidden(ComponentEvent evt) {
stop();
}
#Override
public void componentShown(ComponentEvent evt) {
start();
}
});
}
public void start() {
System.out.println(panelName + " started");
}
public void stop() {
System.out.println(panelName + " stopped");
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Main();
}
});
}
}
Note you haven't actually said where the start method is, so this code/answer is just assuming you have some start method in custom panel. Hope I guessed right. In the future, or even now, you should always post an MCVE so that we don't have to do all this guessing.
I have an interface setup for each of the JPanels so that the method name will be identical in each
So then the problem is getting the current panel that is visible when the panels are swapped so you can invoke the method.
Check out Card Layout Focus for a class that extends CardLayout to provide a few helper methods to add additional functionality for the CardLayout. You would use the getCurrentCard() method.
So your changePane(...) method might be something like:
public final void changePanel(final WindowNames windowName) {
//view.getCardLayout().show(view.getBasePanel(), windowName.getValue());
RXCardLayout layout = view.getCardLayout();
layout.show(view.getBasePanel(), windowName.getValue());
MyInterface panel = (MyInterface)layout.getCurrentCard();
panel.someMethod(...);
}
Of course you would also need to use the RXCardLayout as the layout manager for your main panel.