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);
}
}
Related
i'm using Vaadin GridLayout in a project, and i want to make it big as the screen so i can arrange the UI of the app.
i'm a beginner in java, i tried .setWidth("100%"); , and also tried
to get the Screen resolution with Toolkit, but it did'nt help.
i've already searched around but none could've helping me.
can anyone give me a hint?
here is a part of it:
filter.setWidth("100%"); //TextField
entryList.setWidth("100%"); //Table
entryList.setHeight("100%");
blayout.addComponents(addNew,delete); //HorizontalLayout
blayout.setStyleName("buttons");
Styles style=Page.getCurrent().getStyles();
style.add(".buttons {float:right;}");
layout1.setMargin(true); //VerticalLayout
layout1.setSpacing(true);
layout1.addComponents(filter,entryList,blayout);
layout1.setWidth("100%");
layout1.setHeight("100%");
layout1.setSizeFull();
layout2.setSizeFull(); //VerticalLayout
layout2.addComponent(form); // form is a variable of an other class
layout2.setWidth("100%");
layout2.setHeight("100%");
MPanel panel1=new MPanel("Search");
MPanel panel2=new MPanel("Edit");
panel1.setSizeFull();
panel2.setSizeFull();
panel1.setContent(layout1);
panel2.setContent(layout2);
panel1.setWidth("100%");
panel1.setHeight("100%");
panel2.setWidth("100%");
panel2.setHeight("100%");
mlayout.setRows(1); //GridLayout
mlayout.setColumns(2);
mlayout.addComponent(panel1,0,0);
mlayout.addComponent(panel2,1,0);
mlayout.setSpacing(true);
addComponents(new MVerticalLayout(menue, mlayout)); //Creates Output
I am trying to create a JPanel to display when a user clicks a button within my main JFrame. In Netbeans I first used the wizard to add a new JPanel to my project, I then used the GUI creator to fill in all the content. I am not trying to display the JPanel with the following code
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt)
{
Account selAcc = getSelectedAccount();
if(selAcc != null)
{
AccountView accPanel = new AccountView(Account.getDeepCopy(selAcc));
accPanel.setVisible(true);
}
else
ShowMessage("Please select an account to view");
}
But nothing happens, no error is thrown and the JPanel is not shown. So I then changed the JPanel to a JFrame (Netbeans didn't complain). When I try again with the same code I receive the error GroupLayout can only be used with one Container at a time.
How can I display my JPanel/JFrame?
To change views within a Swing GUI, use a CardLayout as this is a much more robust and reliable way to do this.
Don't try to blindly "change a JPanel to a JFrame". It looks like you're just guessing here.
GroupLayout can't be reused as the error message is telling you. Likely this error comes from the point above. If you avoid trying to make a JFrame out of a JPanel, the error message will likely go away. As an aside, GroupLayout is not easily used manually, especially if you're trying to add components to an already rendered GUI.
So for instance, if your program had a JPanel say called cardHolderPanel, that used a CardLayout, this held by a variable say called cardLayout, and you've already added a "card" JPanel to this holder for accounts, say called accPanel, and if the accPanel had a method to set its currently displayed account, say setAccount(Accoint a), you could easily swap views by calling the CardLayout show(...) method, something like:
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt) {
Account selAcc = getSelectedAccount();
if(selAcc != null) {
accPanel.setAccount(Account.getDeepCopy(selAcc));
cardLayout.show(cardHolderPanel, "Account View");
}
else {
showErrorMessage("Please select an account to view");
}
}
I'm trying to make a simple pie chart appear on a jpanel in netbeans with JFreeChart and got this:
public void createPieChart()
{
DefaultPieDataset myPie = new DefaultPieDataset();
myPie.setValue("Apples",new Integer(12));
myPie.setValue("Oranges",new Integer(23));
myPie.setValue("Mangos",new Integer(7));
myPie.setValue("Pears",new Integer(22));
JFreeChart myChart = ChartFactory.createPieChart3D("Damo's Fruit Sales", myPie,true,true,true);
PiePlot3D pie3D = (PiePlot3D)myChart.getPlot();
ChartPanel myPanel = new ChartPanel(myChart);
lowerMain_PNL.removeAll();
lowerMain_PNL.add(myPanel,BorderLayout.CENTER);
lowerMain_PNL.revalidate();
}
I get no compiler errors and when it runs the window appears with the button, but when I press the button my pie chart doesn't appear. Anyone know what I could be missing?
Check the layout manager of lowerMain_PNL. Netbeans form designer uses GroupLayout by default, so unless you changed it, that's what you got. Adding to a container using GroupLayout at run time is tricky, especially if the component contains more than one subcomponent (And requires adding components to the layout, instead of using the usual add() methods).
Change it to BorderLayout instead, since you are using BorderLayout constraints.
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.
New to NetBeans and just noticed that in the File >> Project Properties >> Application dialog there is a text field labeled Splash Screen that allows you to specify a path to an image that you would like displayed when your program is launching.
I want to customize the way my splash screen works (adding a progress bar, etc.) and would like to code it from the ground up but don't know where to start. What are the best practices for Java/Swing-based splash screens?
Thanks for any and all input!
The project properties -> Application -> Splash Screen allows you to add an image to an application. This property sets a value in the MANIFEST.MF called SplashScreen-Image: e.g. SplashScreen-Image: META-INF/GlassFish316x159.jpg This property will automatically cause the image to display as a splash screen. It does not work inside NetBeans, and must be run outside the IDE.
There is a tutorial Splash Screen Beginner Tutorial that details how to use it more detail. The tutorial was done for NetBeans 6.8, but will work on 7.2.1 which is the latest at the time of this post.
I'm not sure how NetBeans does it, but Splash Screens are supported by the JRE since version 6. See http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/splashscreen/
Splash screen is just a instance of java.awt.Window or undecorated javax.swing.JFrame.
To create window just say new Window(null), then set size and position (using tookit you can calculate where the screen center is) and then say window.setVisible(true)
Due to this is your own window you can do what you want: set layout, image, add process bar to the SOUTH etc.
You can also use JFrame: new JFrame().setUndecorated(true)`
There are a couple of ways to do this.
To do a simple splash screen (an image) you can specify this in the command line of you java application.
Here is a simple example
java -splash:<file name> <class name>
However, if you want a progress bar, you are going to have to do something a little more complicated, and write some code yourself. This is done in the following way.
Create a JWindow (or Window or undecorated JFrame) component with your splash screen elements
Set it to visible
Do the rest of your Swing GUI startup code
Set your JFrame to visible, then immediately follow with setting the JWindow to visible(false)
This should show the splash almost immediately, and then hide once the your application is fully loaded.
To see some splash screen code, take a look here. The implementation in the link only shows how to achieve what you can with the -splash command, but it will give you a good start to also include the progress bar that you requested.
I hope this helps you, it is a small example of how to create yourself a simple splash screen using a dummy Progress Bar:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SplashScreen extends JWindow
{
private static JProgressBar progressBar = new JProgressBar();
private static SplashScreen execute;
private static int count;
private static Timer timer1;
public SplashScreen()
{
Container container = getContentPane();
container.setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new javax.swing.border.EtchedBorder());
panel.setBackground(new Color(255,255,255));
panel.setBounds(10,10,348,150);
panel.setLayout(null);
container.add(panel);
JLabel label = new JLabel("Hello World!");
label.setFont(new Font("Verdana",Font.BOLD,14));
label.setBounds(85,25,280,30);
panel.add(label);
progressBar.setMaximum(50);
progressBar.setBounds(55, 180, 250, 15);
container.add(progressBar);
loadProgressBar();
setSize(370,215);
setLocationRelativeTo(null);
setVisible(true);
}
public void loadProgressBar()
{
ActionListener al = new ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
count++;
progressBar.setValue(count);
if (count == 50){
timer1.stop();
execute.setVisible(false);
//load the rest of your application
}
}};
timer1 = new Timer(50, al);
timer1.start();
}
public static void main (String args[]){
execute = new SplashScreen();
}
}
Cheers!
Also consider to build your application on top of the NetBeans Platform (a Swing-based RCP). One of the many benefits: it comes with a customizable splash screen with progress bar.
Sample progress bar:
http://platform.netbeans.org/tutorials/nbm-paintapp.html#wrappingUp
Port a Swing application to the NetBeans Platform:
http://platform.netbeans.org/tutorials/60/nbm-porting-basic.html
Further links:
http://netbeans.org/features/platform/index.html
http://netbeans.org/features/platform/all-docs.html
If your application is build using NetBeans Platform, then here's a tutorial about splash screen customisation: http://wiki.netbeans.org/Splash_Screen_Beginner_Tutorial
There is a sample Javafx equivalent of Splash screen. However this splash screen is basically a java swing applet that is called from javafx to be displayed to the user and simulates more or less eclipse and netbeans splash screen using progress bar and titles for the loaded contents. This is the link.
You must be able to get the code and separate out the splash screen code written in java swings and use it for yourself.
This is a custom java swings splash screen. and hence to center the splash screen it uses the traditional
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.width / 2 - (labelSize.width / 2),
screenSize.height / 2 - (labelSize.height / 2));