Sign in to follow this  
GarrettSF

[java] JavaGUI help

Recommended Posts

I have been working diligently on this GUI, learning as I go. After about 2 weeks, it comes time to find a new layout manager. I have been trying many different things but still can't get what I would like. http://img.photobucket.com/albums/v91/4wheelingman/GUIISSUEnew.jpg This is my current GUI. I would like the textfields to be just one line wide but I can't figure out how. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
I have been programming in Java for a while now. About 6 years now. I have made many GUI apps, both for fun (games) and at work. I was using the Visual Editor plugin for Eclipse, but that no longer works with either version 3.3 or the new 3.4. I tried netbeans at version 5.5, but I did not the their GUI editor because I could not edit the file. I do not know if is has improved with the new version.

So here is what I use now...
http://www.miglayout.com

I did spend a lot of time learning to use all the layout managers by hand, which has helped a great deal with understanding swing, so if you are just trying to learn, stick with the regular swing layouts and try to make it work. If you don't have time to learn them right now, use MigLayout.

If you want to post the code you are using to layout the components, I'm sure someone can show you how to make it look better.

Share this post


Link to post
Share on other sites
Is there any way I can just set the width of the textfield to a set value?

//SKModeler GUI version 2.0
//By Garrett

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.Toolkit;


public class skmIOv20 extends JFrame
//implements ActionListener
{
public skmIOv20()
{
JFrame mainSKMframe;
Container mainContainer;
JLabel LskpFileName, LfinalModelName, LsmdSceneName, LoutputDir;
JTextField TskpFileName, TfinalModelName, TsmdSceneName;
JButton outputDir;
JPanel outDirPanel, finalNamePanel, finalSKPPanel, finalScenePanel, mainPanel;
Dimension panelSize;

mainSKMframe = new JFrame("SKModeler GUI Version 2.0");
mainContainer = mainSKMframe.getContentPane();
mainContainer.setLayout(new BorderLayout());

outDirPanel = new JPanel();
outDirPanel.setLayout( new BoxLayout( outDirPanel, BoxLayout.LINE_AXIS));
LoutputDir = new JLabel("Output Folder");
outputDir = new JButton("Click here to set output path");
//outputDir.addActionListener(this);
outDirPanel.add( LoutputDir);
outDirPanel.add( outputDir);

finalNamePanel = new JPanel();
finalNamePanel.setLayout( new BoxLayout( finalNamePanel, BoxLayout.LINE_AXIS));
LfinalModelName = new JLabel( "Final Name");
TfinalModelName = new JTextField();
finalNamePanel.add( LfinalModelName);
finalNamePanel.add( TfinalModelName);

finalSKPPanel = new JPanel();
finalSKPPanel.setLayout( new BoxLayout( finalSKPPanel, BoxLayout.LINE_AXIS));
LskpFileName = new JLabel( "SKP file Name" );
TskpFileName = new JTextField();
finalSKPPanel.add( LskpFileName);
finalSKPPanel.add( TskpFileName);

finalScenePanel = new JPanel();
finalScenePanel.setLayout( new BoxLayout( finalScenePanel, BoxLayout.LINE_AXIS));
LsmdSceneName = new JLabel( "smd Scene Name\n with animations" );
TsmdSceneName = new JTextField();
finalScenePanel.add( LsmdSceneName);
finalScenePanel.add( TsmdSceneName);

mainPanel = new JPanel();
mainPanel.setLayout( new BoxLayout( mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.add( finalNamePanel);
mainPanel.add( finalSKPPanel);
mainPanel.add( finalScenePanel);

mainContainer.add( outDirPanel, BorderLayout.NORTH );
mainContainer.add( mainPanel, BorderLayout.CENTER );

mainSKMframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainSKMframe.pack();
mainSKMframe.setSize(400, 400);
mainSKMframe.setVisible(true);
}
/*
public void outputaddressButtonInfo(String outputPathAddress)
{

}
public void actionPerformed(ActionEvent e)
{
FileDialog outputDirPath = new FileDialog(new JFrame(), "Please choose a file:", FileDialog.SAVE);
outputDirPath.setFile("(.SKM)");
outputDirPath.setDirectory("C:/");
outputDirPath.show();
outputaddressButtonInfo(outputDirPath.getFile());

}
*/
public static void main(String[] args)
{
new skmIOv20();

}


}


What can I implement into that?

Share this post


Link to post
Share on other sites
setBounds gives you a static height and width. Now this may alter or be altered by other components, depending on the layout manager and what other components are sized, in terms of explicit setBounds.

Honestly, I use NetBeans for all my GUI designing and then copy the container code over to my real IDE, Eclipse.

Share this post


Link to post
Share on other sites
Quote:
Original post by GarrettSF
Is there any way I can just set the width of the textfield to a set value?


If you just want the text field to be a certain width, use the constructor that sets the column count...

JTextField jTextField = new JTextField(20);

Share this post


Link to post
Share on other sites
One thing I noticed when I compiled and ran your code is that you are using the pack method and then the setSize method? These things cancel each other out. The JavaDocs for the pack method say this:

Quote:
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated.


So by calling pack, you resize everything to be as small as possible. Then you set the window size to 400x400, which undoes the pack. If you comment out the setSize it looks much better.

Share this post


Link to post
Share on other sites
I reworked this using MigLayout. I also changed you original code a little bit. You do not need another JFrame class because you are subclassing the JFrame. A perfect example is that you used JFrame.EXIT_ON_CLOSE. You can just use EXIT_ON_CLOSE without the JFrame in front because you ARE a JFrame. Keep reading that last sentence until you get it!!!

Anyway, that is why the pack() method can be called without anything else.

Enjoy!!!


//SKModeler GUI version 2.0
//By Garrett

import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;


public class skmIOv20 extends JFrame
{
public skmIOv20 ()
{
JLabel LoutputDir = new JLabel("Output Folder");
JButton outputDir = new JButton("Click here to set output path");
JLabel LfinalModelName = new JLabel( "Final Name");
JTextField TfinalModelName = new JTextField();
JLabel LskpFileName = new JLabel( "SKP file Name" );
JTextField TskpFileName = new JTextField();
JLabel LsmdSceneName = new JLabel( "smd Scene Name\n with animations" );
JTextField TsmdSceneName = new JTextField();

Container mainContainer = getContentPane();
mainContainer.setLayout( new MigLayout() );
mainContainer.add( LoutputDir, "align right" );
mainContainer.add( outputDir, "growx, wrap" );
mainContainer.add( LfinalModelName, "align right" );
mainContainer.add( TfinalModelName, "growx, wrap" );
mainContainer.add( LskpFileName, "align right" );
mainContainer.add( TskpFileName, "growx, wrap" );
mainContainer.add( LsmdSceneName, "align right" );
mainContainer.add( TsmdSceneName, "growx, wrap" );

setDefaultCloseOperation( EXIT_ON_CLOSE );
setTitle( "SKModeler GUI Version 2.0" );
pack();
setVisible( true );
}

public static void main( String[] args ) {
new skmIOv20 ();
}

}


Share this post


Link to post
Share on other sites
I successfully got my GUI to look nice last night. I finally got the .setMaximumSize method to work.
http://img.photobucket.com/albums/v91/4wheelingman/GUIFIXED.jpg
Now, its off to other challenges, IE: graphically getting a directory from the user like an installer program.

Thanks for the help glass.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this