Ok so i am trying to get a 3 JPanel JFrame where right and left panel have a fixed width but are vertically re-sizable and a center panel that can be re-sized both horizontally and vertically.
Since standard LayoutManagers are terrible and simply annoying i have been told that the industry standard and easiest to work whit and handle is JGoodies. However seems that a lot of link on JGoodies website are dead regarding their examples / tutorials there is a 400 page PDF i dont want to read.
Anyhow i have started implementing FormLayout to my first UI_View and i ran in to a problem
package ppe.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.jgoodies.forms.layout.*;
public class UI_View extends JFrame
{
private JScrollPane right = new JScrollPane();
private JList browse = new JList();
public UI_View()
{
this.setTitle("Prototype MVC Arhitecture");
this.setMinimumSize(new Dimension(800, 600));
this.setExtendedState(this.MAXIMIZED_BOTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FormLayout layout = new FormLayout("right:pref, 7dlu","p, 1dlu");
layout.setColumnGroups(new int [][]{{1}});
JPanel content = new JPanel(layout);
CellConstraints c = new CellConstraints();
right.add(browse);
content.add(right, c.xy(1, 1));
this.add(content);
}
public static void main(String[] args)
{
new UI_View().setVisible(true);
}
}
You a missing a Jar file. JGoodies has several Jar files, make sure you have the ones you need.
MiG Layout is the best layout manager I've used, but I like JGoodies for its Binding and Validation libraries. You can find tutorial code samples in the older versions in the download archive.
What also might make your life easier is to use Eclipse with the WindowBuilder plugin. It's a layout tool that supports FormLayout.
Related
I have an assignment to show JFileChooser as part of a JFrame. So showing it as a dialog box is out.
I'm doing the most basic approach to adding it as a component to a yet invisible frame, and then the setVisible() call freezes instead of showing the frame.
What irks me the most is that one time out of ten the frame appears with the FileChooser just fine. This makes me think this is a concurrency issue.
Here's the minimal source code that still has the issue.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class ApplicationFrame extends JFrame {
JFileChooser fileChooser;
public ApplicationFrame(String frameName) {
super(frameName);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
fileChooser = new JFileChooser();
fileChooser.setControlButtonsAreShown(false);
panel.add(fileChooser, BorderLayout.CENTER);
getContentPane().add(panel);
}
}
public class lab7{
public static void main(String args[])
{
ApplicationFrame windowForApplication = new ApplicationFrame("lab7");
windowForApplication.setSize(600,600);
windowForApplication.setVisible(true);
}
}
If you put a println after the final setVisible, it doesn't get called.
If you comment out panel.add(), the frame displays just fine.
What else should I do to display the file chooser?
What irks me the most is that one time out of ten the frame appears with the FileChooser just fine.
All Swing component should be created on the Event Dispatch Thread. So the GUI creating code should be wrapped in a SwingUtilities.invokeLater(...).
Read the section from the Swing tutorial on Concurrency for more information and an example of how this is done.
Your code (as is) actually works for me without problem. I'm using JDK7 on Windows 7, so it could be a version/platform issue. Again make sure the code executes on the EDT.
Also, class names ("lab7") should start with an upper case character. Doesn't matter if this is a SSCCE or not, be consistent.
Well basically my image will not display, i'm almost certain it's my file path
(" C:\Users\Alex\Desktop\card.png" is what the image properties say is the file path but unless i put double slashes it confuses them for escape sequences. ) If someone has the answer it will be much appreciated. Here's my code:
import java.awt.*;
import javax.swing.*;
public class IDK extends JFrame {
public static void main(String args[]) {
new IDK();
}
public IDK(){
notsure();
}
public void notsure(){
setBounds(420,100,440,400);
setTitle("Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("Tell me something");
JLabel image = new JLabel();
label1.setText("New Text");
JTextField text = new JTextField("Insert text",30);
image.setIcon(new ImageIcon("C:\\Users\\Alex\\Desktop\\card.jpg"));
panel.add(label1,image);
panel.add(text);
add(panel);
validate();
panel.setBackground(Color.CYAN);
setVisible(true);
}
}
There is no problem with using "\" so long as it's escaped, as you have done, if your prefer you can use / instead and on Windows the JRE will correct for it.
How ever, you should be adding the image separately, for example...
image.setIcon(new ImageIcon("C:\\Users\\Alex\\Desktop\\card.jpg"));
panel.add(label1);
panel.add(image);
The way you are doing it now assumes that image is a layout constraint for label1, which it isn't.
You should try and avoid using absolute paths and learn to use relative paths and/or embed the resources within the application context, this makes it easier to locate these resources at runtime.
Create a folder in your project and call it i.e. 'resources' and put your image in that folder. This will ensure that given image is on your classpath and can be easily accessed.
Then you can just do :
new ImageIcon("resources/card.jgp")
Problem with accessing outside resources is that special characters needs to be escaped and also if you export the project and give it to someone else, he/she will not have the image required by software ;)
So I have made a Jframe with a lot of elements and buttons and things in it, but I am new to using NetBeans. Upon creating the java application a main class.java was created and upon adding the jframe another jframe.java was created. How do I get the main class to open, read, and run my jframe.java? I can upload the specific code if need be.
Thanks in advance
To call a certain method from another class, you must first create a new object for that class, like this:
Jframe frame = new Jframe();
frame.setVisible(true); //or whatever the method is in jframe.class
Maybe rename the actual class name from jframe to something like frameone. I've heard that naming classes the same as classes in the Java API will cause trouble.
Or, you could put it all in one class, with either two separate methods or put it all in the main method. If this doesn't help, then please paste the exact code on pastebin.org and give a link.
Look at this sample example and learn how to set frame visible
import java.awt.*;
import javax.swing.*;
public class exp{
public static void main(String args[]){
JFrame jf=new JFrame("This is JFrame");
JPanel h=new JPanel();
h.setSize(100,100);
h.add(new JButton("Button"));
h.add(new JLabel("this is JLabel"));
h.setBackground(Color.RED);
jf.add(h);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
Useful Links
Designing a Swing GUI in NetBeans IDE
Creating a GUI With Swing (As #MadProgrammer Commented)
Learning Swing with the NetBeans IDE
I'm new to this, but I got a form up. Woo hoo!
1) The project created my main function in japp1.java
2) I created a JFrame, file jfMain.java
3) While there was probably a way to reference it as it was, I didn't see how right away, so I moved it to a peer level with the japp1 file, both in a folder called japp1 which will cause them to get built together, having the same parent reference available.
src\
japp1\
japp1.java
jfMain.java
4) Then instead of creating a generic JFrame with a title, I created an instance of my class...
5) I gave it a size...
7) Then showed it...
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new japp1.jfMain();
frame.setPreferredSize(new Dimension(700, 500));
frame.pack();
frame.setVisible(true);
}
I had already put some code in my jframe... to show a messagedialog with JOptionPane from a mouseclick event on a button and set some text for some textfields.
Hope that helps.
So I've been doing a lot of searching today, trying to figure out how to play videos inside of a JFrame. The reason I want this is because, I'm making a game, and want the option to add movie clips, like in most every good game, such as GW2, Medal of Honor, etc.
So, in my searches, I found JMF, but was completely unable to use it. It is sort of frustrating, but whatever. So, my question is this: Is there a way to play videos WITHOUT installing any other jar's, exe's, etc? For example, run a simple code such as new JFrame(); sort of quick and easy? or is that not possible, but there is a way to do it long and complex?
I've also been looking at other stack overflow stuff, and none of it really fits what i want... If worst comes to worst, i'll just use Xuggler, but i'd rather not.
Also, based on this answer, I plan on making a game engine in the future, so this could be added to it, for additional value.
Thank you in advance :)
PLEASE NOTE: I am not looking for references to JMF, or anything else like that. I'm looking for things such as built in methods/classes to call, or long work arounds, that work pretty good and would be implementable in numerous environments.
EDIT: I was thinking of using JEditorPane, and embedding the video with html, but.... that hasnt been working for me... here's what i've tried there:
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
// jep.setContentType("text/html");
jep.setText("<html><video id=\"sampleMovie\" src=\"C:\\users\\austin\\desktop\\test.mp4\" controls></video></html>");
JScrollPane scrollPane = new JScrollPane(jep);
JFrame f = new JFrame("Test HTML");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setPreferredSize(new Dimension(800,600));
f.setVisible(true);
But this doesnt seem to be working... Please help!
JMF seems to have been around since 1997. No wonder then that, as you said, most of the Java-based players out there rely on it.
So I decided to take a trip back in time using Google's search options and found a result from Feb 2001: a very simple GNU-licensed MPEG-1 player implemented from the ground up using just Java and C. How about that? The following capture is from the website:
I know you emphasised you needed a solution with as little external code as possible. In this case, no additional libraries seem to be required but you have to do some compiling. Plus you're limited to MPEG-1. So, not exactly what you were looking for but perhaps worth a look.
Hope it helps!
if you have a File name "file":
import java.io.*;
import java.net.*;
import javax.swing.*;
//....
//....
//....
try{
mediaURL = file.toURI().toURL();
}
catch(Exception e){
}
if(mediaURL != null){
JFrame mediaTest = new JFrame();
MediaPanel m = new MediaPanel(mediaURL);
mediatTest.add(m);
mediaTest.setVisible(true);
}
Edited:
import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class MediaPanel extend JPanel{
public MediaPanel( URL mediaURL){
setLayout(new BorderLayout());
try{
Player mp = Manager.createRealizedPlayer( mediaURL);
Component video = mp.getVisualComponent();
Component controls = mp.getControlPaneComponent();
if(video != null){
add(video, BorderLayout.CENTER);
}
if(controls != null){
add(controls, BorderLayout.SOUTH);
}
mediaPlayer.start();
}
catch(Exception e){
}
}
}
Trying to set size of a JOptionPane but it's sticking with the same size. I've tried setPreferredSize and setSize but for some reason the JOptionPane is sticking with the same width and height.
Basically I have a bunch of text and it's being "cut off" because of the size of the window.
I'm actually using a port of the swing library in another language, so it could be a bug with their library - but according to the docs it should mirror the Java Swing calls.
Am I missing something?
edit - wanted to add that I create the joptionpane with JOptionPane.showInputDialog
edit again - i'm using ASwing (actionscript port of Java Swing - hence there might be api differences though it's supposed to be a
port...)
It is good idea to create a JPanel with prefereed size or JTextArea with JScrollPane scrollbars and add it to optionpan.
It's easy to do, and not only sets the size of the dialog but allows great flexibility of content.
JTextArea mytext = new JTextArea();
mytext.setText("mytextline1\nmytextline2\nmytextline3\nmytextline4\nmytextline5\nmytextline6");
mytext.setRows(5);
mytext.setColumns(10);
mytext.setEditable(true);
JScrollPane mypane = new JScrollPane(mytext);
Object[] objarr = {
new JLabel("Enter some text:"),
mypane,
};
JOptionPane Optpane = new JOptionPane(objarr, JOptionPane.PLAIN_MESSAGE);
see details
Check the layout manager of the container
Actually, found the fix. The ported library built the JOptionPane I guess slightly different than the Java version.
optionpane.getFrame().setSize()
(using ASwing - Actionscript port of Java Swing)
You can just as well override setSize by extending our Componemt:
public class Headerfield extends JLabel
{
public Headerfield(String text)
{
super(text);
}
public void setSize(int width, int height)
{
super.setSize(100, 20);
}
}