Is it possible to pass events from an instance class to a parent class?
I have a JFrame with a container that has two JPanels. The upper panel contains another smaller JPanel and I've set up a MouseListener successfully to execute a command when the small panel is clicked. I want to display text on the bottom panel when the small panel is clicked. The problem is that the event handling is done within the upper panel class, which does not have access to the instance of the other panel on the JFrame.
Here is the main window code below:
public CalendarWindow() {
// Set global variables
size = new Dimension(600,600);
setMinimumSize(size);
// Set default window layout and behaviour
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
Container c = getContentPane();
// Create panels and components
eventPanel = new EventPanel(new Dimension(size.width - 30, size.height/3));
CalendarPanel calendarPanel = new CalendarPanel(new Dimension(size.width - 30, size.height*2/3));
// Add components and panels to content pane
c.add(eventPanel, BorderLayout.SOUTH);
c.add(calendarPanel, BorderLayout.NORTH);
}
This is the event handling within calendarPanel:
boxes[i][j].addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(selectedBox != null)
selectedBox.unselect();
selectedBox = (DateBox)e.getSource();
selectedBox.getParent().getParent().dispatchEvent(e);
if(SwingUtilities.isRightMouseButton(e)) {
rightClickMenu menu = new rightClickMenu();
menu.show(e.getComponent(), e.getX(), e.getY());
menu.newEvent.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
selectedBox.addCalendarEvent(new CalendarEvent("Kaupa mjólk", 14, 15));
}
});
}
}
});
The boxes variable is a type that extends JPanel. How can I make it so that the CalendarWindow detects the mouse click in the upper panel?
Make a method in your parent class that changes the text,
and pass the parent class to the eventPanel
(add a parameter eg. public eventPanel( CalendarWindow cw),
then pass it, new eventPanel(this)),
and then call the method of the parent within the eventPanel (cw.themethod())
Related
I would replace the same panel several times , but do not know how to do.
I created a class " Grafico " which has a constructor that initializes with more parameters that I calculate in my code . For simplicity I will omit these parameters .
"box " is a JComboBox that depending on the selected item is activated and creates these panels
For example:
JPanel middle = new JPanel(new BorderLayout());
Grafico graph1 = new Grafico(.......);
JPanel conf1 = new JPanel();
middle.add(graph1, BorderLayout.CENTER);
middle.add(conf1, BorderLayou.EAST);
frame.getContentPane().add(middle);
box.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
.........//I do something..I create a new object of type "Grafico" with new parameters........................
Grafico graph2 = new Grafico(.......);
middle.remove(graph1);
middle.add(graph2, BorderLayout.CENTER);
frame.getContentPane().add(middle);
frame.getContentPane().revalidate();
frame.getContentPane().repaint();
}
});
And this works , but the second time I click on an object in the JComboBox I wish it would update me graph2 every time, without creating a new one every click , but it does not!
You have to call repaint() and revalidate() in order to refresh the Panel.
Java Swing revalidate() vs repaint()
I would like to access an extended JPanel from a listener in another class, in order to re-size that panel when the button is clicked.
I tried to access it using Buttons.this.setPreferredSize...., but i got this error
No enclosing instance of the type Buttons is accessible in scope
My Buttons class looks like this
public class Buttons extends JPanel {
public Buttons() {
//code
}
}
and the other from where i want to change the size of the Panel
public class InterfaceCalc extends JPanel {
// others codes
expandIcon = new ImageIcon("src\\img\\expand.png");
expand = new JButton("", expandIcon);
expand.setBorderPainted(false);
expand.setContentAreaFilled(false);
expand.setFocusPainted(false);
expand.setOpaque(false);
expand.setMargin(new Insets(0, 0, 0, 0));
expand.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (!on) {
Buttons.sqrt.setVisible(true);
Buttons.log.setVisible(true);
Buttons.cos.setVisible(true);
Buttons.sin.setVisible(true);
// this is where is want to access the panel
Buttons.this.setPreferredSize(new Dimension());
on = true;
} else {
Buttons.sqrt.setVisible(false);
Buttons.log.setVisible(false);
Buttons.cos.setVisible(false);
Buttons.sin.setVisible(false);
on = false;
}
}
});
// others codes
}
How can achieve that ?
I fixed the problem by creating an instance of Buttons outside the listener. before ,the extended panel was added directly like this in this.add(new Buttons()) in theInterfaceCalc panel, now after i created the instance I added it to the panel and i used it to change the size.
I have created a JScrollPane with a JPanel inside it and I want to add JPanel/JLabel/Other objects after pressing the button. For example after three button presses I want to get something like this:
I tried myJPane.add(testLabel) with testlabel.setBounds()but no result, I don't want to use GridLayout because of the unchangeable sizes. I would like it if the added objects had different sizes - adjusted to the text content.
What should I use for it and how?
Thanks in advance.
Best regards,
Tom.
Here is a JPanel inside a JScrollPane that adds JLabels to it when pressing the button:
public class Example extends JFrame {
public Example() {
JPanel boxPanel = new JPanel();
boxPanel.setLayout(new BoxLayout(boxPanel, BoxLayout.PAGE_AXIS));
JTextField textField = new JTextField(20);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JLabel label = new JLabel(textField.getText());
label.setOpaque(true);
label.setBackground(Color.RED);
boxPanel.add(label);
boxPanel.add(Box.createRigidArea(new Dimension(0,5)));
textField.setText("");
boxPanel.revalidate();
// pack();
}
});
JPanel southPanel = new JPanel();
southPanel.add(textField);
southPanel.add(sendButton);
add(new JScrollPane(boxPanel));
add(southPanel, BorderLayout.PAGE_END);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Example();
}
}
The BoxLayout will stack the labels on top of each other.
Notes:
setOpaque(true) must be called on label for it to honor the background color.
Box.createRigidArea is used for creating gaps. Use it as you wish.
The call to revalidate() is imperative in order to display the new components immediately.
Calling pack() (on the JFrame) will resize it each time to fit all the new components. I just put it there for demonstration since the initial frame size is too small to display the initial components added.
I will use a BoxLayout, creating a vertical box, and after each button action, it will add a new JPanel to this box.
Example:
public class YourChat extends JPanel{
private JScrollPane sc;
private Box bv;
public YourChat(){
bv = Box.createVerticalBox();
sc = new JScrollPane(bv);
//your functions (panel creation, addition of listeners, etc)
add(sc);
}
//panel customized to have red backgroud
private class MyPanel extends JPanel(){
private JLabel label=new JLabel();
public MyPanel(String text){
setBackgroundColor(Color.red);
add(label);
}
}
//inside the action listener
public void actionPerformed(ActionEvent e) {
sc.add(new MyPanel(textField.getText()));
textField.setText("");
}
}
For extra information check on:
[https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html]
See also the example
[http://www.java2s.com/Code/Java/Swing-JFC/VerticalandhorizontalBoxLayouts.htm]
Use BoxLayout if you want only add vertically, otherwise you can use FlowLayout for both directions.
The structure likes this:
I have a Frame and two button - btnA, btnB
when I press btnA, a createPanel() function will return a panelA to be displayed in the frame,
and so does btnB.
btnA and btnB can be switched.
Before I add the panel into the frame, I use a clearPanel() function to clear the existing panels in the frame.
but the question is when I resize or click the panel, I can see the previous panels that should be removed already.
Is there anything I lost?
public void actionPerformed(ActionEvent e) {
String buttonString = e.getActionCommand();
if (buttonString.equals("A")) {
clearPanel();
A = new APanel();
this.getContentPane().add(A.createPanel(), BorderLayout.CENTER);
this.pack();
componentMap.put("A", A);
btnB.setEnabled(true);
btnA.setEnabled(false);
}
else if (buttonString.equals("B")) {
clearPanel();
chart = new BPanel();
this.getContentPane().add(B.createPanel(), BorderLayout.CENTER);
this.pack();
componentMap.put("B", B);
btnA.setEnabled(true);
btnB.setEnabled(false);
}
}
private void clearPanel() {
if (!componentMap.isEmpty()) { // I store panels in a HashMap
for (Object o: componentMap.values()) {
this.getContentPane().remove((JPanel)o);
}
this.getContentPane().invalidate();
componentMap.clear();
}
}
You are adding A.createPanel() and B.createPanel() to the contentPane but you store A and B in your componentMap. Therefore, when you call this.getContentPane().remove((JPanel)o);, you are doing this on A and/or B which are not in the content pane and therefore you don't remove anything.
You could use a simpler/safer approach if you want to clear the content pane:
this.getContentPane().removeAll();
I am learning how to use Swing and found myself quite difficult task.
What I am trying to accomplish: I want to have panel (call it menu panel) on the left side (let's say 100px width) and the second panel (call it content panel), which takes the rest of available place.
In menu panel there are 3 buttons. When I press on of them, to the right side of menu panel (over content panel) second menu panel (submenu) should appear (and it should start in the middle of button which was pressed).
It may be hard to understand, so I've created simple draft:
I tried JLayeredPane but there were problems with resizing window (elements in Layered Pane didn't resize).
JLayeredPane miss implementations for LayoutManager, you have to setPreferredSize or setBounds manually for sizing/place JComponents,
there is one possible workaround you can add ComponentListener to the JFrame, then on componentResized(ComponentEvent e) you can resize/replace JComponent(s) to the desired Bounds
for example
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class LayeredPaneWithOverlap {
private JTextArea textArea = new JTextArea(2, 10);
private JPanel textPanel = new JPanel(new BorderLayout());
private JTable table = new JTable(30, 5);
private JScrollPane scroll = new JScrollPane(table);
private JLayeredPane layer = new JLayeredPane();
private JFrame frame = new JFrame("Frame with resiziable JLayeredPane");
public void makeUI() {
textArea.setBorder(new LineBorder(Color.DARK_GRAY));
textArea.setText("Frame with resiziable JLayeredPane");
textPanel.setOpaque(false);
textPanel.add(textArea, BorderLayout.NORTH);
Font font = textArea.getFont();
FontMetrics fontMetrics = textArea.getFontMetrics(font);
int h = fontMetrics.getHeight() + frame.getInsets().top +
textPanel.getInsets().top + textArea.getInsets().top
+ textArea.getInsets().bottom;
scroll.setBounds(0, h, 400, 300);
layer.add(textPanel, new Integer(2));
layer.add(scroll, new Integer(1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
resizeAll();
}
});
}
});
frame.setLocationRelativeTo(null);
frame.add(layer);
resizeAll();
frame.setVisible(true);
}
void resizeAll() {
Insets insets = frame.getInsets();
int w = frame.getWidth() - insets.left - insets.right;
int h = frame.getHeight() - insets.top - insets.bottom;
textPanel.setSize(w, h);
scroll.setSize(w, h - scroll.getY());
layer.revalidate();
layer.repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayeredPaneWithOverlap().makeUI();
}
});
}
}
You can set a layoutmanager for the layered pane, javax.swing.OverlayLayout uses the full available space and allows resizing.
JLayeredPane layer = new JLayeredPane();
layer.setLayout(new OverlayLayout(layer));
You probably don't want the submenu to occupy the fullspace. To avoid it you can override its get…size-methods. Or you can add a second LayeredPane (for it's transperancy and it's layoutmanager), set a normal BoxLayout and use a spacer.
JPanel normalContents = new JPanel();
layer.add(normalContents, JLayeredPane.DEFAULT_LAYER);
JLayeredPane subMenuAuxiliaryLayer = new JLayeredPane()
subMenuAuxiliaryLayer.setLayout(new BoxLayout(subMenuAuxiliaryLayer, BoxLayout.LINE_AXIS));
layer.add(subMenuAuxiliaryLayer, JLayeredPane.PALETTE_LAYER);
JPanel submenuContents = new JPanel();
subMenuAuliliaryLayer.add(submenuContents);
subMenuAuxiliaryLayer.add(Box.createHorizontalGlue());
contentPanel.setLayout(null); // Absolute positioning of children.
#Override
public void actionPerformed(ActionEvent evt) {
final JButton btn = (JButton) evt.getSource();
final int buttonY = btn.getY(); // Must be final for usage in new Runnable object.
SwingUtilities.invokeLater(new Runnable() { // Return fast from event handling.
#Override
public void run() {
JPanel child = new JPanel();
child.setBackground(Color.RED); // So we'll see it.
child.setBounds(0, buttonY, 100, 300);
contentPanel.removeAll(); // Clear content panel of prior additions.
contentPanel.add(child); // Add a new panel.
contentPanel.repaint(10L);
}
});
}
The JLayeredPane works by defualt with no Layout manager, which means that you are using absolute positioning and no resizing. You could add a resize listener and adjust positions and size of inner components from code, as you see fit.
If you don't want to do this from code, you will need a layout manager, nothing fancy, just something to fill the container as it resizes. But here's the thing... if you add a layout manager, it will layout the components as if they are all in one layer, but most layout managers don't overlap their children so they are useless. The only one you could use is the OverlayLayout - it can also resize children. But using an OverlayLayout with JLayeredPane is overkill. You can just use OverlayLayout with a JPanel. So, yes, JLayeredPane is kind of useless. I recommend using a JPanel with an OverlayLayout instead.
Here is how to set things up so that you can have great control over almost any overlapping UI scenario out there: Using a JPanel with an OverlayLayout, have a separate transparent JPanel for each layer. In this way you can combine various LayoutManagers on different layers, by setting a diferent layout manager for each pane, including absolute positioning if necessary. Then add your visible components inside the panels representing the layers. Don't add them directly to the OverlayLayout panel. Just make sure that all of the JPanels you are using as layers have setAlignmentX and Y to center (0.5f) so that they fill the entire OverlayLayout panel as it resizes.