I created class NewProject extends JInternalFrame. Then I create New...Action named "NEW", localised in File menu. I put code NewProject p = new NewProject(); p.setVisible(true); to the ActionPerformed method of the action.
But when I run the module and click "NEW" in file menu, nothing appears.
Where can be problem?
EDIT:
I partially solved it by code:
public void actionPerformed(ActionEvent e) {
JInternalFrame f = new JInternalFrame();
f.setSize(500, 500);
f.setVisible(true);
JDesktopPane p = new JDesktopPane();
p.add(f);
//WindowManager.getDefault().getMainWindow().setTitle("fFF");
WindowManager.getDefault().getMainWindow().add(p)
}
but GUI is broken. When I create new internal frame, the black background appears as I move by that frame.
Any idea how to solve it?
The customary Container for JInternalFrame is JDesktopPane. The article How to Use Internal Frames outlines the essentials, and you may like this short example of using Action and JMenu in this context.
Although the NetBean's GUI editor is appealing, you may want to become more comfortable using Swing components first.
Addendum: You can't add one Top-Level Container like JFrame to another like JDesktopPane, but you can add any number of JInternalFrame instances to a JDesktopPane. Try the demo to see how it works.
Addendum: Ah, you mean NetBeans Platform. Sorry, I've not used it.
I think the answer you are looking for is here: https://blogs.oracle.com/geertjan/jdesktoppane,-jinternalframe,-and-topcomponent
There Geertjan Wielenga show an example using a TopComponent with a JDesktopPane inside, where you can attach some JInternalFrame.
...
...
...
private JDesktopPane jdpDesktop;
private int openFrameCount = 0;
public DemoTopComponent() {
initComponents();
setName(NbBundle.getMessage(DemoTopComponent.class, "CTL_DemoTopComponent"));
setToolTipText(NbBundle.getMessage(DemoTopComponent.class, "HINT_DemoTopComponent"));
setLayout(new BorderLayout());
jdpDesktop = new JDesktopPane();
createFrame(); // Create first window
createFrame(); // Create second window
createFrame(); // Create third window
//Add the JDesktop to the TopComponent
add(jdpDesktop);
}
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
class MyInternalFrame extends JInternalFrame {
int xPosition = 30, yPosition = 30;
public MyInternalFrame() {
super("IFrame #" + (++openFrameCount), true, // resizable
true, // closable
true, // maximizable
true);// iconifiable
setSize(300, 300);
setLocation(xPosition / openFrameCount, yPosition / openFrameCount);
// Add some content:
add(new JLabel("hello IFrame #" + (openFrameCount)));
}
}
...
...
...
Related
I am trying to write an application that get video frames, process them and then display them in JPanel as images. I use the OpenCV library to get video frames (one by one), then they are processed and after that displayed on the screen (to get the effect of playing video).
I created the GUI using Java Swing. A window application is created with the necessary buttons and a panel to display the video. After clicking "START", a method playVideo is called, which takes video frames from the selected video, modifies them and displays them in the panel. My code looks like this:
public class HelloApp {
private JFrame frame;
private JPanel panel;
final JLabel vidpanel1;
ImageIcon image;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
HelloApp window = new HelloApp();
window.frame.setVisible(true);
}
});
}
public void playVideo() throws InterruptedException{
Mat inFrame = new Mat();
VideoCapture camera = new VideoCapture();
camera.open(Config.filename);
while (true) {
if (!camera.read(inFrame))
break;
Imgproc.resize(inFrame, inFrame, new Size(Config.FRAME_WIDTH, Config.FRAME_HEIGHT), 0., 0., Imgproc.INTER_LINEAR);
... processing frame
ImageIcon image = new ImageIcon(Functions.Mat2bufferedImage(inFrame)); // option 0
vidpanel1.setIcon(image);
vidpanel1.repaint();
}
}
public HelloApp() {
frame = new JFrame("MULTIPLE-TARGET TRACKING");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);//new FlowLayout()
frame.setResizable(false);
frame.setBounds(50, 50, 800, 500);
frame.setLocation(
(3 / 4) * Toolkit.getDefaultToolkit().getScreenSize().width,
(3 / 4) * Toolkit.getDefaultToolkit().getScreenSize().height
);
frame.setVisible(true);
vidpanel1 = new JLabel();
panel = new JPanel();
panel.setBounds(11, 39, 593, 371);
panel.add(vidpanel1);
frame.getContentPane().add(panel);
JButton btnStart = new JButton("START / REPLAY");
btnStart.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
playVideo();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
});
}
}
I tried to delete the old panel and create a new one every time when button "START" is clicked, but it didn't work. Also I tried before running method playVideo to clean all the panel with methods:
panel.removeAll();
panel.repaint();
playVideo();
And to be honest I don't know what's wrong. The GUI is created, frames are taken and processed, but the panel displays only the last frame. I would be grateful for any advice :)
First of all, a proof it can actually work, somehow, with your code.
Here I read JPG images located in the resources folder, but it actually doesn't really matter.
Your code is a bit messy too. Where are you attaching the btnStart JButton to the outer panel? You need to understand how to layout components too.
You have a main JFrame, and a root JPanel which needs a layout. In this case we can opt for a BorderLayout.
panel = new JPanel(new BorderLayout());
Then we add our components.
panel.add(btnStart, BorderLayout.PAGE_START);
panel.add(vidpanel1, BorderLayout.CENTER);
Now coming to your issue, you say
The gui is created, frames are taken and processed, but panel display only the last frame
I don't know how much the "last frame" part is true, mostly because you're running an infinite - blocking - loop inside the Event Dispatch Thread, which will cause the GUI to freeze and become unresponsive.
In actionPerformed you should actually spawn a new Thread, and inside playVideo you should wrap
ImageIcon image = new ImageIcon(Functions.Mat2bufferedImage(inFrame));
vidpanel1.setIcon(image);
vidpanel1.repaint(); // Remove this
in EventQueue.invokeAndWait, such as
// Process frame
...
// Update GUI
EventQueue.invokeAndWait(() -> {
ImageIcon image = new ImageIcon(Functions.Mat2bufferedImage(inFrame));
vidpanel1.setIcon(image);
});
I am not new to Java and OOP but I'm new to swing.
I want to write a software for building pedigrees. That means: right click into the middle of the drawing area and choose "New -> Man". A rectangle appears where I have right-clicked. Then I click on the rectangle and choose "New -> Sibling -> Woman" and the pedigree expands dynamically with a circle that is connected to the rectangle. You get the idea.
Additionally I need to save information for each node of the pedigree. Such as "mutation in gene x: positive".
I thought this must be perfect for OOP. I need every node of my pedigree to be an instance which draws itself into the drawing area. So ... a Jpanel? I extend JPanel, I give that class some attributes (such as "int mutationX = 1") and a method to add itself to the JFrame. At the moment I am only trying to add a rectangle into the middle of the screen via the menu. Easy step for a swing beginner. But the desired rectangle doesn't show up. So basically my questions are:
Am I even following the right approach of solving what I'm trying to achieve?
Why doesn't the rectangle show up?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HelloWorld
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Hello World");
JPanel mainpanel = new JPanel();
JScrollPane scroll = new JScrollPane(mainpanel);
frame.add(scroll);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.exit(0);
}
}
);
file.add(exit);
JMenuItem newMember = new JMenuItem("Add");
newMember.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
FamilyMember fm = new FamilyMember();
mainpanel.add(fm);
mainpanel.revalidate();
}
}
);
file.add(newMember);
frame.setLocation(400, 100);
frame.setPreferredSize(new Dimension(500,500));
frame.pack();
frame.setVisible(true);
}
}
public class FamilyMember extends JPanel {
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(230,80,10,10);
g.setColor(Color.RED);
g.fillRect(230,80,10,10);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
}
For the second point (why, a minima, it does not work/show any rectangle), you could take a look at Oracle docs (from more precise to broader concern) :
FlowLayout
Layout managers
Swing panels
In short,
your JPanel instance mainpanel has a FlowLayout as layout manager (which is the default for a new instance)
a flow layout will use each child preferred size
and you force the preferred size of your FamilyMember to 50x50
but you want to draw only south-east of 230,80 which is not in the 50x50 area.
=> it won't show.
For the first point, I'd say first : separate the model from the view.
I have tried in many ways to add an internal frame to my existing one. I have tried with and without JPanel. But nothing worked and I don´t have a clue why. Anyone?
public class Menu_new extends JFrame{
private BufferedImage background = null;
public Menu_new() {
try {
background = ImageIO.read(new File("pics_1/hallo.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
JDesktopPane desktop = new JDesktopPane();
JInternalFrame inside = new JInternalFrame(("Data"), true, true, true, true);
desktop.add(inside);
inside.setBounds(50, 50, 300, 500);
inside.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inside.setVisible(true);
JLabel label = new JLabel("Your name");
inside.add(label);
JTextField text = new JTextField(10);
inside.add(text);
Icon icon = new ImageIcon("pics_1/Button.png");
JButton start = new JButton(icon);
start.setBorder(BorderFactory.createEmptyBorder());
inside.add(start);
inside.moveToFront();
this.add(desktop);
}
public void paintComponent(Graphics g) {
g.drawImage(background, 0, 0, this);
}
}
Your method of adding the components to the internal frame is wrong.
The default layout manager (for internal frame as well as for JFrame) is BorderLayout and therefore requires that you specify where you want to place your components. (As a special case if you only add a single component it seems to work without specifying a constraint).
Your code to add the components should look like this:
inside.add(label, BorderLayout.PAGE_START);
...
inside.add(text, BorderLayout.CENTER);
...
inside.add(start, BorderLayout.PAGE_END);
As an additional note, this inside.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); has no effect, since JFrame.EXIT_ON_CLOSE is not valid for internal frames.
I used the NteBeans' GUI making tool.
It created a frame.
I want to close this frame using a button.
I know that I need to use "my_frame_name.dispose();" to close a frame.
But the problem is I cant find the name of the frame in the "Source" tab.
I think this is because, NetBeans created this frame and its code automatically.
Could anyone tell me how to close this frame using a code or a function, please?
Please don't tell me I have to recode everything, because I have multiple frames like tis one and don't have the luxury of time.
You can try this one also
Here program is using container.getParent() method to find out the top most JFrame.
public static void main(String[] a) {
JFrame frame = new JFrame();
JPanel p = new JPanel();
final JButton btn = new JButton("close");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Container parent = btn;
while ((parent = parent.getParent()) != null) {
System.out.println(parent.getClass().getName());
if (parent instanceof JFrame) {
((JFrame) parent).setVisible(false);
} else {
parent = parent.getParent();
}
}
}
});
p.add(btn);
frame.getContentPane().add(p);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
How to change Look and Feel for the ToolBar, the top menu (where the buttons to close, minimize, maximize)
Is it possible to like something change? (Add, delete button, assign a background)
What is import is required to create it?
you can set the background image of a JButton you could have a look at this: Swing Tutorial: JButton which shows the use of the new JButton(String text,ImageIcon imgIco) to create a JButton with an ImageIcon and String.
To set the colour of the background and text you could use setBackground(Color c) and setForeground(Color c)
or
Alternatively just customize Look and Feel color scheme by setting an appropriate supported Look and Feel and then change the color scheme/size etc of its components thier are hundreds of things you can change for every component see this for them all.
To customize the Exit, Minimize and Maximize Toolbar buttons this can also be using the Look and Feel ( Custom design for Close/Minimize buttons on JFrame ):
import java.awt.BorderLayout;
import javax.swing.*;
public class FrameCloseButtonsByLookAndFeel {
FrameCloseButtonsByLookAndFeel() {
String[] names = {
UIManager.getSystemLookAndFeelClassName(),
UIManager.getCrossPlatformLookAndFeelClassName()
};
for (String name : names) {
try {
UIManager.setLookAndFeel(name);
} catch (Exception e) {
e.printStackTrace();
}
// very important to get the window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame(UIManager.getLookAndFeel().getName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel gui = new JPanel(new BorderLayout());
f.setContentPane(gui);
JTree tree = new JTree();
tree.setVisibleRowCount(4);
gui.add(tree, BorderLayout.LINE_START);
gui.add(new JScrollPane(new JTextArea(3,15)));
JToolBar toolbar = new JToolBar();
gui.add(toolbar, BorderLayout.PAGE_START);
for (int ii=1; ii<5; ii++) {
toolbar.add(new JButton("Button " + ii));
if (ii%2==0) {
toolbar.addSeparator();
}
}
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new FrameCloseButtonsByLookAndFeel();
}
});
}
}
Well the easiest way to change frame titlebar look is to set LookAndFeel before you create your frame.
Probably this is what you are looking for - http://www.jtattoo.net/ScreenShots.html