How to update JTextFields in a GridLayout? - java

I have a MainPanel which uses the Gridlayout. Consequently I have created four JPanel classes for the: NORTH, EAST, CENTER and EAST layouts respectively. I then add all four to my MainPanel.
However, on my WEST panel I use another grid layout to store JButtons and JTextFields. I want to constantly update my JTextFields as they display a value (that changes when a button on another panel is clicked). How do I allow that value to be changed when the JFrame is running?
I tried using paintComponent, but it keeps on adding multiple copies of the same JTextField after each other, as I add it in my paintComponent method. If I remove the add method the values won't update.

Action works well to encapsulate such functionality. In the example below, a number of text fields listen for an ActionEvent received from a single Update button. The common UpdateHandler is derived from AbstractAction.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/** #see http://stackoverflow.com/a/14947144/230513 */
public class Test {
private JButton button = new JButton("Update");
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(createPanel(button), BorderLayout.NORTH);
f.add(createPanel(button), BorderLayout.WEST);
f.add(createPanel(button), BorderLayout.EAST);
f.add(createPanel(button), BorderLayout.SOUTH);
JPanel p = new JPanel();
p.add(button);
f.add(p, BorderLayout.CENTER);
f.getRootPane().setDefaultButton(button);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static JPanel createPanel(JButton b) {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
final JTextField text = new JTextField();
b.addActionListener(new UpdateHandler(text));
panel.add(text);
return panel;
}
private static class UpdateHandler extends AbstractAction {
private JTextField text;
private DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
public UpdateHandler(JTextField t) {
super("update");
t.setText(df.format(new Date()));
this.text = t;
}
#Override
public void actionPerformed(ActionEvent e) {
text.setText(df.format(new Date()));
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Test().display();
}
});
}
}

Related

BorderLayout orientation

I'm trying to create the top buttons of a window. I have a JFrame and a JPanel with the differents buttons when I try to add the panel with the buttons to a JPanel on the frame, it doesn't show... digging and trying to find the solution, I realize that the issue is when I set the orientation to the panel with the buttons on the BorderLayout panel. I think that it might be something dumb that I haven't realize but I haven't found any issue like this.
The issue is here when I set the orientation:
contentPanel.add(buttons,BorderLayout.PAGE_START);
if I remove the:
BorderLayout.PAGE_START
it works
This is my Frame:
package view;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.CardLayout;
import java.awt.BorderLayout;
public class MainFrame extends JFrame{
private JPanel contentPanel, layOutPanel;
private CardLayout mainCardLayout;
private BorderLayout borderLayout;
private static MainFrame instance = null;
private FrameButtonsPanel buttons;
private MainFrame(){
setSize(1000,700);
//setUndecorated(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPanel = new JPanel();
borderLayout = new BorderLayout();
contentPanel.setLayout(borderLayout);
add(contentPanel);
buttons = new FrameButtonsPanel();
buttons.setBackground(Color.red);
contentPanel.add(buttons,BorderLayout.PAGE_START);
/*layOutPanel = new JPanel();
mainCardLayout = new CardLayout();
layOutPanel.setLayout(mainCardLayout);
layOutPanel.setBackground(Color.red);
contentPanel.add(layOutPanel,BorderLayout.SOUTH);*/
}
public static MainFrame getInstance(){
if (instance == null){
instance = new MainFrame();
}
return instance;
}
public static void main(String[] args) {
MainFrame.getInstance().setVisible(true);
}
}
and this is my panel with the buttons:
package view;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.Spring;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameButtonsPanel extends JPanel{
private Spring spring;
private JButton iconify, maximize, close;
public FrameButtonsPanel(){
SpringLayout mySpring = new SpringLayout();
setLayout(mySpring);
iconify = new JButton("-");
add(iconify);
maximize = new JButton("O");
add(maximize);
close = new JButton("X");
add(close);
spring = Spring.constant(850,850,2000);
mySpring.putConstraint(SpringLayout.WEST,iconify,spring,SpringLayout.WEST,this);
mySpring.putConstraint(SpringLayout.WEST,maximize,3,SpringLayout.EAST,iconify);
mySpring.putConstraint(SpringLayout.WEST,close,3,SpringLayout.EAST,maximize);
mySpring.putConstraint(SpringLayout.EAST,this,3,SpringLayout.EAST,close);
iconifyWindow();
maximizeWindow();
closeWindow();
}
private void iconifyWindow(){
iconify.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
MainFrame.getInstance().setExtendedState(JFrame.ICONIFIED);
}
});
}
private void maximizeWindow(){
maximize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(MainFrame.getInstance().getExtendedState() == JFrame.MAXIMIZED_BOTH){
MainFrame.getInstance().setExtendedState(JFrame.NORMAL);
}else{
MainFrame.getInstance().setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
});
}
private void closeWindow(){
close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
}
}
I have no idea why you are trying to use a SpringLayout to display buttons.
Just use a JPanel with a right aligned FlowLayout.
Read the FlowLayout API for more information on how to right align the components added to the panel.

How can I switch focus to a canvas?

I have this class and I want to switch focus to the Game class right after it was invoked. I might've not understand the purpose of focus but when I press start I have to click on the game canvas itself so I can use the keyboard . In other words: How can I make it so I don't have to click on it to use the keyboard?
package com.runner.panels;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import com.runner.main.Game;
import com.runner.main.Main;
public class PlayPanel extends JPanel{
private static final long serialVersionUID = 1L;
public PlayPanel(){
//setting the layout of the playpanel to null
setLayout(null);
//setting up the info panel : high score, meters ran, pause button etc...
JPanel info = new JPanel();
info.setBounds(0,0,1200,50);
add(info);
//back button
JButton back = new JButton("Back");
info.add(back);
Game game = new Game();
game.setBounds(0,50,1200,521);
game.setBackground(Color.black);
add(game);
back.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout) Main.mainp.getLayout();
cl.show(Main.mainp, "Menu");
}
});
}
}
Off topic: (kinda)
The fact that you are doing Main.mainp.getLayout();, calling the panel statically tells me you have poor design and should be looking into other options like an Model-view-controller pattern, an Observer pattern, or at the very least passing a reference of of the Main to the panel, instead of using static objects/calls.
Back on topic
Sounds like a common KeyListener problem. Generally to gain focus you call requestFocusInWindow(). But you still have to worry about other components stealing the focus way after the fact.
I would instead recommend using Key Bindings instead of KeyListener. You have more control over the focus. For instance, by using
InputMap im = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke("SPACE"), "hitSpace");
panel.getActionMap().put("hitSpace", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
// do something.
}
});
The panel will have immediate access to the action once you show it from the CardLayout. If you happen to use any other components that would steal the focus away from the panel, the action is still accessible because of the WHEN_IN_FOCUSED_WINDOW input map
Here's a simple example. Type A if it is on panel A, you will see it print. If you type B, it won't print because panel A is in the window. Also if you try and press the button in the panel to steal the focus, you can still type and it will still print. Same goes for panel B
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class Main {
CardLayout layout = new CardLayout();
JPanel panel = new JPanel(layout);
JPanel p1 = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
JPanel p2 = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
JButton b1 = new JButton("panelA");
JButton b2 = new JButton("panelB");
public Main() {
addKeyBind(p1, "pressA", "A");
addKeyBind(p2, "pressB", "B");
p1.add(new JButton("Button for Panel A"));
p2.add(new JButton("Button for Panel B"));
panel.add(p1, "panelA");
panel.add(p2, "panelB");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
show("panelA");
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
show("panelB");
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(b1);
buttonPanel.add(b2);
JFrame frame = new JFrame();
frame.add(panel);
frame.add(buttonPanel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void show(String panelName) {
layout.show(panel, panelName);
}
private void addKeyBind(JComponent comp, String name, final String stroke) {
InputMap im = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(stroke), name);
comp.getActionMap().put(name, new AbstractAction(){
public void actionPerformed(ActionEvent e) {
System.out.println(stroke + " pressed");
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new Main();
}
});
}
}
Take some time to go over the link I gave you for Key Bindings to learn more.
Back off-topic
Take a look at #AndrewThompson's comment about the null layouts. Learn how to use the LayoutManagers
I believe your problem would be fixed if you add
setFocusable(true);
to your PlayPanel constructor (works for me).
Also, if you want a specific Panel in your GUI to have focus when you start your application, follow the link in the comment of "user3218114", as this will explain how to implement this functionality with Listeners.
Good luck!
You don't need to do anything. The container should be focusable for that.
Here is a code to demonstrate.
package one;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
public class PlayPanel extends Canvas {
public static void main(String... args) {
PlayPanel p = new PlayPanel();
p.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
p.msg = "Focus gained";
p.repaint();
}
#Override
public void focusLost(FocusEvent e) {
p.msg = "Focus Lost";
p.repaint();
}
});
p.setBackground(Color.GRAY);
JFrame f = new JFrame();
f.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 200);
f.setLocation(300, 300);
f.setVisible(true);
}
String msg = "NO FOCUS";
public void paint(Graphics g) {
g.drawString(msg, 50, 50);
}
}

Java Swings Select Tab

I have 4 JPanels. In one of the panel I have a combo Box.Upon selecting "Value A" in combo box Panel2 should be displayed.Similarly if I select "Value B" Panel3 should be selected....
Though action Listener should be used in this context.How to make a call to another tab with in that action listener.
public class SearchComponent
{
....
.
public SearchAddComponent(....)
{
panel = addDropDown(panelList(), "panel", gridbag, h6Box);
panel.addComponentListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ItemSelectable is = (ItemSelectable)actionEvent.getSource();
Object name=selectedString(is);
}
});
}
public static final Vector<String> panelList(){
List<String> panelList = new ArrayList<String>();
panelList.add("A");
panelList.add("B");
panelList.add("C");
panelList.add("D");
panelList.add("E");
panelList.add("F);
Vector<String> panelVector = null;
Collections.copy(panelVector, panelList);
return panelVector;
}
public Object selectedString(ItemSelectable is) {
Object selected[] = is.getSelectedObjects();
return ((selected.length == 0) ? "null" : (ComboItem)selected[0]);
}
}
Use a Card Layout. See the Swing tutorial on How to Use a Card Layout for a working example.
Try This code:
import java.awt.EventQueue;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CardLayoutExample {
JFrame guiFrame;
CardLayout cards;
JPanel cardPanel;
public static void main(String[] args) {
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new CardLayoutExample();
}
});
}
public CardLayoutExample()
{
guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("CardLayout Example");
guiFrame.setSize(400,300);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
guiFrame.setLayout(new BorderLayout());
//creating a border to highlight the JPanel areas
Border outline = BorderFactory.createLineBorder(Color.black);
JPanel tabsPanel = new JPanel();
tabsPanel.setBorder(outline);
JButton switchCards = new JButton("Switch Card");
switchCards.setActionCommand("Switch Card");
switchCards.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
cards.next(cardPanel);
}
});
tabsPanel.add(switchCards);
guiFrame.add(tabsPanel,BorderLayout.NORTH);
cards = new CardLayout();
cardPanel = new JPanel();
cardPanel.setLayout(cards);
cards.show(cardPanel, "Fruits");
JPanel firstCard = new JPanel();
firstCard.setBackground(Color.GREEN);
addButton(firstCard, "APPLES");
addButton(firstCard, "ORANGES");
addButton(firstCard, "BANANAS");
JPanel secondCard = new JPanel();
secondCard.setBackground(Color.BLUE);
addButton(secondCard, "LEEKS");
addButton(secondCard, "TOMATOES");
addButton(secondCard, "PEAS");
cardPanel.add(firstCard, "Fruits");
cardPanel.add(secondCard, "Veggies");
guiFrame.add(tabsPanel,BorderLayout.NORTH);
guiFrame.add(cardPanel,BorderLayout.CENTER);
guiFrame.setVisible(true);
}
//All the buttons are following the same pattern
//so create them all in one place.
private void addButton(Container parent, String name)
{
JButton but = new JButton(name);
but.setActionCommand(name);
parent.add(but);
}
}

How to add JLabel to JEditorPane?

I am trying to extend the StyledEditorKit in Swing to be able to include a JLabel inside the editor. I was able to do that and this is what I got so far. In the image below, the highlighted text button is of type JLabel whereas the rest of the text is normal text.
As you can see the label renders a little below than the normal text. How do I align its top with top of the remaining text?
Here is the code for the view that is used to create this label element:
class ComponentView(Element elem) {
#Override
protected Component createComponent() {
JLabel lbl = new JLabel("");
lbl.setOpaque(true);
lbl.setBackground(Color.red);
try {
int start = getElement().getStartOffset();
int end = getElement().getEndOffset();
String text = getElement().getDocument().getText(start, end - start);
lbl.setText(text);
} catch (BadLocationException e) {}
return lbl;
}
}
Try adjusting Component.getAlignmentY that controls the positioning of component relative to the text baseline as suggested in ComponentView.
You could also try using JTextPane that provides easier support for embedded components. Components can be added using insertComponent() method. Here is an example, it also demos setAlignmentY:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
public class TextPaneDemo {
private static void createAndShowGUI() {
final JTextPane pane = new JTextPane();
pane.setText("Some text");
JButton buttonButton = new JButton("Insert label");
buttonButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JLabel label = new JLabel("label");
label.setAlignmentY(0.85f);
pane.insertComponent(label);
}
});
JPanel panel = new JPanel(new BorderLayout());
panel.add(buttonButton, BorderLayout.SOUTH);
panel.add(pane, BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(400, 200);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

How to make radio buttons change text dynamically in Java

I'm fairly new to GUI. I'm trying to make it so that depending on which radio button is selected, a JLabel changes its value. For example, if "id" is selected, it'll display "http://steamcommunity.com/id/" and if "profile" is selected, it'll display "http://steamcommunity.com/profiles/". I have some code up and running and it's nearly complete:
package sgt;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class RadioButtonPrompt extends JPanel
implements ActionListener {
private static final long serialVersionUID = 1L;
static String idString = "ID";
static String profileString ="Profile";
static String type = idString;
public RadioButtonPrompt() {
super(new BorderLayout());
// Create radio buttons.
JRadioButton idButton = new JRadioButton(idString, true);
idButton.setMnemonic(KeyEvent.VK_I);
idButton.setActionCommand(idString);
JRadioButton profileButton = new JRadioButton(profileString);
profileButton.setMnemonic(KeyEvent.VK_P);
profileButton.setActionCommand(profileString);
// Group radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(idButton);
group.add(profileButton);
idButton.addActionListener(this);
profileButton.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(idButton);
radioPanel.add(profileButton);
JPanel textPanel = new JPanel ();
JLabel URL = new JLabel(setJLabelValue());
JTextField text = new JTextField("sampletextfield");
text.setPreferredSize(new Dimension(100, 20));
textPanel.add(URL);
textPanel.add(text);
JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
JButton submit = new JButton("Submit");
submit.setMnemonic(KeyEvent.VK_S);
buttonPanel.add(submit);
add(radioPanel, BorderLayout.LINE_START);
add(textPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
setBorder(BorderFactory.createCompoundBorder());
}
private String setJLabelValue() {
if (type.equals("ID")) {
return "http://steamcommunity.com/id/";
}
return "http://steamcommunity.com/profiles/";
}
public void actionPerformed(ActionEvent e) {
// Returns either "Profile" or "ID"
type = ((JRadioButton)e.getSource()).getText();
System.out.println(type);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Steam Game Tracker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new RadioButtonPrompt();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Take a look at this SO thread.
in actionPerformed() you need to textpanel.setText() to whatever you want based on which button was clicked. I'm guessing at the method name, haven't done any UI stuff with Java for a while.

Categories