Let's say that I have 1 frame, 1 JDialog and 1 panel. The panel is on the frame. What I want to do is if a button is clicked i wanna switch the location of the panel to JDialog. I need two windows so i use Jdialog for that. Maybe there is a better way of creating that window rather then using JDialog.
Part of my code:
public class Bestellterminal {
private static JPanel panel;
public static void addComponentsToPane(final Container pane) {}
public static void addComponentsToPane1(final Container pane) {}
public static void addComponentsToPane2(final Container pane) {
final JPanel kpanel1 = new JPanel();
kpanel1.setBounds(0 + insets.left, 0 + insets.top, size.width + 900,
size.height + 700);
kpanel1.setVisible(true);
final JDialog meinJDialog = new JDialog();
meinJDialog.setTitle("Küchenterminal");
meinJDialog.setSize(1200,900);
meinJDialog.setVisible(true);
meinJDialog.setLayout(null);
meinJDialog.add(kpanel1);
Classic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (brclassic == 1) {
if (kunde == 1)
{Bestellpanel.add(buttonx);buttonx.setVisible(true);brclassic++;
kpanel1.add(Bestellpanel);
}
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
addComponentsToPane1(frame.getContentPane());
addComponentsToPane2(frame.getContentPane());
Insets insets = frame.getInsets();
frame.setSize(1200 + insets.left + insets.right,
900 + insets.top + insets.bottom);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I made a simple example using two JFrames, it could be done with any type of container/layout really.
package helloworld;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
/**
* Created on 4/30/17.
*/
public class SwapPanel {
public static void main(String[] args){
JPanel panel = new JPanel();
panel.add(new JLabel("mover"));
JFrame a = new JFrame("frame a");
JButton aButton = new JButton("swap");
JFrame b = new JFrame("frame b");
JButton bButton = new JButton("swap");
bButton.setEnabled(false);
a.getContentPane().add(aButton, BorderLayout.SOUTH);
a.getContentPane().add(panel, BorderLayout.CENTER);
b.getContentPane().add(bButton, BorderLayout.SOUTH);
aButton.addActionListener(evt->{
if(aButton.isEnabled()){
aButton.setEnabled(false);
a.getContentPane().remove(panel);
b.getContentPane().add(panel, BorderLayout.CENTER);
bButton.setEnabled(true);
a.pack();
b.pack();
a.repaint();
b.repaint();
}
});
bButton.addActionListener(evt->{
if(bButton.isEnabled()){
bButton.setEnabled(false);
b.getContentPane().remove(panel);
a.getContentPane().add(panel, BorderLayout.CENTER);
aButton.setEnabled(true);
a.pack();
b.pack();
a.repaint();
b.repaint();
}
});
a.pack();
a.setVisible(true);
b.pack();
b.setVisible(true);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
The main thing that needs to be done, first remove the panel from one layout/container, then add the component panel to another container, and finally validate/repaint.
Related
I am trying to get the mouse coordinates display in the panel but each time I move the cursor the message and new coordinates are being displayed on the previous one.I am using MouseMotionListener with JPanel. I can't figure out the problem.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseMotionListener;
public class Main extends JPanel implements MouseMotionListener {
public JLabel label;
public static void main(String[] args) {
new Main();
JFrame frame = new JFrame();
frame.setTitle("MouseCoordinates");
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(new Main());
frame.setVisible(true);
}
public Main() {
setSize(400, 400);
label = new JLabel("No Mouse Event Captured", JLabel.CENTER);
add(label);
addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e) {
label.setText("Mouse Cursor Coordinates => X:" + e.getX() + " |Y:" + e.getY());
}
public void mouseDragged(MouseEvent e) {}
}
You're creating Main twice.
public class Main extends JPanel implements MouseMotionListener {
public JLabel label;
public static void main(String[] args) {
Main m = new Main();// create an object and reference it
JFrame frame = new JFrame();
frame.setTitle("MouseCoordinates");
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(m);
frame.setVisible(true);
}
//...
your problem was creating the Main object twice (which is a jpanel) and then the writing appeared twice. if you give the Main object a reference, then your problem should be fixed.
I am trying to create a GUI with a couple of buttons and a drawing area.
it seems to be working except the drawing area is very small and not in the right location.
here is my code:
public class ssGUI extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
protected JButton b1, bConnect, bDisconnect, b2;
protected JPanel canvas;
public ssGUI() {
// run button
b1 = new JButton("do something");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING);
b1.setMnemonic(KeyEvent.VK_D);
b1.addActionListener(this);
b1.setEnabled(false);
// connect button
bConnect = new JButton("Connect");
bConnect.setMnemonic(KeyEvent.VK_E);
bConnect.addActionListener(this);
bConnect.setEnabled(true);
// disconnect button
bDisconnect = new JButton("Disconnect");
bDisconnect.setMnemonic(KeyEvent.VK_E);
bDisconnect.addActionListener(this);
bDisconnect.setEnabled(false);
// clean nmea data button
b2 = new JButton("do something else");
b2.setMnemonic(KeyEvent.VK_E);
b2.addActionListener(this);
b2.setEnabled(false);
// drawing panel
canvas = new JPanel();
canvas.setBackground(Color.white);
add(b1); add(bConnect); add(bDisconnect); add(b2); add(canvas, BorderLayout.CENTER);
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Range Adjustment GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ssGUI newContentPane = new ssGUI();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
frame.setLocation(500, 500);
frame.setSize(500, 500);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}}
this is how my GUI looks like:
as you can see, the "canvas" is very small and at the side of the buttons.
i need it to be placed underneath them and fill frame area.
can anyone help me to resolve that problem?
thank you for your help.
Set your main panel's layout to BorderLayout.
Create a panel just for your buttons and add this panel to your main panel's NORTH position.
Here is a working example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ssGUI extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
protected JButton b1, bConnect, bDisconnect, b2;
protected JPanel canvas;
public ssGUI() {
setLayout(new BorderLayout());
// run button
b1 = new JButton("do something");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING);
b1.setMnemonic(KeyEvent.VK_D);
b1.addActionListener(this);
b1.setEnabled(false);
// connect button
bConnect = new JButton("Connect");
bConnect.setMnemonic(KeyEvent.VK_E);
bConnect.addActionListener(this);
bConnect.setEnabled(true);
// disconnect button
bDisconnect = new JButton("Disconnect");
bDisconnect.setMnemonic(KeyEvent.VK_E);
bDisconnect.addActionListener(this);
bDisconnect.setEnabled(false);
// clean nmea data button
b2 = new JButton("do something else");
b2.setMnemonic(KeyEvent.VK_E);
b2.addActionListener(this);
b2.setEnabled(false);
// drawing panel
canvas = new JPanel();
canvas.setBackground(Color.white);
JPanel topPanel = new JPanel();
topPanel.add(b1);
topPanel.add(bConnect);
topPanel.add(bDisconnect);
topPanel.add(b2);
add(topPanel, BorderLayout.NORTH);
add(canvas, BorderLayout.CENTER);
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Range Adjustment GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ssGUI newContentPane = new ssGUI();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
frame.setLocation(500, 500);
frame.setSize(500, 500);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
This is another problem with the layouts.
JPanel has a default layout and I don't see you setting the layout for ssGUI class. Hence the default layout (FlowLayout) will be used.
All components added to ssGUI will be arranged in a linear fashion in a row for as much as it can hold. When your component exceed the width, it will be placed to the next row.
You can consider using a layout for your main panel ssGUI. A GridBagLayout will probably give you what you want.
I am new to Swing programming. And I m trying to develop a desktop application.
First all I need to create a login window, which should not be draggable and its position must be in center of the screen.
So by learning , I have created a window by the following code:
import com.sun.awt.AWTUtilities;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Newframe {
private JLabel label;
JFrame frame;
JButton btn;
Newframe(){
prepareGUI();
}
public static void main(String arg[]) {
Newframe n=new Newframe();
}
public void prepareGUI(){
frame=new JFrame();
frame.setUndecorated(true);
frame.setSize(300, 300);
frame.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
}
}
Now, I want to add components (e.g textfields, labels, buttons, etc...) to this created frame.
I m trying to add the components to the frame by initialize the components and add them to the frame ( by this frame.add(jbutton)) , but components are not going to add to the created frame...
Can any one help me for this?
frame.getContentPane().add(component)
Note this may vary depending on the layout you use.
Also, it'd be better to put the UI in the Event Dispatch Thread, with this:
public static void main(String arg[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Newframe n=new Newframe();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
You can use below code to add component to the frame and to center the frame you can use frame.setLocationRelativeTo(null);.
public class Newframe {
private JLabel label;
private JTextField txt;
JFrame frame;
JButton btn;
Newframe() {
prepareGUI();
}
public static void main(String arg[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
Newframe n = new Newframe();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void prepareGUI() {
frame = new JFrame();
frame.setLayout(null);
frame.setSize(300, 300);
frame.setUndecorated(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
// frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
label = new JLabel("Name");
label.setBounds(10, 10, 100, 20);
frame.add(label);
txt = new JTextField();
txt.setBounds(50, 10, 100, 20);
frame.add(txt);
btn = new JButton("OK");
btn.setBounds(40, 40, 80, 20);
frame.add(btn);
}
}
I want to increase the value of my counter to 10 when I click on a button.
It works so far, but I don't know how to get the new counted value globally to
use it in other methods.
Here is a simple code:
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class Test {
public JTextPane textpane=new JTextPane();
public JPanel panel;
public JButton button = new JButton("count!");
public static int counter=10;
Test() {
addListener();
panel = new JPanel(new BorderLayout());
panel.add(textpane, BorderLayout.CENTER);
panel.add(button, BorderLayout.SOUTH);
createFrame();
}
private void addListener() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
counter=counter+10;
textpane.setText("counter is set to -> "+counter);
}
});
System.out.println("new value >> "+counter);
}
private void createFrame() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(400, 200);
frame.setVisible(true);
}
public static void main(String[] args) { new Test(); }
}
How can I get the new counter value globally?
I am trying to remove a JPanel not hide it but i can't find anything that works.
This is the code in the panel that needs to remove itself when a button is pressed:
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Frame frame = new Frame(); //referencing to my JFrame class (this class is a JPanel)
//need to remove this panel on this line
frame.ThreeD(); // adds a new panel
}
});
UPDATED
This is the full code:
package ThreeD;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import Run.Frame;
public class Launcher extends JPanel{
private JButton play, options, help, mainMenu;
private Rectangle rplay, roptions, rhelp, rmainMenu;
private int buttonWidthLocation, buttonWidth, buttonHeight;
private int width = 1280;
public Launcher() {
this.setLayout(null);
drawButtons();
}
private void drawButtons() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
play = new JButton("Play");
options = new JButton("Options");
help = new JButton("Help");
mainMenu = new JButton("Main Menu");
buttonWidthLocation = (width / 2) - (buttonWidth / 2);
buttonWidth = 80;
buttonHeight = 40;
rplay = new Rectangle(buttonWidthLocation, 150, buttonWidth, buttonHeight);
roptions = new Rectangle(buttonWidthLocation, 300, buttonWidth, buttonHeight);
rhelp = new Rectangle(buttonWidthLocation, 450, buttonWidth, buttonHeight);
rmainMenu = new Rectangle(buttonWidthLocation, 600, buttonWidth, buttonHeight);
play.setBounds(rplay);
options.setBounds(roptions);
help.setBounds(rhelp);
mainMenu.setBounds(rmainMenu);
add(play);
add(options);
add(help);
add(mainMenu);
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Frame frame = new Frame();
//need to remove this panel here
frame.ThreeD();
}
});
options.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("options");
}
});
help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("help");
}
});
mainMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("mainMenu");
}
});
}
}
And this is my Frame class:
package Run;
import javax.swing.*;
import ThreeD.Display;
import ThreeD.Launcher;
import TowerDefence.Window;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Frame extends JFrame{
public static String title = "Game";
/*public static int GetScreenWorkingWidth() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
}*/
/*public static int GetScreenWorkingHeight() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
}*/
//public static Dimension size = new Dimension(GetScreenWorkingWidth(), GetScreenWorkingHeight());
public static Dimension size = new Dimension(1280, 774);
public static void main(String args[]) {
Frame frame = new Frame();
System.out.println("Width of the Frame Size is "+size.width+" pixels");
System.out.println("Height of the Frame Size is "+size.height+" pixels");
}
public Frame() {
setTitle(title);
setSize(size);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ThreeDLauncher();
}
public void ThreeDLauncher() {
Launcher launcher = new Launcher();
add(launcher);
setVisible(true);
}
public void TowerDefence() {
setLayout(new GridLayout(1, 1, 0, 0));
Window window = new Window(this);
add(window);
setVisible(true);
}
public void ThreeD() {
BufferedImage cursor = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(0, 0), "blank");
getContentPane().setCursor(blank);
Display display = new Display();
add(display);
setVisible(true);
display.start();
}
}
Basically - you are creating new instance of Frame in line:
Frame frame = new Frame(); //referencing to my JFrame class (this class is a JPanel)
New instance of Frame is not visible, and you're try to remove your Launcher from not visible new Frame. But this is wrong - you should remove Launcher from Frame that you created previously in main function (that is: parent of Launcher component).
Here goes an example:
public class TestFrame extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TestFrame frame = new TestFrame();
frame.getContentPane().add(new MyPanel(frame));
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
And MyPanel class:
public class MyPanel extends JPanel {
public MyPanel(final TestFrame frame) {
JButton b = new JButton("Play");
add(b);
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Container pane = frame.getContentPane();
pane.remove(MyPanel.this);
JPanel otherPanel = new JPanel();
otherPanel.add(new JLabel("OtherPanel"));
pane.add(otherPanel);
pane.revalidate();
}
});
}
}
In your example you should add a reference to Frame in your Launcher constructor:
public Launcher(Frame frame) {
this.frame = frame;
...
Init Launcher:
public void ThreeDLauncher() {
Launcher launcher = new Launcher(this);
and use:
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//need to remove this panel here
frame.getContentPane().remove(Launcher.this);
frame.ThreeD();
}
});
Say your panel is myPanel you can remove it from the main frame by:
frame.getContentPane().remove(myPanel);