Runtime Repositioning components in swing
Hi all,
i want to reposition the components at run time, (like in netbeans)
please suggest or any idea how can i do it.
i want to crate a tool like netbeans (drag and drop components facility) using swing.
thanks
daya
hi camickr,
thanks for your replay.
yes i know i need to post in sscce,
what i want is suggestions from you people so that i can take decision to move further(starting coding).
ok, let me start with null layout. here is the demo using null layout
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
* @author Dayananda B V
public class DragNDrop extends JFrame implements MouseMotionListener {
JButton btn = new JButton("drag and drop this component");
public DragNDrop() {
btn.addMouseMotionListener(this);
btn.setBounds(20, 20, 200, 20);
getContentPane().setLayout(null);
getContentPane().add(btn);
setVisible(true);
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
public static void main(String[] agrs) {
new DragNDrop();
public void mouseDragged(MouseEvent e) {
btn.setLocation(e.getPoint());
public void mouseMoved(MouseEvent e) {
}thanks
daya
Similar Messages
-
Using of JLoox components in Swing/AWT Paint
I am facing problem in using JLoox components in Swing/AWT Paint method.I am able to use the JLoox components in Constructor of Swing JFrame and facing problem while using the JLoox components in Paint method.If anybody used JLoox please suggest the way to use it.
Yes I have used JLOOX since last 2 years.
There is a LxGraph derived from LxAbstract and LxView derived from LxAbstractView. This LxAbstractView is in turn derived from JComponent. LxGraph is a model for LxView (same apporch like MVC). You create components (i.e. LxRectangle, LxCircle, LxLine, LxLink, etc) and add it to either graph or in view). Finally this view (i.e. LxView) is added to a java container (JPanel, JScrollBar, etc.) You cannot pass JLoox component into JFrame constructor.
-regards,
Pratap -
Greetings,
After the installation of the extension pack (local sql server and installed local) i get this warning:
Common requirement SSMA Runtime emulation components were not found on SQL Server, please check if Extension Pack is installed correctly
and
Common requirement SSMA Synchronization components were not found on SQL Server, please check if Extension Pack is installed correctly.
In the Program and Features Control panel the Feature "SSMA Extensionpack" (or similar) is installed and i see it in the startmenu.
what's wrong here, any hints?Hi A. Bloise,
Based on your description ,we need to verify that the type and version of SSMA Extension Pack. Assume that you install SSMA for Oracle Extension Pack, I recommend you check the following things.
1. Make sure that the setup file of SSMA for Oracle Extension Pack is not corrupted. You can download it from
Microsoft Download Center.
2. Make sure that your computer meets the following requirements for the SSMA for Oracle Extension Pack.
•SQL Server 2005 or higher.
•Microsoft Windows Installer 3.1 or a later version.
•Oracle Client 9.0 or a later version, and connectivity to the Oracle databases that you want to migrate.
•The SQL Server Browser service must be running during installation.
3. Make sure that the account used to install the extension pack, is a member of the sysadmin server role on the instance of SQL Server.
If this does not work, please contact with SSMA support team for further help:
[email protected]
Thanks,
Lydia Zhang -
Changing AWT components to Swing components
I'm having a little trouble changing some AWT components to Swing components. I received an error message when I tried to run my program. I looked up the component in the java docs. But, I did not see the option the error was talking about. The error and the area of code is listed below. Thank you for any help you can provide.
Error message:
Exception in thread "main" java.lang.Error: Do not use P5AWT.setLayout() use P5A
WT.getContentPane().setLayout() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:446)
at javax.swing.JFrame.setLayout(JFrame.java:512)
at P5AWT.<init>(P5AWT.java:56)
at P5AWT.main(P5AWT.java:133)
Press any key to continue . . .
Code:
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout(FlowLayout.CENTER));
ta = new JTextArea(20, 60);
p3.add(ta);
setLayout(new FlowLayout(FlowLayout.CENTER));
add(p3);you need to change the line...
setLayout(new FlowLayout(FlowLayout.CENTER)); to
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER)); -
Text input in text components in swing?
Hi,
is there any UML diagram, or any site on how text input is carried out in text components? Is it just a simple matter of JTextComponent adding itself as a KeyListener to itself, and then updating the document with each key press? or is there a little more too it?
thanks,
Jjustinlawler wrote:
or is there a little more too it?There's a lot more to it. I think good beginning reading is the summary at the top of the [Javadoc for JTextComponent|http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html] . It is a convoluted API that has many "kludges" that can't be completely removed because of the need for backwards compatibility.
I'd also recommend reading up on the classes EditorKit, InputMap, ActionMap, Keymap, and (Basic)TextUI. Good luck! -
How to arrange the components in swings
hi..
i hava a small problem with components, how to arrange components as i like .
Name : |____________________|
Number :|____________________| phone : |____________________|
Address : |____________________| <------ it is TextField ok.
Like this, how to do this one in swings. I want 10 Lables & 10 TextField .
I want Label Behind the Textfield as shown above
Message was edited by:
therathna
Message was edited by:
therathnabut gives error when resize the window frameIf you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the code retains its original formatting. -
Calling runtime.exec in menu(swing)
Hi all,
I want to pop up a command (NT) window from a menu in java swing.
1. However I am able to call from the menu, but was Not able to get the batch file (either .bat or .exe) to run in another window
2. and all the output from the batch file did not printout to screen.
any help?
JMenuItem menuItem = null;
startAction = new AbstractAction("Start") {
public void actionPerformed(ActionEvent e) {
displayResult("Starting Application...", e);
Process JobProcess = null;
try {
// JobProcess = Runtime.getRuntime().exec("cmd.exe /c test.bat");
//JobProcess = Runtime.getRuntime().exec("cmd /K start cmd /K test.bat");
//JobProcess = Runtime.getRuntime().exec("cmd.exe /c test.bat"); //works
JobProcess = Runtime.getRuntime().exec("test.bat"); // works
JobProcess.waitFor();
int i = JobProcess.exitValue();
catch (Exception ex) {
textArea.append(ex.getMessage());
in the test.bat :
echo "this is test" > test.txt
thanks
andrewI don't believe you can ues redirection (>) when executing a command. Your program must handle the output itself. Add code something like:
String line;
BufferedReader output = new BufferedReader( new InputStreamReader( JobProcess.getInputStream() ) );
while ( (line = output.readLine()) != null )
System.out.println( line );
output.close();Note: the variable JobProcess should be renamed to jobProcess. The Java convention is to use uppercase for class names. -
AWT components in swing not working
Hi,
We are developing a swing based GUI application.
Here we add a third party AWT based component into the JPanel. But if we add that AWT component, it does not show our JMenuItems properly.
Can somebody tell me what is the problem with the code below? Why it is not displaying my JMenuItems ?????
import java.awt.*;
import javax.swing.*;
public class ButtonTest extends JFrame
JMenuBar mbar;
JMenu file;
JMenuItem i1, i2, i3;
Container con;
public ButtonTest()
con = this.getContentPane();
con.setLayout(new BorderLayout());
mbar = new JMenuBar();
file = new JMenu("FILE");
i1 = new JMenuItem("Item1");
i2 = new JMenuItem("Item2");
i3 = new JMenuItem("Item3");
this.setJMenuBar(mbar);
file.add(i1);file.add(i2);file.add(i3);
mbar.add(file);
con.add(new Button("OK"));
public static void main(String args[])
JFrame mf = new ButtonTest();
mf.setSize(400 ,400);
mf.setVisible(true);
regards,
AnandReplace them with this.
import java.awt.*;
import javax.swing.*;
public class ButtonTest extends JFrame
JMenuBar mbar;
JMenu file;
JMenuItem i1, i2, i3;
Container con;
public ButtonTest()
con = this.getContentPane();
setSize(600,600);
mbar = new JMenuBar();
setLayout(new FlowLayout());
file = new JMenu("FILE");
i1 = new JMenuItem("Item1");
i2 = new JMenuItem("Item2");
i3 = new JMenuItem("Item3");
file.add(i1);file.add(i2);file.add(i3);
mbar.add(file);
con.add(new JButton("OK"));
setJMenuBar(mbar);
setVisible(true);
public static void main(String args[])
ButtonTest mf = new ButtonTest();
} -
Removing select components from swing JPanel
I have a JPanel that contains many different JLabels that move around to different parts of the JPanel. These components may end up occupying the same location in the JPanel.
1. How do I determine which objects is the one visible? Is it the one most recently added to the JPanel?
2. How do I selectively remove a certain component that may be in under one component and above 2 others...? This JLabel that I want to remove, does not have a specific name, instead it is indexed in an array.steeveesas wrote:
I have a JPanel that contains many different JLabels that move around to different parts of the JPanel. These components may end up occupying the same location in the JPanel.
1. How do I determine which objects is the one visible? Is it the one most recently added to the JPanel?I would guess that much depends on the layout manager (if any used) and how you are adding the components to the jpanel.
2. How do I selectively remove a certain component that may be in under one component and above 2 others...? This JLabel that I want to remove, does not have a specific name, instead it is indexed in an array.call remove(component) on the JPanel followed by revalidate() and repaint(). -
About the difference between the AWT components and the swing components
As we know that an AWT component has a peer, but dose a lightweight component(e.g: a swing component) has also a peer? And could somebody tell me that who and how creates the CLICKED_EVENT when I click a JButton(a swing component), and where the CLICKED_EVENT to go util it was processed by a method like btn_ok_clicked(Event e)? And if I click a Button(an AWT component), what's the difference?
Thanks a lot!The main difference is that AWT are native components, and Swing components are entirely in java written. That's besides the look and feel. There are a lot of other differences between AWT and Swing, and there are better described in the documentation.
-
GetTreeLock during constructor for swing components
I've noticed some dead locks in our application that are showing up in the call to getTreeLock that exists in the constructor for various JComponents. Specifically, the updateUI method winds up calling validateTree.
I thought it was OK to construct JComponents from any thread, this suggests it is not.
Has anyone else experienced this?It goes like this:
On win32, thread 0 is our windows message pump. If dispatching one of our messages results in a call from C into Java that constructs a JComponent, we call getTreeLock from thread 0.
Imagine that at the same time, the awt event disptatch thread is trying to show a frame. Since the awt event dispatch thread is part of our process, when the native implemention of the frame calls SendMessage to show the frame, the call to SendMessage blocks until thread 0 calls GetMessage.
Since thread 0 is now blocking waiting for synchronized(getTreeLock()) to return and the awt event dispatch thread has the tree lock - fetched while executing the show() method - we are in a dead lock.
This is not a problem with AWT components, only swing components.
The stack looks like this:
at java.awt.Component.getTreeLock(Component.java:811)
at java.awt.Container.invalidateTree(Container.java:1116)
at java.awt.Container.setFont(Container.java:1148)
at javax.swing.JComponent.setFont(JComponent.java:2310)
at javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:89)
at javax.swing.plaf.basic.BasicButtonUI.installDefaults(BasicButtonUI.java:124)
at com.sun.java.swing.plaf.windows.WindowsButtonUI.installDefaults(WindowsButtonUI.java:63)
at javax.swing.plaf.basic.BasicButtonUI.installUI(BasicButtonUI.java:60)
at javax.swing.JComponent.setUI(JComponent.java:449)
at javax.swing.AbstractButton.setUI(AbstractButton.java:1616)
at javax.swing.JButton.updateUI(JButton.java:119)
at javax.swing.AbstractButton.init(AbstractButton.java:1952)
at javax.swing.JButton.<init>(JButton.java:109)
at javax.swing.JButton.<init>(JButton.java:64)
It seems like we could simply have the invalidateTree method return early if there is no parent or there are no children or somesuch.
A work around we've done is to have the updateUI method delegate to the event dispatch thread. This is a drag as we need to subclass all the JComponents and make sure developers use the safe subclass. -
I am having a element in XML and I have to represent this element whether it's a simple textbox or combobox. After that I have to store values in the combobox through XML.
I dont know how to do this.
Plz help me.Hi,
Is your problem one of howto conditionally create components in swing. Does your xml have a schema attached to it and do you want to display a textfield if it is a single value and a combo box if it is a list of values. In that case, you would use the schema to determine the type of the element at runtime and instantiate the appropriate component. Some of the creational patterns in the GoF patterns should help you in doing this.
Cheers,
Prakash -
JRE1.6 in Windows Vista for Swing based application
Hi,
I am running a Swing based and applet based application in Windows VISTA using JRE 1.6_U13. The text font is appearing bold in JRE 1.6_U13 whereas it is appearing normal while using JRE 1.5, though the style of the Font
is "SansSerif", PLAIN and size 12 for both JRE 1.6 and JRE 1.5.
Reason for the Issue:
Swings follows the concept of taking the display values from the Windows UIManager(). The UIManager consists of keys and values. The values for the keys change from OS to OS. As a result, if the UIManager key values are not overridden with the customized values, the display will change from OS to OS. Hence the issue has occurred.
Analysis:
Case-1:
1. CALM is setting the font style to Font("SansSerif", Font.PLAIN, 12).
2. This font is being set to the Label and Text components in swings.
3. The font class consists of Font.PLAIN argument whose value is 0 as per the java API.
4. This value cant be changed.
5. Hence we couldnt solve.
Case-2:
1. We tried to get the key value for the key Label.font and tried to override its value.
2. But then the issue is with the Font.Plain which is one of the arguments in Font object.
3. Hence couldnt achieve it.
Case-3:
1. We thought of creating a new font where in we can have control over the weight and width of the text. Then set this font to respective key of the UIManager.
2. Here the weight indicates the bold thickness. As a result of which we thought of changing the weight.
3. But then the options that we have in the API are less and the least weight value is TextAttribute.WEIGHT_EXTRA_LIGHT which is equal to 0.5.
4. This value is not sufficient as the boldness of the text is comparatively more than the text that is appearing in jre-1.5.
5. Then we tried to give the value manually, instead of using the API. But then, it was mentioned in the API that any values mentioned other than the ones specified in the API will be rounded to the nearest integer.
6. By doing this we have customized the boldness of the Label, but could not achieve the required boldness of the Label.
Please need a urgent solution.Hi,
The reason of the problem is Antialiasing effect of the graphics. In Control Panel -> Performance Information and tools -> Adjust visual effect, if the 'Smooth edges of screen font' is unchecked then the boldiness of the font or the Antialiasing effect is not there.
But this effect is reflecting not only on my swing based and applet based application but also on the whole system.
I tried to call the renderingHint method and change default value of the key KEY_TEXT_ANTIALIASING to VALUE_TEXT_ANTIALIASING_OFF, to make the antialiasing effect off for the application.
I implemented the below code:
Graphics g= new Graphics();
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
But I am not able to create an object of Graphics. -
hi there,
I am a student at the University of Portsmouth in the UK in my third and last year. I am doing my dissertation as a game using applets for a client at the university of which there consits four prototypes. One of the main specification points is that it should work on an apple mac, therefore I am using Swing applets. however with my first prototype it works fine on windows in windows Explorer, firefox and Netscape but i cant get it to work on Mac os 9 or X even with the latest version of java installed on the machine. Can anyone tell me why this is so, I would be very gratefull.
As I cant post the whole applet here as a file download I will just put some code snippets of the main program classes.
Applet ------------------------------
* Class JugglingBalls - A sub class of JApplet that combines the GameInterface and GamePanel into a single application
* @author (Liam Morren)
* @version (v1.0)
import java.awt.*;
import javax.swing.*;
public class JugglingBalls extends JApplet
// instance variables -----------------------------------------------------------------------------------------------------------------------------------
private GamePanel gamePanel; // The game panel including game and render loop
private GameInterface gameInterface; // The swing interface to modify the game scene components
// JApplet methods -------------------------------------------------------------------------------------------------------------------------------------
* Called by the browser or applet viewer to inform this JApplet that it
* has been loaded into the system. It is always called before the first
* time that the start method is called.
public void init()
gamePanel = new GamePanel();
gameInterface = new GameInterface(gamePanel.getGameScene());
Container content = getContentPane(); // The container for components in the applet
setLayout(new BoxLayout(content, BoxLayout.X_AXIS));
content.add(gamePanel);
content.add(gameInterface);
* Returns information about this applet.
* An applet should override this method to return a String containing
* information about the author, version, and copyright of the JApplet.
* @return a String representation of information about this JApplet
public String getAppletInfo()
// provide information about the applet
return "Title: JugglingBalls \nAuthor: Liam Morren \nA simple application showing balls juggling in the air.";
Interface -------------------------------------
* Class GameInterface - Lets you manipulate the game components using swing components
* @author (Liam Morren)
* @version (v1.0)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class GameInterface extends JPanel
// instance variables -------------------------------------------------------------------------------------------------------------------------------------
private GameScene gameScene; // Reference to the game scene
private JTabbedPane gameTabs; // Tabs for Game, Balls and Background
// Scene panel
JButton pause, start, reset; // Buttons
// Balls panel
JRadioButton redBall, greenBall, blueBall, archBall, figureOfEightBall; // Radio buttons
JCheckBox canSplit; // Check box
JComboBox setAllPaths, setAllTypes, newBall, selectBall; // Drop down boxes
JButton deleteBall; // Button
Ball currentBall; // Current ball being changed
// Background panel
JRadioButton black, white, red, green, blue, hide, show, stop, move, arch, figureOfEight; // Radio buttons
// Constructors -------------------------------------------------------------------------------------------------------------------------------------------
* Constructor for objects of class GameInterface
public GameInterface(GameScene gameSceneIn)
gameScene = gameSceneIn; // Reference to the game scene
gameTabs = new JTabbedPane(); // JTabbedPane to hold different toolbars
gameTabs.setPreferredSize(new Dimension(200, 494)); // Set the preffered size for the tabbed pane
currentBall = gameScene.getBall(0); // First ball
// Add the panels to the tabbed pane
gameTabs.addTab("Scene", makeScenePanel());
gameTabs.addTab("Balls", makeBallsPanel());
gameTabs.addTab("Background", makeBackgroundPanel());
add(gameTabs); // Add the tab to the game interface panel
setPreferredSize(new Dimension(200, 200));
// Other methods ------------------------------------------------------------------------------------------------------------------------------------------
* makeScenePanel
* @return JPanel - The scene panel with buttons added
private JPanel makeScenePanel()
// Make scene panel
JPanel scenePanel = new JPanel();
scenePanel.setLayout(new BoxLayout(scenePanel, BoxLayout.Y_AXIS)); // Change layout
pause = new JButton("Pause"); // Make buttons
start = new JButton("Start");
reset = new JButton("Reset");
// Add button actions
pause.addActionListener(new ActionListener() // Add button listener to pause button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
gameScene.setPaused(true); // Pause game
start.addActionListener(new ActionListener() // Add button listener to start button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
gameScene.setPaused(false); // Unpause game
reset.addActionListener(new ActionListener() // Add button listener to reset button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
gameScene.reset(); // Reset game
updateSelectBallComboBox();
changeSelectedBall(0);
scenePanel.add(pause); // Add buttons to scene panel
scenePanel.add(start);
scenePanel.add(reset);
return scenePanel;
* makeBackgroundPanel
* @return JPanel - The background panel with buttons added
private JPanel makeBackgroundPanel()
// Make scene panel
JPanel backgroundPanel = new JPanel();
backgroundPanel.setLayout(new GridLayout(5, 1, 2, 2)); // Change layout
// Make buttons
// background colour buttons
JPanel backgroundcolour = new JPanel(); // Change backgroundcolour panel
backgroundcolour.setBorder(BorderFactory.createLineBorder(Color.black));
black = new JRadioButton("Black"); // Radio buttons to change colour
white = new JRadioButton("white");
black.setSelected(true); // Set black as already selected
ButtonGroup backColourGroup = new ButtonGroup(); // Group buttons so only one can be pressed at a time
backColourGroup.add(black); // Add buttons to group
backColourGroup.add(white);
backgroundcolour.setLayout(new GridLayout(2, 2, 0, 0)); // Change layout
backgroundcolour.add(new JLabel("Background colour")); // Add label
backgroundcolour.add(black); // Add buttons to colours panel
backgroundcolour.add(white);
// Add radio button actions
black.addActionListener(new ActionListener() // Add button listener to black button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
gameScene.setBackgroundColour(false); // Background colour = black
white.addActionListener(new ActionListener() // Add button listener to white button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
gameScene.setBackgroundColour(true); // Background colour = white
// colour buttons
JPanel colours = new JPanel(); // Change colours panel
colours.setBorder(BorderFactory.createLineBorder(Color.black));
red = new JRadioButton("Red"); // Radio buttons to change colour
green = new JRadioButton("Green");
green.setSelected(true); // Set green as already selected
blue = new JRadioButton("Blue");
ButtonGroup colourGroup = new ButtonGroup(); // Group buttons so only one can be pressed at a time
colourGroup.add(red); // Add buttons to group
colourGroup.add(green);
colourGroup.add(blue);
colours.setLayout(new GridLayout(2, 2, 0, 0)); // Change layout
colours.add(new JLabel("Change colours")); // Add label
colours.add(red); // Add buttons to colours panel
colours.add(green);
colours.add(blue);
// Add radio button actions
red.addActionListener(new ActionListener() // Add button listener to red button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
if (gameScene.getBackgroundEffect().getColour() != 0)
gameScene.getBackgroundEffect().setColour(0); // Background effect colour = red
green.addActionListener(new ActionListener() // Add button listener to green button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
if (gameScene.getBackgroundEffect().getColour() != 1)
gameScene.getBackgroundEffect().setColour(1); // Background effect colour = green
blue.addActionListener(new ActionListener() // Add button listener to blue button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
if (gameScene.getBackgroundEffect().getColour() != 2)
gameScene.getBackgroundEffect().setColour(2); // Background effect colour = blue
// hide buttons
JPanel hideShow = new JPanel(); // Change hideShow panel
hideShow.setBorder(BorderFactory.createLineBorder(Color.black));
hide = new JRadioButton("Hide"); // Radio buttons to hide show
show = new JRadioButton("Show");
show.setSelected(true); // Set show as already selected
ButtonGroup hideShowGroup = new ButtonGroup(); // Group buttons so only one can be pressed at a time
hideShowGroup.add(hide); // Add buttons to group
hideShowGroup.add(show);
hideShow.setLayout(new GridLayout(2, 2, 0, 0)); // Change layout
hideShow.add(new JLabel("Hide / Show")); // Add label
hideShow.add(hide); // Add buttons to hideShow panel
hideShow.add(show);
// Add radio button actions
hide.addActionListener(new ActionListener() // Add button listener to hide button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
gameScene.getBackgroundEffect().hide(); // Hide
show.addActionListener(new ActionListener() // Add button listener to show button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
gameScene.getBackgroundEffect().unHide(); // Unhide
// stopMove buttons
JPanel stopMove = new JPanel(); // Change StopMove panel
stopMove.setBorder(BorderFactory.createLineBorder(Color.black));
stop = new JRadioButton("Stop"); // Radio buttons to stop move
move = new JRadioButton("Move");
stop.setSelected(true); // Set stop as already selected
ButtonGroup stopMoveGroup = new ButtonGroup(); // Group buttons so only one can be pressed at a time
stopMoveGroup.add(stop); // Add buttons to group
stopMoveGroup.add(move);
stopMove.setLayout(new GridLayout(2, 2, 0, 0)); // Change layout
stopMove.add(new JLabel("Stop / Move")); // Add label
stopMove.add(stop); // Add buttons to stopMove panel
stopMove.add(move);
// Add radio button actions
stop.addActionListener(new ActionListener() // Add button listener to stop button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
gameScene.getBackgroundEffect().setMove(false); // stop
move.addActionListener(new ActionListener() // Add button listener to move button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
gameScene.getBackgroundEffect().setMove(true); // move
// changePath buttons
JPanel changePath = new JPanel(); // Change changePath panel
changePath.setBorder(BorderFactory.createLineBorder(Color.black));
arch = new JRadioButton("Arch"); // Radio buttons to change path
figureOfEight = new JRadioButton("Figure of eight");
arch.setSelected(true); // Set stop as already selected
ButtonGroup changePathGroup = new ButtonGroup(); // Group buttons so only one can be pressed at a time
changePathGroup.add(arch); // Add buttons to group
changePathGroup.add(figureOfEight);
changePath.setLayout(new GridLayout(2, 2, 0, 0)); // Change layout
changePath.add(new JLabel("Change Path")); // Add label
changePath.add(arch); // Add buttons to changePath panel
changePath.add(figureOfEight);
// Add radio button actions
arch.addActionListener(new ActionListener() // Add button listener to arch button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
gameScene.getBackgroundEffect().setPath(new Arch()); // Arch
figureOfEight.addActionListener(new ActionListener() // Add button listener to figureOfEight button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
gameScene.getBackgroundEffect().setPath(new FigureOfEight()); // Figure of eight
// Add components to background panel
backgroundPanel.add(backgroundcolour);
backgroundPanel.add(colours);
backgroundPanel.add(hideShow);
backgroundPanel.add(stopMove);
backgroundPanel.add(changePath);
return backgroundPanel;
* makeBallsPanel
* @return JPanel - The ball panel with buttons added
private JPanel makeBallsPanel()
// Make balls panel
JPanel ballsPanel = new JPanel();
ballsPanel.setLayout(new BoxLayout(ballsPanel, BoxLayout.Y_AXIS)); // Change layout
// Make buttons
// set all drop down boxes
JPanel setAll = new JPanel(); // Set all panel
setAll.setBorder(BorderFactory.createLineBorder(Color.black));
String[] setAllPathsChoices = {"Arch", "Figure of eight"}; // Selections for set all paths
setAllPaths = new JComboBox(setAllPathsChoices); // Add selections to combo box
String[] setAllTypesChoices = {"Primary ball", "Pastel ball", "3D ball", "Image ball"}; // Selections for set all types
setAllTypes = new JComboBox(setAllTypesChoices); // Add selections to combo box
setAll.add(new JLabel("Set all paths")); // Add combo boxes to set all panel
setAll.add(setAllPaths);
setAll.add(new JLabel("Set all types"));
setAll.add(setAllTypes);
// Add combo box actions
setAllPaths.addActionListener(new ActionListener() // Add selection listener to set all paths combo box
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
if (setAllPaths.getSelectedItem() == "Arch")
gameScene.setAllPathsArch();
else
gameScene.setAllPathsFigureOfEight();
setAllTypes.addActionListener(new ActionListener() // Add selection listener to set all types combo box
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
if (setAllTypes.getSelectedItem() == "Primary ball")
gameScene.setAllTypesPrimaryBall();
else if (setAllTypes.getSelectedItem() == "Pastel ball")
gameScene.setAllTypesPastelBall();
else if (setAllTypes.getSelectedItem() == "Image ball")
gameScene.setAllTypesImageBall();
else
gameScene.setAllTypesBall3D();
// new ball drop down box
JPanel newBallPanel = new JPanel(); // new ball panel
newBallPanel.setBorder(BorderFactory.createLineBorder(Color.black));
String[] newType = {"Primary ball", "Pastel ball", "3D ball", "Image ball"}; // Selections for new ball
newBall = new JComboBox(newType); // Add selections to combo box
newBallPanel.add(new JLabel("New ball")); // Add combo boxes to new ball panel
newBallPanel.add(newBall);
// Add combo box actions
newBall.addActionListener(new ActionListener() // Add selection listener to newBall combo box
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
String ballType = (String) newBall.getSelectedItem();
if (ballType == "Primary ball")
gameScene.addBall(new PrimaryBall(gameScene));
else if (ballType == "Pastel ball")
gameScene.addBall(new PastelBall(gameScene));
else if (ballType == "Image ball")
gameScene.addBall(new ImageBall(gameScene));
else
gameScene.addBall(new Ball3D(gameScene));
updateSelectBallComboBox();
changeSelectedBall(gameScene.getBalls().size() - 1); // Update interface to show new ball
// selected ball panel
JPanel selectedBallPanel = new JPanel(); // selected ball panel
selectedBallPanel.setBorder(BorderFactory.createLineBorder(Color.black));
// Select ball
selectBall = new JComboBox(gameScene.getBallList()); // Add ball list to the select drop down box
deleteBall = new JButton("DeleteBall"); // Delete button
// Ball colour
redBall = new JRadioButton("Red"); // Red choice
redBall.setSelected(true); // Start as selected
greenBall = new JRadioButton("Green"); // Green choice
blueBall = new JRadioButton("Blue"); // Blue choice
ButtonGroup ballColour = new ButtonGroup(); // So only one colour can be selected at a tim
ballColour.add(redBall);
ballColour.add(greenBall);
ballColour.add(blueBall);
// Path
archBall = new JRadioButton("Arch"); // Arch path choice
archBall.setSelected(true); // Start as selected
figureOfEightBall = new JRadioButton("Figure of eight"); // Figure of eight path choice
ButtonGroup ballPath = new ButtonGroup(); // So only one path can be selected at a tim
ballPath.add(archBall);
ballPath.add(figureOfEightBall);
// Can split
canSplit = new JCheckBox("Can split", false); // Initialy not selected
selectedBallPanel.add(new JLabel("Select ball")); // Add components to selected ball panel
selectedBallPanel.add(selectBall);
selectedBallPanel.add(deleteBall);
selectedBallPanel.add(new JLabel("Ball colour"));
selectedBallPanel.add(redBall);
selectedBallPanel.add(greenBall);
selectedBallPanel.add(blueBall);
selectedBallPanel.add(new JLabel("Path"));
selectedBallPanel.add(archBall);
selectedBallPanel.add(figureOfEightBall);
selectedBallPanel.add(canSplit);
// Add select ball drop down box action
selectBall.addActionListener(new ActionListener() // Add selection listener to selectBall combo box
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
changeSelectedBall(selectBall.getSelectedIndex()); // Change the selected ball using the index from the select ball drop down box
// Add delete button action
deleteBall.addActionListener(new ActionListener() // Add selection listener to deleteBall button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
int tempIndex = selectBall.getSelectedIndex(); // Temp index
if (gameScene.getBalls().size() > 1) // If there is more than 1 ball left
gameScene.removeBall(tempIndex); // Remove ball
updateSelectBallComboBox(); // Update with new ball list
changeSelectedBall(tempIndex - 1); // Update interface with new current selected ball
// Add radio button colour actions
redBall.addActionListener(new ActionListener() // Add button listener to redBall button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
if (currentBall.getColour() != 0)
currentBall.setColour(0); // currentBall colour = red
greenBall.addActionListener(new ActionListener() // Add button listener to greenBall button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
if (currentBall.getColour() != 1)
currentBall.setColour(1); // currentBall colour = green
blueBall.addActionListener(new ActionListener() // Add button listener to blueBall button
public void actionPerformed(ActionEvent ev) // Background colour = blue
if (currentBall.getColour() != 2)
currentBall.setColour(2); // currentBall colour = blue
// Add path radio button actions
archBall.addActionListener(new ActionListener() // Add button listener to arch button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
currentBall.setPath(new Arch()); // Arch
figureOfEightBall.addActionListener(new ActionListener() // Add button listener to figureOfEight button
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
currentBall.setPath(new FigureOfEight()); // Figure of eight
// Add can split check button actions
canSplit.addActionListener(new ActionListener() // Add button listener to can split
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
currentBall.setCanGiveBirth(canSplit.isSelected()); // Ball can split if can split check button is selected
// Add components to ball panel
ballsPanel.add(setAll);
ballsPanel.add(newBallPanel);
ballsPanel.add(selectedBallPanel);
return ballsPanel;
* changeSelectedBall
* @param ballIndexIn - The numbered index of the ball in the game scene list of balls. If less than 0 then equals 0. If more than ball list size then equals last ball
public void changeSelectedBall(int ballIndexIn)
if (ballIndexIn < 0) // Make sure index is within bounds
ballIndexIn = 0;
else if (ballIndexIn >= gameScene.getBalls().size())
ballIndexIn = gameScene.getBalls().size() - 1; // Else put within bounds at either end
currentBall.highlight(false); // Unhighlight current ball
currentBall = gameScene.getBall(ballIndexIn); // Get new current ball
currentBall.highlight(true); // Highlight new current ball
// Update controls to new ball
selectBall.setSelectedIndex(ballIndexIn);
switch (currentBall.getColour()) // Update colour controls
case 0: redBall.setSelected(true); break;
case 1: greenBall.setSelected(true); break;
default: blueBall.setSelected(true); break;
if (currentBall.getPath() instanceof Arch) // If path is arch
archBall.setSelected(true); // Update controls as arch
else
figureOfEightBall.setSelected(true); // Else figure of eight
canSplit.setSelected(currentBall.canGiveBirth()); // Set if the ball can split or not
* updateSelectBallComboBox
* This method updates the combo box selection model with a new one made from the updated balls arraylist from the game scene
public void updateSelectBallComboBox()
DefaultComboBoxModel model = new DefaultComboBoxModel(gameScene.getBallList());
selectBall.setModel(model);
}Kind regards
Liam morrenhttp://docs.info.apple.com/article.html?artnum=301073
This link states Mac OS X 10.4 now supports Java 5.0. It does mention nothing about Mac OS 9 or previous version of OS X. The problem you may be facing is that your Mac is running only 1.4.2 or less. The class files compiled for Java 5.0 can't run in non 5.0 versions of the Java Virtual Machine. Java 5.0 was a major release for the Java language and thus the change in numbering (though it took a little bit for this to change).
Anyway you should confirm what version of Java you have installed on your Mac.
At a command prompt:
java -version
The above URL states that the previous 1.4.2 Java version is not uninstalled, and this could still be the preferred version to use for Java applications even after installing Java 5.0.
The URL above states there is a utility to set your apps to use Java 5.0 as the perferred Java version. Who knows maybe this is all you need to do. I hope this is the case, then it probably is an easy fix, otherwise you may need to rewrite your app in Java 1.4.2 to get it to run on your version of Mac. -
(Youtube-) Video in a Swing GUI
Hey everyone,
I'm currently trying to play a video in my Swing GUI with JMF but I really can't get it to work.
With the help of google I got this far:
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JFrame;
public class MediaPanel extends JFrame {
public MediaPanel() {
setLayout(new BorderLayout()); // use a BorderLayout
// Use lightweight components for Swing compatibility
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
URL mediaURL = null;
try {
mediaURL = new URL("http://www.youtube.com/watch?v=Q7_Z_mQUBa8");
} catch (MalformedURLException ex) {
System.err.println(ex);
try {
// create a player to play the media specified in the URL
Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);
// get the components for the video and the playback controls
Component video = mediaPlayer.getVisualComponent();
Component controls = mediaPlayer.getControlPanelComponent();
if (video != null) {
add(video, BorderLayout.CENTER); // add video component
if (controls != null) {
add(controls, BorderLayout.SOUTH); // add controls
mediaPlayer.start(); // start playing the media clip
} // end try
catch (NoPlayerException noPlayerException) {
System.err.println("No media player found");
} // end catch
catch (CannotRealizeException cannotRealizeException) {
System.err.println("Could not realize media player");
} // end catch
catch (IOException iOException) {
System.err.println("Error reading from the source");
} // end catch
} // end MediaPanel constructor
}But all I get is errors:
Warning: The URL may not exist. Please check URL
No media player found
Can you please please help me get this working? I would really appreciate a little walkthrough
Best regards,
Patrick
Edited by: 954807 on Aug 24, 2012 6:52 AMJust use \ tags. People don't like to go to external sites.
I really advise you to consider using JavaFX 2 here. Swing is old and not really supported anymore, JMF is also old and absolutely not supported anymore.
Maybe you are looking for
-
I have recently been experincing this error and i am the admin on the computer and it is the only apple ID i have ever had or used yet it seems to think there is another one. My wife has an account on the computer and her itunes is signed under her n
-
How can I change the place of a photo in iPhoto?
How can I change the place label of a photo in iPhoto?
-
Open Dataset No Authority runtime error
Hi friends I am getting error like this No Authorization to open the file 'HCM_PAYROLL_MDATA_IT2010.LSMW.READ'. Can anyone help me . whay i am getting . Error in the abap program /1cadmc/sap_lsmw_read_00000036' had to be terminated coz it has come cr
-
Adobe Acrobat 8 Standard setup problem
I am not sure this is the right forum for my problem: if it isn't, can anybody please point me to the right forum (or channel) to troubleshoot and solve my problem? I attempted to install Adobe Acrobat 8 Standard (Italian) on several Windows XP clien
-
My macbook pro has a sound problme while booting .....
wen my sytem on sound s nt cuming ....i tryed to incress voliame its nt working .....i chke d system prefarance its shows no audio out presend ...after restaring my system sound came back ......n between its hapening ...how can i sove dis problam