How to change Look and Feel for the ToolBar, the top menu (where the buttons to close, minimize, maximize)
Is it possible to like something change? (Add, delete button, assign a background)
What is import is required to create it?
you can set the background image of a JButton you could have a look at this: Swing Tutorial: JButton which shows the use of the new JButton(String text,ImageIcon imgIco) to create a JButton with an ImageIcon and String.
To set the colour of the background and text you could use setBackground(Color c) and setForeground(Color c)
or
Alternatively just customize Look and Feel color scheme by setting an appropriate supported Look and Feel and then change the color scheme/size etc of its components thier are hundreds of things you can change for every component see this for them all.
To customize the Exit, Minimize and Maximize Toolbar buttons this can also be using the Look and Feel ( Custom design for Close/Minimize buttons on JFrame ):
import java.awt.BorderLayout;
import javax.swing.*;
public class FrameCloseButtonsByLookAndFeel {
FrameCloseButtonsByLookAndFeel() {
String[] names = {
UIManager.getSystemLookAndFeelClassName(),
UIManager.getCrossPlatformLookAndFeelClassName()
};
for (String name : names) {
try {
UIManager.setLookAndFeel(name);
} catch (Exception e) {
e.printStackTrace();
}
// very important to get the window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame(UIManager.getLookAndFeel().getName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel gui = new JPanel(new BorderLayout());
f.setContentPane(gui);
JTree tree = new JTree();
tree.setVisibleRowCount(4);
gui.add(tree, BorderLayout.LINE_START);
gui.add(new JScrollPane(new JTextArea(3,15)));
JToolBar toolbar = new JToolBar();
gui.add(toolbar, BorderLayout.PAGE_START);
for (int ii=1; ii<5; ii++) {
toolbar.add(new JButton("Button " + ii));
if (ii%2==0) {
toolbar.addSeparator();
}
}
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new FrameCloseButtonsByLookAndFeel();
}
});
}
}
Well the easiest way to change frame titlebar look is to set LookAndFeel before you create your frame.
Probably this is what you are looking for - http://www.jtattoo.net/ScreenShots.html
Related
I'm fairly new to Java Swing and I'm running into a few problems.
As a side question, when making a fairly large Java Swing Application, what is the best way to split up code?
In my case I want to have an application that has a layout just as Microsoft Word where there is a JToolBar filled with buttons and a main JPanel where changes are made based on the buttons pressed in the Tool Bar.
So as shown in the code below, I have a JFrame and I call the MainPanel class in order to create a panel and add a ToolBar with a button. When the button is pressed it adds a button to the panel. The problem comes when you click the button nothing shows up until you resize the window(in my case I simply manually drag the screen to make it larger).
public class Main {
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("MathMaker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create the menu bar. Make it have a green background.
//MainToolBar mainTB = new MainToolBar();
MainPanel mainPanel = new MainPanel();
frame.getContentPane().add(mainPanel.getGUI(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
public class MainPanel implements ActionListener{
JPanel mPanel;
JToolBar mToolBar;
JButton addQuestion;
public MainPanel() {
mPanel = new JPanel(new BorderLayout());
mToolBar = new JToolBar();
addQuestion = new JButton("test");
addQuestion.addActionListener(this);
mPanel.setLayout(new BorderLayout());
mPanel.setBackground(new Color(248, 213, 131));
mPanel.setPreferredSize(new Dimension(200, 180));
mToolBar.add(addQuestion);
mPanel.add(mToolBar, BorderLayout.PAGE_START);
}
public JComponent getGUI()
{
return mPanel;
}
#Override
public void actionPerformed(ActionEvent e) {
JButton temp = new JButton("temp");
mPanel.add(temp);
}
}
You should revalidate your panel
#Override
public void actionPerformed(ActionEvent e) {
JButton temp = new JButton("temp");
mPanel.add(temp);
mPanel.revalidate();
mPanel.repaint();
}
I believe you need to call revalidate() and repaint() to see the changes, here is a similar question here
The problem here is the panel is not repainted automatically.. When you resize the panel Java repaints the panel on the screen. Try repainting the panel everytime any button to modify the panel is clicked..
Just call the validate() and repaint() method with the panel
I have a JFrame containing various components and I would like to add a translucent grey overlay over the top while the application is initializing various things. Ideally it would prevent interaction with the underlying components and would be able to display some "Loading..." text or a spinning wheel or something similar.
Is there a simple way to do this using Java and Swing?
Take a look at JRootPane and JLayeredPane http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html#layeredpane
What you're asking about specifically sounds like a Glass Pane.
http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html#glasspane
The Glass Pane prevents interaction with underlying components and can be used to display something on top of your JFrame.
As #David said, you can use the glass pane for displaying some loading text or image above the rest of the application.
As for the grey overlay: why don't you use the built in ability to disable components as long as your application is loading? Disabled components will get grayed out automatically and cannot be interacted with by the user.
Something like this:
public class LoadingFrame extends JFrame{
JButton button;
public LoadingFrame() {
button = new JButton("ENTER");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Application entered");
}
});
setLayout(new BorderLayout());
add(button, BorderLayout.CENTER);
}
public void startLoading(){
final Component glassPane = getGlassPane();
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
final JLabel label = new JLabel();
panel.add(label, BorderLayout.SOUTH);
setGlassPane(panel);
panel.setVisible(true);
panel.setOpaque(false);
button.setEnabled(false);
Thread thread = new Thread(){
#Override
public void run() {
for (int i = 5; i > 0; i--) {
label.setText("Loading ... " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// loading finished
setGlassPane(glassPane);
button.setEnabled(true);
}
};
thread.start();
}
public static void main(String[] args) {
LoadingFrame frame = new LoadingFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.startLoading();
frame.setVisible(true);
}
}
I Have recently started Java programming and am trying to create a login screen. However, I cannot figure out how to create a new line to put my buttons and text on. Also, I would like to move these to the bottom right corner of the JPanel. I apologise for my poor wording and hopefully you can see what I mean from my code. Please help and thank you in advance.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo implements ItemListener {
JPanel cards;
final static String BUTTONPANEL = "Card with JButtons";
public void addComponentToPane(Container pane) {
JPanel card1 = new JPanel();
card1.add(new JLabel("Username:"));
card1.add(new JTextField("Username", 10));
card1.add(new JButton("Login")); //end line here
//New line
card1.add(new JLabel("Password:"));
card1.add(new JTextField("Password", 10));
card1.add(new JButton("Register")); //end line here
//New line
card1.add(new JCheckBox());
card1.add(new JLabel("Remember credentials"));
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
pane.add(cards, BorderLayout.BOTTOM_RIGHT);// Add cards to bottom right hand corner.
}
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CardLayoutDemo demo = new CardLayoutDemo();
demo.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
As shown here, you can position your top-level container in the lower, right portion of the screen. Substitute your own components for the placeholder mentioned in getPreferredSize(). Also, consider a JToolBar, which can float on many implementations.
Addendum: I want to move the buttons to the bottom right corner of the JPanel, not move the JPanel to the bottom of the screen.
Specifying FlowLayout.RIGHT for your card1 produces the result shown. Substitute your panel having CardLayout in the content pane's BorderLayout.CENTER.
public void addComponentToPane(Container pane) {
JPanel card1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
…
pane.add(new JPanel() {
#Override // placeholder for actual content
public Dimension getPreferredSize() {
return new Dimension(800, 200);
}
}, BorderLayout.CENTER);
pane.add(cards, BorderLayout.PAGE_END);
}
Addendum: Here's an update to your example.
public void addComponentToPane(Container pane) {
…
pane.add(cards, BorderLayout.PAGE_END);
}
…
private static void createAndShowGUI() {
JFrame frame = new JFrame("Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Move frame to lower right corner of screen
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int) rect.getMaxX() - frame.getWidth();
int y = (int) rect.getMaxY() - frame.getHeight();
frame.setLocation(x, y);
CardLayoutDemo demo = new CardLayoutDemo();
demo.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
Suggestion:
There is no such constraint BorderLayout.BOTTOM_RIGHT with BorderLayout.
BorderLayout.SOUTH : place the component at the bottom of container
BorderLayout.EAST : Place the component at the right side of container
BorderLayout.NORTH : place the component at the top of container
BorderLayout.WEST : place the component at the right side of container
BorderLayout.CENTER: place the component at the middle of container
If you want to orient your component as you wish, where the
component will appear in order, positioned in relative to each
other, responsive with Main Container's re-size, the you need to
learn about Layout Manager first.
Learn about Event listeners. Instead of implementing an ItemListener to a JPanel/Top level class which doesn't have such listeners, either implement it to a new class with naming convention MyItemListener implements ItemListener and create a new instance to add with addItemListener(listener) function or add them inline using the means of anonymous class.
checkBox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
// put your code
}
});
ItemListener is for the Components which works with item i.e CheckBox, ComboBox etc. There are other kind of Even listeners exist too e.g, ActionListener, MouseListener etc
Tutorial Resource:
Writing Event Listeners
Using Layout Managers
I've taken your code and modified it to the following:
package sandbox;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LoginLayoutDemo
{
JPanel cards;
public void addComponentToPane(Container pane)
{
// Panel for text and fields
JPanel textAndFieldsPanel = new JPanel();
textAndFieldsPanel.setLayout(new GridLayout(2,2));
textAndFieldsPanel.add(new JLabel("Username ", JLabel.RIGHT));
textAndFieldsPanel.add(new JTextField("Username", 10));
textAndFieldsPanel.add(new JLabel("Password ", JLabel.RIGHT));
textAndFieldsPanel.add(new JPasswordField("password", 10));
JPanel controlsPanel = new JPanel();
controlsPanel.setLayout(new BoxLayout(controlsPanel, BoxLayout.PAGE_AXIS));
controlsPanel.add(textAndFieldsPanel);
controlsPanel.add(new JCheckBox("Remember credentials"));
JPanel bottomPanel = new JPanel();
bottomPanel.add(controlsPanel);
bottomPanel.add(new JButton("Login")); // end line here
bottomPanel.add(new JButton("Register")); // end line here
cards = new JPanel(new BorderLayout());
// cards.setLayout(new BorderLayout(cards, BoxLayout.LINE_AXIS));
cards.add(bottomPanel, BorderLayout.LINE_END);
pane.add(cards, BorderLayout.PAGE_END); // BOTTOM_RIGHT);// Add cards to bottom right hand
// corner.
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// CardLayoutDemo demo = new CardLayoutDemo();
new LoginLayoutDemo().addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex)
{
ex.printStackTrace();
} catch (IllegalAccessException ex)
{
ex.printStackTrace();
} catch (InstantiationException ex)
{
ex.printStackTrace();
} catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
You really need to learn about LayoutManagers -- the Oracle/Java site has a decent one, and there are others readily available. The thing I think most of them do least well is explain overall what the managers are for and how to design things with them - they tend to plunge right into code.
A LayoutManager is applied to a Container, and tells it how to treat the components that are added to it. FlowLayout and BoxLayout tend to lay things out in a line, either horizontal or vertical at your choice. GridLayout lays things out in a table, with all 'cells' in the grid the same size. BorderLayout has a center section and one section each for N, S, E, and W; N,S stretch horizontally, E,W stretch vertically; all four of these take their other dimension from their contained component, and the center of the BorderLayout stretches in both directions to fill the available space in its container. There is GroupLayout and GridBagLayout, etc., they are all designed to solve some problem or set of problems in UI design, and you need to learn what they do in order to design Swing UIs.
Something that some of the tutorials do but don't really explain: Each container has one layout manager, but the container can be a component in another container, and the enclosing container can have a different layout manager. That's what we've done here; the BorderLayout of the overall frame puts the panel we've built at the bottom, and the right-aligned panel within our panel puts them to the right; that's how they all get to the bottom right.
You may have meant for others of your controls to be on other lines; I'll leave doing that as an exercise for you... Good luck.
One more thing: CardLayout is for situations is where, for some reason, two or more panels are arranged on TOP of each other, i.e., one obscures the other. You have no such need in your UI that I could tell, so I eliminated the CardLayout manager.
Is it possible to create a Window object of some sort in Java that has a frame and border, but no caption buttons (minimise, restore, close).
Naturally, I can't use the undecorated setting. Further, the window needs to:
Have a platform-rendered border
Have a titlebar
Have no caption buttons. If need be, I'll take care of disposing the window programmatically.
Use the default, or System Look and Feel
Here is an example:
this is about
decent How to Create Translucent and Shaped Windows
undecorated JDialog with Compound Borders, then you can create similair or nicer Borders as came from Native OS
create JPanel (orJLabel#opaque(true)) with GradientPaint
or (better non_focusable == my view) JLabel with prepared Icon
add to JPanel / JLabel the Component Mover / Component Resize (notice, don't never to mix these two codes together) by #camickr
set Alpha Transparency for painting in JPanel / JLabel for great look and feel
simplest of ways is put there JMenuBar
The short answer is no.
The longer answer is, probably, but you'll need to investigate a JNI/JNA implementation
Try this small example. It will remove (not only disable) minimse, maximise and close button from JFrame.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Example {
public void buildGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
frame.setResizable(false);
removeButtons(frame);
JPanel panel = new JPanel(new GridBagLayout());
JButton button = new JButton("Exit");
panel.add(button,new GridBagConstraints());
frame.getContentPane().add(panel);
frame.setSize(400,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
System.exit(0);
}
});
}
public void removeButtons(Component comp) {
if(comp instanceof AbstractButton) {
comp.getParent().remove(comp);
}
if (comp instanceof Container) {
Component[] comps = ((Container)comp).getComponents();
for(int x=0, y=comps.length; x<y; x++) {
removeButtons(comps[x]);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Example().buildGUI();
}
});
}
}
I'm trying to add JTextField to JToolBar and it works, but it is too long. I need it to take only 3 letters.
Here is the screenshot of it now...
I tried following methods,
JTextField field = new JextField(3); // thought this limits to three characters.
And I tried,
field.setColumns(3); // this didn't work either.
The default layout of a tool-bar respects the maximum size set for a text field.
import java.awt.*;
import javax.swing.*;
public class TextFieldInToolBar {
TextFieldInToolBar() {
JPanel p = new JPanel(new BorderLayout());
JToolBar tb = new JToolBar();
p.add(tb, BorderLayout.PAGE_START);
Icon disk = (Icon)UIManager.get("FileView.floppyDriveIcon");
Icon pc = (Icon)UIManager.get("FileView.computerIcon");
tb.add(new JButton(disk));
JTextField tf = new JTextField(3);
tf.setMaximumSize(tf.getPreferredSize());
tb.add(tf);
tb.addSeparator();
tb.add(new JButton(pc));
p.setPreferredSize(new Dimension(250,50));
JOptionPane.showMessageDialog(null, p);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TextFieldInToolBar();
}
});
}
}
JToolBar is container as JFrame, JDialog or JWindow, you can laying this container by using the proper LayoutManager
this issue should be the same by using JMenuBar as container for JComponents (alternative)