How to get a JInternalFrame in the order i add it - java

I tried to access a JInternalFrame in my JDesktopPane and use getAllFrames method.
I just want to access the JInternalFrame in the order that i added into the JDesktopPane.
for example, i add a,b,c
frames[0] contain a
frames[1] contain b
frames[2] contain c
But i found out that the content in the array will change in case that i change my selection.
Every time I change my selection.
The selected JInternalFrame in the array will move to the top one.
For example , I select b
The array will become
frames[0] contain b
frames[1] contain a
frames[2] contain c
Are there any other ways to get the internal frame in the order i add it into desktoppane??

package org.app;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
public class MainFrame extends JFrame{
private static final long serialVersionUID = 1L;
private JDesktopPane theDesktop;
private List<JInternalFrame> frameList=new ArrayList<>();
public MainFrame() {
super("Internal Frame Demo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
this.setJMenuBar(setMenubar());
theDesktop=new JDesktopPane();
this.add(theDesktop);
this.setVisible(true);
}
public JMenuBar setMenubar() {
JMenuBar bar=new JMenuBar();
JMenu addMenu=new JMenu("Add");
JMenuItem newFrame=new JMenuItem("Internal Frame");
newFrame.addActionListener(new MenuAction());
addMenu.add(newFrame);
bar.add(addMenu);
return bar;
}
public JInternalFrame addInternalFrame() {
JInternalFrame jif=new JInternalFrame("Internal frame",true,true,true,true);
jif.setSize(new Dimension(240, 300));
jif.addInternalFrameListener(new InternalFrameAdapter() {
#Override
public void internalFrameClosing(InternalFrameEvent e){
frameList.remove(e.getInternalFrame());
System.out.println("from frame closing event");
}
});
jif.show();
return jif;
}
public JInternalFrame getInternalFrame(int index) {
return frameList.get(index);
}
class MenuAction implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JInternalFrame f=addInternalFrame();
theDesktop.add(f);
frameList.add(f);
System.out.println("from menu action");
}
}
public static void main(String[] args){
new MainFrame();
}
}

Related

How can I access a JButton on another file or class in Java

I'm having a problem with JButton. I need to change the text on the goPauseButton when it has been clicked, but I get this error: goPauseButton cannot be resolved. I'm quite new to Java, so I started trying to solve the issue using techniques from other languages such as Free Pascal. There you need to refer to the class where the button is in, and then the button. In my code it would look like this:
PrisonersDilemma.goPauseButton.setText("Pause");
But then I get this error: Cannot make a static reference to the non-static field PrisonersDilemma.goPauseButton
This is my code (so far), I've erased unimportant things:
Main class
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.util.Hashtable;
//...
public class PrisonersDilemma /* possible extends... */ {
// declaring
JFrame frame;
PlayingField field;
JPanel componentPanel;
public JButton goPauseButton;
public JPanel createComponentPanel() {
componentPanel = new JPanel();
componentPanel.setLayout(new GridLayout(2,6));
// set goPauseButton
goPauseButton = new JButton("GO!");
goPauseButton.addActionListener(field);
goPauseButton.setBounds(110,350, 80,20); // first coordinates, then size
frame.add(goPauseButton);
return componentPanel;
}
void buildGUI() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
field = new PlayingField();
// set frame
frame = new JFrame("Prisoners Dilemma");
frame.add(field);
createComponentPanel();
frame.add(field, BorderLayout.CENTER);
frame.setLocation(200, 200);
frame.pack();
frame.setVisible(true);
frame.setSize(400, 450);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} );
}
Class with ActionEventHandler
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
public class PlayingField extends JPanel
implements ActionListener,
ChangeListener {
private boolean started;
#Override
public void actionPerformed(ActionEvent e) {
// TODO
if ("GO!".equals(e.getActionCommand())){
System.out.println("GO!");
started = true;
goPauseButton.setText("Pause"); // here is the error
} else if ("Pause".equals(e.getActionCommand())){
System.out.println("Pause");
started = false;
} else if ("Reset".equals(e.getActionCommand())){
System.out.println("Reset");
}
}
}
I think you need to change the way you're approaching the problem. The PlayingField has no responsibility for modifying the state of the goPauseButton in PrisonersDilemma. Instead, PrisonersDilemma should update the goPauseButton and call an appropriate method of PlayingField
For example...
goPauseButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
goPauseButton.setText("Pause");
field.start();
}
});
And...
public class PlayingField extends JPanel {
public void start() {
System.out.println("GO!");
started = true;
}
public void pause() {
started = false;
System.out.println("Pause");
}
public void reset() {
System.out.println("Reset");
}
}

Invoking one class from another

I am trying to learn java and surely I am not getting something.
For the sake of simplification let us assume I have two classes, class A and class B. I want to call class B from class A and when I do that I need that anywhere I right click (right button) inside class A to get the pop-up menu as defined in class B. The code is the following:
CLASS A code:
package test1;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class A {
JFrame frame;
Toolkit toolkit;
A () {
frame = new JFrame("A class");
setDimm(frame);
B b = new B();
frame.setVisible(true);
}
void setDimm(JFrame frame) {
this.frame=frame;
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.gray);
frame.setSize(800, 600);
toolkit = frame.getToolkit();
Dimension size = toolkit.getScreenSize();
frame.setLocation((size.width-frame.getWidth())/2, (size.height-frame.getHeight())/2);
}
public static void main (String [] args) {
new A();
}
}
and CLASS B code is:
package test1;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class B extends JFrame {
JPopupMenu menu;
JMenuItem addRow, removeRow;
B(){
menu = new JPopupMenu();
addRow = new JMenuItem("Add row");
removeRow = new JMenuItem("Remove row");
// Adding two action listneres just to test the clicking on the items
addRow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Add rows. Testing and it works");
}
});
removeRow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("Remove rows. Testing and it works");
}
});
// end of action listeners
addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e){
if (e.getButton() == e.BUTTON3) {
menu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
menu.add(addRow);
menu.add(removeRow);
add(menu);
}
}
I added class B inside class A but it does not work.
I tried creating a JPanel and adding it to that, still nothing.
I tried creating a B instance with a JPanel or JFrame parameter and then inside class A invoking class B and putting a JPanel or JFrame from class A but still nothing.
I tried to create a main method inside class B and it works to create the right click but inside class B and that's not where I want it.
What am I doing wrong here?
Thanks and all the best!

Resize JDialog by pushing it against the edge of the screen

I have a JFrame and a JDialog. The latter simply contains a JTextArea. Windows 7 gives now the possibility to resize a window to the half of the screen by pushing it against the right or the left edge. This works fine with the JFrame window. However, the JDialog window does not react to it, though it is manually resizable. I suppose it is closely connected with the missing maximize/minimize function of JDialogs. On the other hand, creating a JFrame as subframe is not recommended.
** Edit 1 **
Here a test code:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
public class JDialogResizetest extends JFrame {
private JButton openButton;
public JDialogResizetest() {
openButton = new JButton();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
openButton.setText("Open");
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
openButtonActionPerformed(evt);
}
});
getContentPane().add(openButton);
pack();
}
private void openButtonActionPerformed(ActionEvent evt) {
JDialog dialog = new JDialog(this, false);
dialog.getContentPane().add(new JTextPane());
dialog.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JDialogResizetest().setVisible(true);
}
});
}
}
Here a picture of the desired behavior:

Java button actions in multiple classes

Working in Java: I have a JFrame class, and separate classes for my two JPanels that are added to the JFrame. One of the JPanel classes has some buttons in it, which can interact with each other(when I click on one button, it can disable another button). However, I can't figure out how to get the button to call a method in the other JPanel (written in a separate class).
So, my program look like this:
JFrame
Jpanel1
Jpanel2 - This class has my buttons in it, I'm trying to get them to interact with the JPanel1 object.
Any tips appreciated, thanks!
One way to do this is to pass an instance of (to use your terminology) Jpanel1 into Jpanel2. This doesn't have to be done in the constructor, you can have a setConnectedPanel(JPanel) method, for example.
Here's some code that demonstrates what you want to do:
MyFrame.java
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MyFrame extends JFrame {
public MyFrame() {
ReactionPanel rp = new ReactionPanel();
ActionPanel ap = new ActionPanel(rp);
setLayout(new GridLayout(2, 1));
add(ap);
add(rp);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MyFrame();
}
});
}
}
ActionPanel.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class ActionPanel extends JPanel implements ActionListener {
private ReactionPanel rp;
private JButton button;
public ActionPanel(ReactionPanel rp) {
this.rp = rp;
button = new JButton("Click");
button.addActionListener(this);
this.add(button);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(button)) {
rp.react();
}
}
}
ReactionPanel.java
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ReactionPanel extends JPanel {
private JLabel label;
public ReactionPanel() {
label = new JLabel("PING");
this.add(label);
}
public void react() {
if(label.getText().equals("PING")) {
label.setText("PONG");
} else {
label.setText("PING");
}
}
}
As you can see, I tend to override all of my JFrames/JPanels when I write Swing GUIs as I find it easier and more flexible but YMMV.

how to stop JPopupMenu show() from visually un-selecting the list item clicked on

Right now when a user right clicks on a selected JList item in my program the resulting JPopupMenu clears the selection (at least visually) until the popup menu is closed. This isn't consistent with the native look and feel of any platform I know of. The item should stay visually selected or have a selected-color border around it. But I can't find anythin in the API about popup menus changing selection appearance. Is there any way I can control this behavior?
How are you implementing your Mouse Listener that shows the popup? I have created a test application to demonstrate the behaviour of List selections and popup menus that I would typically expect. On Windows with Java 1.5/6 this behaves correctly.
Maybe this will help you with your particular problem.
package jlist;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Test extends JPanel implements ListSelectionListener {
private static final String ACTION_FEED = "Feed";
private JList list;
private JPopupMenu menu;
// Initialise a JList and add to JPanel.
public Test() {
super(new BorderLayout());
list = new JList(new Object[]{"Badger", "Ferret", "Stoat", "Weasel"});
initActions();
list.addListSelectionListener(this);
// Add mouse listener
list.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) showPopup(e);
}
#Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) showPopup(e);
}
private void showPopup(MouseEvent e) {
menu.show(e.getComponent(), e.getX(), e.getY());
}
});
add(new JScrollPane(list), BorderLayout.CENTER);
valueChanged(null);
}
// Triggered when List Selection changes. Used to control Actions enabled state.
public void valueChanged(ListSelectionEvent e) {
boolean selected = list.getSelectedValue() != null;
getActionMap().get(ACTION_FEED).setEnabled(selected);
}
// Initialise Actions and Popup Menu
private void initActions() {
menu = new JPopupMenu();
Action feed = new AbstractAction(ACTION_FEED) {
public void actionPerformed(ActionEvent e) {
String value = (String) list.getSelectedValue();
JOptionPane.showMessageDialog(Test.this, "Fed " + value);
}
};
getActionMap().put(ACTION_FEED, feed);
menu.add(feed);
}
public static void main(String [] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new Test());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}

Categories