I am creating a colour chooser and need to modify one of the colour chooser panels.
What I wanted was, I want to enter input values via the RGB fields to set the colour,The problem is the RGB values seem to be disabled is there a method within the api to turn on the RGB inputs to take a value?
Seems fine here.
import javax.swing.*;
class ColorChooserTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, new JColorChooser());
}
});
}
}
Is there anyway you can combine the RGB slider panel and the HSB panel?
Yes, apparently it is possible. Check this (very fragile, poorly laid out) example.
import java.awt.*;
import javax.swing.*;
import javax.swing.colorchooser.*;
import javax.swing.border.*;
class ColorChooserTest2 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JColorChooser cc = new JColorChooser();
AbstractColorChooserPanel[] panels = cc.getChooserPanels();
JPanel p = new JPanel();
panels[1].setBorder(
new TitledBorder(panels[1].getDisplayName()));
p.add(panels[1]);
panels[2].setBorder(
new TitledBorder(panels[2].getDisplayName()));
p.add(panels[2]);
JPanel gui = new JPanel(new BorderLayout(2,2));
gui.add(p, BorderLayout.CENTER);
gui.add(cc.getPreviewPanel(), BorderLayout.SOUTH);
JOptionPane.showMessageDialog(null, gui);
}
});
}
}
Related
I have written a Java XML Parser as an Applet. It is looking and functioning well enough in this form.
My Question, Is if I want to run this without a browser, how Would I properly wrap it to run as an executable?
GUI.java
--------------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GUI extends JPanel implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Parser xmlEditor;
private String startTimeValue;
private String endTimeValue;
public GUI(){
init();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
new GUI();
}
});
}
public void init() {
this.setXmlEditor(new Parser("C:\\Users\\Administrator\\workspace\\XMLParser\\src\\test.xml"));
add(new Label("Start Time"));
startTimeValue = xmlEditor.getStartTimeValue();
endTimeValue = xmlEditor.getEndTimeValue();
startTime = new TextField(startTimeValue);
add(new Label("End Time"));
endTime = new TextField(endTimeValue);
save = new Button("save");
save.addActionListener(this);
add(startTime);
add(endTime);
add(save);
}
public void actionPerformed(ActionEvent e)
{
System.out.println(endTime.getText());
xmlEditor.updateStartTimeValue(startTime.getText());
xmlEditor.updateEndTimeValue(endTime.getText());
System.out.println(e);
System.exit(0);
}
public Parser getXmlEditor() {
return xmlEditor;
}
public void setXmlEditor(Parser xmlEditor) {
this.xmlEditor = xmlEditor;
}
TextField startTime, endTime;
Button save;
}
While trying things with Swing and JFRame etc, I am not getting properly layout, or am opening multiple windows. Can anyone provide assistance? The second Panel Keeps replacing the First. Id like to really try to learn how to place multiple components inside an executable jar is the goal.
SwingPaintDemo.java
import java.awt.Label;
import java.awt.TextField;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
public class SwingPaintDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
System.out.println("Created GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
Parser myParser = new Parser("C:\\Users\\Administrator\\workspace\\XMLParser\\src\\test.xml");
JPanel top = new JPanel();
top.add(new Label("Start Time"));
TextField startTimeField = new TextField(myParser.getStartTimeValue());
top.add(startTimeField);
f.getContentPane().add(top);
JPanel bottom = new JPanel();
bottom.add(new Label("End Time"));
TextField endTimeField = new TextField(myParser.getEndTimeValue());
bottom.add(endTimeField);
f.getContentPane().add(bottom);
f.pack();
}
}
JFrame uses a BorderLayout by default, where as a JPanel uses a FlowLayout
Instead of rebuilding the UI in the JFrame, simply add an instance of GUI to it, since you've already defined the functionality in a JPanel, this makes it easily reusable.
public class SwingPaintDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
System.out.println("Created GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new GUI());
f.pack();
f.setVisible(true);
}
}
FYI: You should never reference src in any path element, src won't exist once the program is built and packaged. This is also doubly concerning for applets, as applets run in a tight security model, which prevents them from accessing the file system by default.
Instead, you should be using Class#getResource or Class#getResourceAsStream, depending on your needs.
this.setXmlEditor(new Parser(getClass().getResource("/test.xml")));
for example. You may need to change your Parser to accept either a URL and/or InputStream as well
I used the following line of code to change a JFrame icon in IntelliJ on a Linux platform:
myFrame.setIconImage(IMAGE_CIRCLE_ICON_RED.getImage());
The icon is displayed however it is a transparent icon and it doesn't display as transparent. How can I make my frame icon render as a transparent icon?
Here is a link to the icon.
IconRedCircleTransparent
It looks like a red circle surrounded by a grey square. My frame is ocean blue.
It looks very much like this bug: [#JDK-6429220] Default LAF decorated frame does not support transparent icons - Java Bug System
import java.net.*;
import javax.swing.*;
public class TransparentIconTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
try {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ImageIcon icon = new ImageIcon(
new URL("http://i.stack.imgur.com/AnvwU.png"));
f.setIconImage(icon.getImage());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Seems to work just fine here. Run the following source code. If you see transparency around the image, select the 'Yes' option in the option pane and you might see something like this as the output in the standard output stream.
This is the output here.
java.version: 1.7.0_25
java.runtime.version: 1.7.0_25-b17
java.vm.version: 23.25-b01
java.specification.vendor: Oracle Corporation
OK? true
This is the code used.
import java.awt.BorderLayout;
import java.net.URL;
import javax.swing.*;
public class TransparentPNG_2 {
private static String getPropertyString(String name) {
String eol = System.getProperty("line.separator");
String value = System.getProperty(name);
return String.format("%1s: \t%2s%3s", name, value, eol);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
JPanel ui = new JPanel(new BorderLayout());
URL url = new URL("http://i.stack.imgur.com/AnvwU.png");
ui.add(new JLabel(new ImageIcon(url)), BorderLayout.PAGE_START);
StringBuilder sb = new StringBuilder();
sb.append(getPropertyString("java.version"));
sb.append(getPropertyString("java.runtime.version"));
sb.append(getPropertyString("java.vm.version"));
sb.append(getPropertyString("java.specification.vendor"));
JTextArea output = new JTextArea(sb.toString().trim(), 4, 27);
ui.add(new JScrollPane(output));
int result = JOptionPane.showConfirmDialog(null, ui);
System.out.println(sb.toString());
System.out.println("OK? " + (result==JOptionPane.YES_OPTION));
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
SwingUtilities.invokeLater(r);
}
}
I know there are some topics relative to this question (mainly this unanswered one and this one which is not handling full screen app).
I basically tried every combination of first topic sample and available methods (requestFocus, requestFocusInWindow, ...) but JFileChooser is always displaying behind the fullscreen app. I tried to change filechooser's parent too (setting it to null, itself or the parent frame) with no more success.
Have anyone a working example of this not-that-much-particular use case? Or is there a workaround to let user select files in a fullscreen app?
Unfortunately I can't say how you realised the implementation of the fullscreen app. But I tried a few things and came up with this:
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Gui extends JFrame {
public Gui() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
// Set some charateristics of the frame
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setBackground(Color.black);
this.setUndecorated(true);
JButton a = new JButton("PRESS ME!");
a.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(getParent());
}
});
this.add(a);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Gui();
}
});
}
}
Pay attention to the fact, that I created a new JFileChooser with the parent of the current JFrame as parameter.
EDIT:
I now even tried to set
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(new Gui());
and without the
this.setUndecorated(true);
it worked for me (got a nice fullscreen view and the JFileChooser was in the front). I believe the problem with the window decoration is linked to my window manager (I'm using linux with gnome).
Hopefully this solution works for you, if not:
Could you explain a little bit more, how you create the fullscreen app?
I would suggest instead of using using a Popup, just embed the JFileChooser into your application. It doesn't really make sense to have popups in a windowless application (Personally, I don't like popups much anyways).
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FullScreenApp {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60)); // Ugh.
frame.setVisible(true);
final Box panel = Box.createVerticalBox();
JButton btn = new JButton();
btn.setText("Button");
panel.add(btn);
frame.add(panel);
final CustomFileChooser chooser = new CustomFileChooser(panel);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
chooser.show();
}
});
}
public static class CustomFileChooser extends JFileChooser{
/** Node this chooser should be added to.
* There's likely a better way of doing this,
* but it was convenient for a quick example */
Container parent;
public CustomFileChooser(Container parent){
super();
this.parent = parent;
//Make configurations for your file chooser
setApproveButtonText("Open");
}
#Override
public void approveSelection(){
super.approveSelection();
//Perform accept action here
System.out.println(getSelectedFile().getAbsolutePath());
parent.remove(CustomFileChooser.this);
parent.repaint();
}
#Override
public void cancelSelection(){
super.cancelSelection();
//Perform cancel action here
System.out.println("Canceled");
parent.remove(CustomFileChooser.this);
parent.repaint();
}
#Override
public void show(){
rescanCurrentDirectory();
parent.add(this);
revalidate();
repaint();
}
#Override
public Dimension getMaximumSize(){
//Not necessary - But I felt the chooser should have a maximum size
return new Dimension(500,300);
}
}
}
FullscreenLib
//import
import argha.util.Fullscreen;
//this for JFrame
//true for setting Undecorated on/off
Fullscreen screen = new Fullscreen(this, true);
screen.DoTheWorkFor();
You can use my library for creating fullscreen windows and the problem you are facing hope it solved after that i tested and its working.
Hope it may helped you
How to create a border of JPanel, which will be able to handle MouseEvents?
I tried to do something like that:
abstract public class MyBorder extends LineBorder implements MouseListener
But after implementing virtual methods I cannot assign mouseListener to my class. I guess, that I have to assign it into some JComponent.
So, how can I create some sort of border with mouseListener?
A MouseListener must be added to a Component, not a Border. So to use your class the code would need to be something like:
Border border = new MyBorder();
panel.setBorder( border );
panel.addMouseListener( border );
Here is an SSCCE that supports that borders get mouse events on the component to which they are applied.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
class BorderListener {
private void initGui() {
final JPanel gui = new JPanel();
gui.setBackground(Color.green);
gui.setPreferredSize(new Dimension(300,50));
gui.setBorder(new LineBorder(Color.blue, 10));
gui.addMouseListener( new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent me) {
System.out.println(me.getPoint());
}
});
JOptionPane.showMessageDialog(null, gui);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BorderListener bl = new BorderListener();
bl.initGui();
}
});
}
}
Typical Output
When clicking in the wide border assigned to this panel, you might see output along these lines.
java.awt.Point[x=8,y=3]
java.awt.Point[x=3,y=26]
java.awt.Point[x=1,y=43]
java.awt.Point[x=15,y=6]
java.awt.Point[x=101,y=5]
java.awt.Point[x=220,y=4]
java.awt.Point[x=287,y=5]
java.awt.Point[x=295,y=3]
Press any key to continue . . .
The border is 10px wide, so if (x||y < 10), it is within the line border.
Update
(Comment to camickr, which also applied to my answer)
Yes but then this mouseListener will be added for the whole JPanel. Not only for my Border. Am I wrong?
Just ignore the event if it happens in the non-border area of the panel.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
class BorderListener {
private void initGui() {
final JPanel gui = new JPanel();
gui.setBackground(Color.yellow);
gui.setPreferredSize(new Dimension(300,50));
gui.setBorder(new LineBorder(Color.orange, 15));
gui.addMouseListener( new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent me) {
int w = gui.getWidth();
int h = gui.getHeight();
int x = me.getPoint().x;
int y = me.getPoint().y;
Insets ins = gui.getInsets();
boolean inBorder =
( x<ins.left ||
x>w-ins.right ||
y<ins.top ||
y>h-ins.bottom);
if (inBorder) {
System.out.println(me.getPoint());
} else {
System.out.println("Ignore!");
}
}
});
JOptionPane.showMessageDialog(null, gui);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BorderListener bl = new BorderListener();
bl.initGui();
}
});
}
}
Output
java.awt.Point[x=168,y=7]
Ignore!
java.awt.Point[x=164,y=41]
java.awt.Point[x=297,y=39]
java.awt.Point[x=297,y=21]
Ignore!
Ignore!
java.awt.Point[x=2,y=21]
Press any key to continue . . .
I'm just getting into GUI programming, slowly learning.
However I'm having a problem right of the bat. I can't get the Fore/Background color to change in my window at all.
However when I add a label via JLabel and then use setFore/Back, they change colors just fine. Just not the whole window.
I thought .setForeground and .setBackground are supposed to change the color of the window?
import javax.swing.*;
import java.awt.*;
public class MyWindow {
public static void main(String args[])
{
Runnable init = new Runnable()
{
public void run()
{
JFrame myWindow = new JFrame("Hola!");
myWindow.setForeground(Color.YELLOW);
myWindow.setBackground(Color.YELLOW);
myWindow.setSize(400, 300);
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setLayout(null);
myWindow.setVisible(true);
}
};
SwingUtilities.invokeLater(init);
}
}
First of all, do not use a null layout. Let the layout manager do its job. Second of all, you need to set the background of the content pane of the JFrame instance, as such
myWindow.getContentPane().setBackground(Color.YELLOW);
See also:
Using Top-Level Containers
you cannot color a frame. However you can color the ContentPane inside.
import javax.swing.*;
import java.awt.*;
public class MyWindow {
public static void main(String args[])
{
Runnable init = new Runnable()
{
public void run()
{
JFrame myWindow = new JFrame("Hola!");
myWindow.getContentPane().setBackground(Color.YELLOW);
myWindow.setSize(400, 300);
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setLayout(null);
myWindow.setVisible(true);
}
};
SwingUtilities.invokeLater(init);
}
}
this should fix your problem...
tangina naman ang bobo naman neto, pokegooo
import javax.swing.;
import java.awt.;
public class MyWindow {
public static void main(String args[])
{
Runnable init = new Runnable()
{
public void run()
{
JFrame myWindow = new JFrame("Hola!");
myWindow.getContentPane().setBackground(Color.YELLOW);
myWindow.setSize(400, 300);
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setLayout(null);
myWindow.setVisible(true);
}
};
SwingUtilities.invokeLater(init);
}
}
// tanga amputa aral ka muna dudong
// pakangkang