I would like to create a textwidget/component wich looks like the firefox address bar. I mean a Textfield wich allows me to place little Buttons inside the field (e.g. cancel/reload/...)
I tried customizing a JLayeredPane, by creating a custom layout manager which maximizes the Textfield, and places the remainder from right to left. My problem is that this gave painting issues, I would not always see the items I added over the textfield. This might be Jython related, I try suppling java.lang.Integer(1) to the JLayeredPane.add. However the Layers are ordered exactly reverse to what the documentation says.
TO cricumvent this I derived my own JLayeredPane class and redefined paint to call paintComponents which in turn iterates over all components and calls their paint method, starting with the textbox, the rest thereafter.
However I don't always get the updates right away, meaning the buttons are hidden/only partly displayed and I can't Interact with the button.
What do I have to acutally see the update on screen (is it hidden in a buffer?))
How can I make it so that I can interact with the buttons?
How can I shorten the Texxtfield, so that the text starts scrolling to the front before I reach the end of the Textfield so that the text does not get hidden by the buttons? I still want the Textfields area to extend under the buttons
edit: the button is only displayed in the right place after i make the window smaller, after that it is also clickable
edit2:
I took the freedom to boil the answer down to this, which hides away a lot of that button code/unneccessary stuff
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class playground {
private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
public playground() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(makeButton(), BorderLayout.WEST);
JTextField text = new JTextField(20);
text.setBorder(null);
panel.add(text, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel();
buttonsPanel.setOpaque(false);
buttonsPanel.setLayout(new GridLayout(1, 2, 2, 2));
buttonsPanel.add(makeButton());
buttonsPanel.add(makeButton());
panel.add(buttonsPanel, BorderLayout.EAST);
panel.setBackground(text.getBackground());
JMenuBar menuBar = new JMenuBar();
menuBar.add(panel);
menuBar.add(Box.createHorizontalGlue());
JFrame frame = new JFrame("MenuGlueDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(menuBar);
frame.pack();
frame.setVisible(true);
}
public JToggleButton makeButton() {
final JToggleButton button = new JToggleButton();
button.setFocusable(false);
button.setMargin(new Insets(0, 0, 0, 0));
button.setContentAreaFilled(false);
button.setBorder(null);
button.setIcon((errorIcon));
button.setRolloverIcon((infoIcon));
button.setSelectedIcon(warnIcon);
button.setPressedIcon(warnIcon);
button.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
if (button.isSelected()) {
} else {
}
}
});
return button;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
playground menuGlueDemo = new playground();
}
});
}
}
may be could it be simple by using JMenuBar, with Auto complete ComboBox / JFextField for example
import java.awt.ComponentOrientation;
import javax.swing.*;
public class MenuGlueDemo {
public MenuGlueDemo() {
JMenuBar menuBar = new JMenuBar();
menuBar.add(createMenu("Menu 1"));
menuBar.add(createMenu("Menu 2"));
menuBar.add(createMenu("Menu 3"));
menuBar.add(new JSeparator());
menuBar.add(new JButton(" Seach .... "));
menuBar.add(new JTextField(" Seach .... "));
menuBar.add(new JComboBox(new Object[]{"height", "length", "volume"}));
menuBar.add(Box.createHorizontalGlue());
menuBar.add(createMenu("About"));
JFrame frame = new JFrame("MenuGlueDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(menuBar);
frame.pack();
frame.setVisible(true);
}
public JMenu createMenu(String title) {
JMenu m = new JMenu(title);
m.add("Menu item #1 in " + title);
m.add("Menu item #2 in " + title);
m.add("Menu item #3 in " + title);
if (title.equals("About")) {
m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
return m;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MenuGlueDemo menuGlueDemo = new MenuGlueDemo();
}
});
}
}
EDIT
I can simply but a text input and some buttons in a container with a proper layout and achieve [Textfield...] [B1] [B2] but I want [Textfield [B1] [B2]]
with proper LayoutManager
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class MenuGlueDemo {
private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
public MenuGlueDemo() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JButton button = new JButton();
button.setFocusable(false);
//button.setMargin(new Insets(0, 0, 0, 0));
button.setContentAreaFilled(false);
button.setIcon((errorIcon));
button.setPressedIcon(warnIcon);
panel.add(button, BorderLayout.WEST);
JTextField text = new JTextField(20);
text.setBorder(null);
panel.add(text, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel();
buttonsPanel.setOpaque(false);
buttonsPanel.setLayout(new GridLayout(1, 2, 2, 2));
final JToggleButton toggleButton = new JToggleButton();
toggleButton.setFocusable(false);
toggleButton.setMargin(new Insets(0, 0, 0, 0));
toggleButton.setContentAreaFilled(false);
toggleButton.setIcon((errorIcon));
toggleButton.setRolloverIcon((infoIcon));
toggleButton.setSelectedIcon(warnIcon);
toggleButton.setPressedIcon(warnIcon);
toggleButton.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
if (toggleButton.isSelected()) {
} else {
}
}
});
buttonsPanel.add(toggleButton);
final JToggleButton toggleButton1 = new JToggleButton();
toggleButton1.setFocusable(false);
toggleButton1.setMargin(new Insets(0, 0, 0, 0));
toggleButton1.setContentAreaFilled(false);
toggleButton1.setIcon((errorIcon));
toggleButton1.setRolloverIcon((infoIcon));
toggleButton1.setSelectedIcon(warnIcon);
toggleButton1.setPressedIcon(warnIcon);
toggleButton1.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
if (toggleButton1.isSelected()) {
} else {
}
}
});
buttonsPanel.add(toggleButton1);
panel.add(buttonsPanel, BorderLayout.EAST);
panel.setBackground(text.getBackground());
JMenuBar menuBar = new JMenuBar();
menuBar.add(createMenu("Menu 1"));
menuBar.add(createMenu("Menu 2"));
menuBar.add(createMenu("Menu 3"));
menuBar.add(new JSeparator());
menuBar.add(new JButton(" Seach .... "));
menuBar.add(panel);
menuBar.add(new JComboBox(new Object[]{"height", "length", "volume"}));
menuBar.add(Box.createHorizontalGlue());
menuBar.add(createMenu("About"));
JFrame frame = new JFrame("MenuGlueDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(menuBar);
frame.pack();
frame.setVisible(true);
}
private JMenu createMenu(String title) {
JMenu m = new JMenu(title);
m.add("Menu item #1 in " + title);
m.add("Menu item #2 in " + title);
m.add("Menu item #3 in " + title);
if (title.equals("About")) {
m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
return m;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MenuGlueDemo menuGlueDemo = new MenuGlueDemo();
}
});
}
}
You may be able to adapt the approach shown in Component Border, which allows "a JTextField and a JButton to work together." The related article Text Prompt may also prove useful. Finally, consider JToolBar, illustrated here, as a flexible way to tie components together.
Related
I am stuck on getting this swing UI to act the way I was hoping. I wrote this demo code to showcase what it is doing and I will now explain what I was hoping to make it do.
I have a JFrame and 3 JPanels
https://i.stack.imgur.com/B82tF.png
I want the JFrame to have an image in the background on the JFrame, like a world map, then on top of that I was trying to have: a top nav bar with buttons, then on top of the map, I want buttons that a player can click on for different areas of the map on the layer below, then I want to have a drawer that opens and closes if the user clicks on the show/hide drawer button that gives info about the action they performed by clicking the buttons.
What I have so far is three panels all aligned side by side and that is not what I want.
How can i get this UI to act like I described above?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TestFrame extends JFrame {
static JFrame frame;
static JButton btnExit, btnShowHide;
static JPanel gridPanel, drawerPanel;
public static void main(String[] args)
{
GridLayout layout = new GridLayout(1,1,0,0);
frame = new JFrame("Main Frame");
frame.setLayout(layout);
// 1: Creating grid panel
gridPanel = new JPanel();
gridPanel.setBackground(Color.yellow);
gridPanel.setLayout(new GridLayout(5, 5, 0, 0));
gridPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
gridPanel.setOpaque(false);
gridPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
placeButtons();
// 2: Creating button panel
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.red);
// add buttons
btnExit = new JButton("Exit");
buttonPanel.add(btnExit);
btnExit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
System.exit(0);
} catch (Exception err) {
System.out.println("doh");
}
}
});
// 3: Creating button panel
drawerPanel = new JPanel();
drawerPanel.setBackground(Color.blue);
btnShowHide = new JButton("show drawer");
buttonPanel.add(btnShowHide);
btnShowHide.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
System.out.println("show drawer");
drawerPanel.setVisible(true);
} catch (Exception err) {
System.out.println("Could not close the DB: " + err);
}
if(btnShowHide.getText().equals("show drawer")){
btnShowHide.setText("hide drawer");
} else{
btnShowHide.setText("show drawer");
drawerPanel.setVisible(false);
}
}
});
// Adding panels to frame
frame.add(gridPanel);
frame.add(buttonPanel);
frame.add(drawerPanel);
frame.setSize(500, 500);
frame.setVisible(true);
}
public static void placeButtons(){
System.out.println("place buttons");
int dbx = 0;
int dby = 0;
for(int xCnt = 0; xCnt < 5; xCnt++){
dby = 0;
for(int yCnt = 0; yCnt < 5; yCnt++) {
JButton click = new JButton("x:"+xCnt+" y:"+yCnt);
gridPanel.add(click);
dby++;
}
dbx++;
}
}
}```
A common strategy to solve complex computing tasks is to break them into small, well-defined manageable tasks. Divide and conquer.
This also applies to gui: break the design into small, easy-to-layout containers and take it step by step.
First step: to have a background image implement a main panel, override its paintComponent to draw the image:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TestFrame /*extends JFrame*/ {
private static final String imageURL = "https://previews.123rf.com/images/pingebat/pingebat1710/pingebat171000035/88604429-great-detail-illustration-of-the-world-map-in-vintage-style-.jpg";
public static void main(String[] args) throws IOException {
URL url = new URL(imageURL);
BufferedImage image = ImageIO.read(url);
JFrame frame = new JFrame("Main Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPane(image));
frame.pack();
frame.setVisible(true);
}
}
class MainPane extends JPanel{
private final Image background;
private final Dimension size;
MainPane(Image background) {
this.background = background;
size = new Dimension(background.getWidth(null), background.getHeight(null));
}
#Override
public Dimension preferredSize() {
return size;
}
#Override //Override to paint image at the background
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, null);
}
}
Second step: add controls buttons at the top. Define the buttons panel:
class ControlsPane extends JPanel{
public ControlsPane(ActionListener listener) {
setOpaque(false);
JButton btnShowHide = new JButton("Show Drawer");
add(btnShowHide);
btnShowHide.addActionListener(listener);
JButton btnExit = new JButton("Exit");
add(btnExit);
btnExit.addActionListener(e-> System.exit(0));
}
}
and modify MainPane constructor to use BorderLayout and add the buttons panel as suggested by #camickr:
MainPane(Image background) {
this.background = background;
size = new Dimension(background.getWidth(null), background.getHeight(null));
setLayout(new BorderLayout(5,5));
//action listener for show drawer button
ActionListener listener = e-> System.out.println("Show Drawer clicked");
add(new ControlsPane(listener), BorderLayout.PAGE_START);
}
Now take it to the next step (for example add a drawer).
Something like this?
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
JToolBar toolBar = new JToolBar(JToolBar.HORIZONTAL);
toolBar.setFloatable(false);
frame.add(toolBar, new GridBagConstraints(0, 0, 2, 3, 0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
JPanel drawer = new JPanel();
drawer.setBackground(Color.BLUE);
drawer.setOpaque(true);
drawer.setVisible(false);
JPanel content = new JPanel();
content.setBackground(Color.GREEN);
content.setOpaque(true);
frame.add(drawer, new GridBagConstraints(0, 1, 1, 2, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(4, 4, 4, 4), 0, 0));
frame.add(content, new GridBagConstraints(1, 1, 1, 2, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(4, 4, 4, 4), 0, 0));
Action toggleDrawer = new AbstractAction("Toggle Drawer") {
#Override
public void actionPerformed(ActionEvent e) {
drawer.setVisible(!drawer.isVisible());
frame.revalidate();
frame.repaint();
}
};
toolBar.add(new JButton(toggleDrawer));
frame.pack();
frame.setSize(300, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
I am making an UI in a minecraft plugin. Everything is working, except I have a JPanel and it doesn't fill the whole JFrame. So what I want is the JPanel fill the entire JFrame even if we re-scale the window.
I use Layout manager (FlowLayout) for the JPanel.
I tried using a Layout manager for the JFrame, well it didn't solved my problem because it didn't resize the JPanel.. I tried setting the size of the JPanel to the JFrame's size, but when it's resized it doesn't scale with it.
So, how can I do this?
My plugin creates a button for every player and when I click the button it kicks the player.
My code (I can't really post less because I don't know where I need to change something):
public static JFrame f;
public static JTextField jtf;
public static JPanel jp;
public static void creategui()
{
System.out.println("GUI created.");
f = new JFrame("Players");
jp = new JPanel();
jp.setLayout(new FlowLayout(FlowLayout.LEFT));
jp.setBackground(Color.GRAY);
jtf = new JTextField("Reason");
jtf.setPreferredSize(new Dimension(200,20));
jtf.setToolTipText("Write the reason here.");
jp.setSize(new Dimension(200,200));
f.setLayout(null);
f.setSize(500,500);
f.setVisible(true);
jp.add(jtf);f.add(jp, BorderLayout.CENTER);
for (final Player p : Bukkit.getOnlinePlayers())
{
System.out.println("Looping.");
final JButton b = new JButton();
b.setName(p.getName());
b.setText(p.getName());
b.setToolTipText("Kick " + b.getText());
b.setBackground(Color.GREEN);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!b.getBackground().equals(Color.RED))
{
Bukkit.getScheduler().runTask(main, new Runnable() {
public void run() {
Bukkit.getPlayer(b.getText()).kickPlayer(jtf.getText());
b.setBackground(Color.RED);
}
});
}
}
});
jp.add(b);
System.out.println("Button added.");
}
f.add(jp, BorderLayout.CENTER);
}
The question should include an mcve reproducing the problem so we can test it.
It could look like this :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Arrays;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Mcve {
private static List<String> players = Arrays.asList(new String[]{"Player A", "Player B"});
public static void main(String[] args) {
creategui();
}
public static void creategui()
{
JPanel jp = new JPanel();
jp.setBackground(Color.GRAY);
JTextField jtf = new JTextField("Reason");
jtf.setPreferredSize(new Dimension(200,20));
jtf.setToolTipText("Write the reason here.");
jp.setSize(new Dimension(200,200));
jp.add(jtf);
for (final String p : players)
{
final JButton b = new JButton();
b.setText(p);
b.setBackground(Color.GREEN);
b.addActionListener(e -> {
if (!b.getBackground().equals(Color.RED))
{
b.setBackground(Color.RED);
}
});
jp.add(b);
}
JFrame f = new JFrame("Players");
f.setLayout(null);
f.setSize(500,500);
f.add(jp, BorderLayout.CENTER);
f.setVisible(true);
}
}
To make the JPanel fill the entire frame simply remove this line :
f.setLayout(null);
and let the default BorderLayout manager do its work.
Here is a modified version with some additional comments:
public class Mcve {
private static List<String> players = Arrays.asList(new String[]{"Player A", "Player B"});
public static void main(String[] args) {
creategui();
}
public static void creategui()
{
JPanel jp = new JPanel();
jp.setBackground(Color.GRAY);
JTextField jtf = new JTextField("Reason");
jtf.setPreferredSize(new Dimension(200,20));
jtf.setToolTipText("Write the reason here.");
jp.setPreferredSize(new Dimension(250,200)); // set preferred size rather than size
jp.add(jtf);
for (final String p : players)
{
final JButton b = new JButton();
b.setText(p);
b.setBackground(Color.GREEN);
b.addActionListener(e -> {
if (!b.getBackground().equals(Color.RED))
{
b.setBackground(Color.RED);
}
});
jp.add(b);
}
JFrame f = new JFrame("Players");
//f.setLayout(null); null layouts are bad practice
//f.setSize(500,500); let layout managers set the sizes
f.add(jp, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
A 1x1 grid layout does the job quite nicely.
window = new JFrame();
panel = new JPanel();
window.setLayout(new java.awt.GridLayout(1, 1));
window.add(panel);
Either set the layout manager for jp (the JPanel in the code you posted) to BorderLayout and add jtf (the JTextField in the code you posted) to the CENTER of jp, as in:
f = new JFrame();
jp = new JPanel(new BorderLayout());
jtf = new JTextField(30); // number of columns
jp.add(jtf, BorderLayout.CENTER);
f.add(jp, BorderLayout.CENTER);
or dispense with jp and add jtf directly to f (the JFrame in the code you posted), as in:
f = new JFrame();
jtf = new JTextField(30);
f.add(jtf, BorderLayout.CENTER);
The key is that the CENTER component of BorderLayout expands to fill the available space.
So I fixed it somehow, this is the code:
public static void creategui()
{
System.out.println("GUI created.");
f = new JFrame("Players");
jp = new JPanel();
jp.setBackground(Color.GRAY);
jp.setSize(200,200);
jtf = new JTextField(30);
jtf.setToolTipText("Write the reason here.");
jp.add(jtf);
for (final Player p : Bukkit.getOnlinePlayers())
{
System.out.println("Looping.");
final JButton b = new JButton();
b.setName(p.getName());
b.setText(p.getName());
b.setToolTipText("Kick " + b.getText());
b.setBackground(Color.GREEN);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!b.getBackground().equals(Color.RED))
{
Bukkit.getScheduler().runTask(main, new Runnable() {
public void run() {
getplr(b.getText()).kickPlayer(jtf.getText());
b.setBackground(Color.RED);
}
});
}
}
});
jp.add(b);
System.out.println("Button added.");
}
f.setLayout(new BorderLayout());
f.add(jp, BorderLayout.CENTER);
f.setSize(500,500);
f.pack();
f.setVisible(true);
}
I am working on a project for my college course. I was just wondering if anyone knew how to add a scrollBar to a JTextArea. At present I have the GUI laid out correctly, the only thing missing is the scroll bar.
This is what the GUI looks like. As you can see on the second TextArea I would like to add the Scrollbar.
This is my code where I create the pane. But nothing seems to happen... t2 is the JTextArea I want to add it to.
scroll = new JScrollPane(t2);
scroll.setBounds(10,60,780,500);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Any help would be great, thanks!
The Scroll Bar comes when your text goes beyond the bounds of your view area. Don't use Absolute Positioning, for such a small talk at hand, always prefer Layout Managers, do read the first para of the first link, to know the advantage of using a Layout Manager.
What you simply need to do is use this thingy :
JTextArea msgArea = new JTextArea(10, 10);
msgArea.setWrapStyleWord(true);
msgArea.setLineWrap(true);
JScrollPane msgScroller = new JScrollPane();
msgScroller.setBorder(
BorderFactory.createTitledBorder("Messages"));
msgScroller.setViewportView(msgArea);
panelObject.add(msgScroller);
Here is a small program for your understanding :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextAreaScroller
{
private JTextArea msgArea;
private JScrollPane msgScroller;
private JTextArea logArea;
private JScrollPane logScroller;
private JButton sendButton;
private JButton terminateButton;
private Timer timer;
private int counter = 0;
private String[] messages = {
"Hello there\n",
"How you doing ?\n",
"This is a very long text that might won't fit in a single line :-)\n",
"Okay just to occupy more space, it's another line.\n",
"Don't read too much of the messages, instead work on the solution.\n",
"Byee byee :-)\n",
"Cheers\n"
};
private ActionListener timerAction = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
if (counter < messages.length)
msgArea.append(messages[counter++]);
else
counter = 0;
}
};
private void displayGUI()
{
JFrame frame = new JFrame("Chat Messenger Dummy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(0, 1, 5, 5));
logArea = new JTextArea(10, 10);
logArea.setWrapStyleWord(true);
logArea.setLineWrap(true);
logScroller = new JScrollPane();
logScroller.setBorder(
BorderFactory.createTitledBorder("Chat Log"));
logScroller.setViewportView(logArea);
msgArea = new JTextArea(10, 10);
msgArea.setWrapStyleWord(true);
msgArea.setLineWrap(true);
msgScroller = new JScrollPane();
msgScroller.setBorder(
BorderFactory.createTitledBorder("Messages"));
msgScroller.setViewportView(msgArea);
centerPanel.add(logScroller);
centerPanel.add(msgScroller);
JPanel bottomPanel = new JPanel();
terminateButton = new JButton("Terminate Session");
terminateButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
if (timer.isRunning())
timer.stop();
else
timer.start();
}
});
sendButton = new JButton("Send");
bottomPanel.add(terminateButton);
bottomPanel.add(sendButton);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(bottomPanel, BorderLayout.PAGE_END);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
timer = new Timer(1000, timerAction);
timer.start();
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new JTextAreaScroller().displayGUI();
}
});
}
}
Here is the outcome of the same :
The scroll bar by default will only be shown when the content overfills the available viewable area
You can change this via the JScrollPane#setVerticalScrollBarPolicy method, passing it ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
I have found how to keep a JButton in its pressed state using this code:
JButton[] buttons;
.
.
.
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i < buttons.length; i++)
{
if(e.getSource() == buttons[i])
{
buttons[i].getModel().setPressed(true);
}
else
{
buttons[i].getModel().setPressed(false);
}
}
}
This code captures the clicked button, keeps it pressed, and makes all other buttons on the panel unpressed. And this code works great... until the window loses focus (or more specifically, its parent JPanel loses focus). After that, all the buttons return to a non-pressed state.
Right now the tutorial on how to write WindowFocusListeners is down. Is there a way to make a JButton's pressed state persist through a loss of focus?
Why not simply use a series of JToggleButtons and add them to the same ButtonGroup object? All the hard work is done for you since the toggle button is built to stay in pressed state if pressed. Think of it as a JRadioButton that looks like a JButton (since in actuality, JRadioButton descends from JToggleButton).
For example:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class BunchOfButtons extends JPanel {
private static final String[] TEXTS = {"One", "Two", "Three", "Four", "Five"};
private ButtonGroup btnGroup = new ButtonGroup();
private JTextField textField = new JTextField(20);
public BunchOfButtons() {
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
BtnListener btnListener = new BtnListener();
for (String text : TEXTS) {
JToggleButton toggleBtn = new JToggleButton(text);
toggleBtn.addActionListener(btnListener);
toggleBtn.setActionCommand(text);
btnPanel.add(toggleBtn);
btnGroup.add(toggleBtn);
}
JPanel otherPanel = new JPanel();
otherPanel.add(textField ); // just to take focus elsewhere
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new GridLayout(0, 1, 0, 15));
add(btnPanel);
add(otherPanel);
}
private class BtnListener implements ActionListener {
public void actionPerformed(ActionEvent aEvt) {
textField.setText(aEvt.getActionCommand());
}
}
private static void createAndShowGui() {
BunchOfButtons mainPanel = new BunchOfButtons();
JFrame frame = new JFrame("BunchOfButtons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
import java.awt.Frame;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class IndicatorWindow implements ItemListener {
JRadioButton RMA, EMA, SMA, Williams, Stochastic;
JPanel IndPan, RadioPanel, title;
JLabel Lab;
JButton OK;
public JPanel createContentPane() {
JPanel GUI = new JPanel();
GUI.setLayout(null);
title = new JPanel();
title.setLayout(null);
title.setLocation(0, 0);
title.setSize(500, 145);
GUI.add(title);
Lab = new JLabel("Please Select Indicator Type");
Lab.setLocation(5, 0);
Lab.setSize(200, 30);
title.add(Lab);
ButtonGroup bg1 = new ButtonGroup();
RadioPanel = new JPanel();
RadioPanel.setLayout(null);
RadioPanel.setLocation(10, 30);
RadioPanel.setSize(190, 220);
GUI.add(RadioPanel);
RMA = new JRadioButton("RMA");
RMA.setLocation(0, 0);
RMA.addItemListener(this);
RMA.setSize(110, 20);
bg1.add(RMA);
RadioPanel.add(RMA);
EMA = new JRadioButton("EMA");
EMA.setLocation(0, 30);
EMA.addItemListener(this);
EMA.setSize(110, 20);
bg1.add(EMA);
RadioPanel.add(EMA);
SMA = new JRadioButton("SMA");
SMA.setLocation(0, 60);
SMA.addItemListener(this);
SMA.setSize(110, 20);
bg1.add(SMA);
RadioPanel.add(SMA);
Stochastic = new JRadioButton("Stochastic");
Stochastic.setLocation(0, 90);
Stochastic.addItemListener(this);
Stochastic.setSize(110, 20);
bg1.add(Stochastic);
RadioPanel.add(Stochastic);
Williams = new JRadioButton("Williams");
Williams.setLocation(0, 120);
Williams.addItemListener(this);
Williams.setSize(110, 20);
bg1.add(Williams);
RadioPanel.add(Williams);
OK = new JButton();
OK.setText("Confirm");
OK.setLocation(45, 150);
OK.addItemListener(this);
OK.setSize(90, 30);
RadioPanel.add(OK);
//GUI.setOpaque(true);
return GUI;
}
public void itemStateChanged(ItemEvent e) {
Object source = e.getItemSelectable();
if (source == RMA) {
System.out.print("Browse");
} else if (source == EMA) {
System.out.print("EMA");
} else if (source == SMA) {
System.out.print("SMA");
} else if (source == Williams) {
System.out.print("Williams");
} else if (source == Stochastic) {
System.out.print("Stochastic");
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Indicators");
IndicatorWindow ind = new IndicatorWindow();
frame.setContentPane(ind.createContentPane());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(200, 250);
frame.setLayout(null);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setAlwaysOnTop(true);
frame.setState(Frame.NORMAL);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
My problem is that when i compile and run this code, the jFrame appears but there is only one problem, 3 JRadioButtons dont appear until you put your mouse over them. The RMA and Williams radiobuttons appear, the 3 in the middle do not though, any thoughts on why this is?
http://i.stack.imgur.com/gNnIb.jpg
You should be using layout managers. People think using a "null layout" is easier, but it is not and you are more prone to having errors with your code. Layout managers will position and size components properly to make sure all components are displayed. Sometimes you even use multiple different layout managers to achieve the layout you desire.
Your problem in this case is that you have two components occupying the same space in your container. So one component gets painted over top of the other. After you mouse over your radio button, the button is repainted because of the rollover effect of the button. However, now try resizing the frame and the radio buttons will disappear because all the components are repainted and the component is painted over top of the buttons again.
The following line of code is the problem:
// title.setSize(500, 145);
title.setSize(500, 20);
But the real solution is to rewrite the code and use layout managers. While you are at it use proper Java naming conventions. Variable names do NOT start with an uppercase letter. You got "title" and "bg1" correct. So fix "EMA", "RMA" etc...
#camickr is correct. Note how using layout managers (and a little re-factoring) can actually simplify your code. Also, the relevant tutorial suggests using an action listener, rather than an item listener.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** #see http://stackoverflow.com/questions/5255337 */
public class IndicatorWindow implements ActionListener {
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
JRadioButton rma, ema, sma, stochastic, williams;
ButtonGroup bg = new ButtonGroup();
public JPanel createContentPane() {
JPanel gui = new JPanel(new BorderLayout());
JPanel title = new JPanel();
JLabel lab = new JLabel("Please Select Indicator Type");
title.add(lab);
gui.add(title, BorderLayout.NORTH);
createRadioButton(rma, "RMA");
createRadioButton(ema, "EMA");
createRadioButton(sma, "SMA");
createRadioButton(stochastic, "Stochastic");
createRadioButton(williams, "Williams");
gui.add(radioPanel, BorderLayout.CENTER);
JButton ok = new JButton();
ok.setText("Confirm");
ok.addActionListener(this);
radioPanel.add(ok);
return gui;
}
private void createRadioButton(JRadioButton jrb, String name) {
jrb = new JRadioButton(name);
bg.add(jrb);
jrb.addActionListener(this);
radioPanel.add(jrb);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Indicators");
frame.add(new IndicatorWindow().createContentPane());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
You should add your JRadioButtons with a method:
private void bgAdd (String name, int y)
{
JRadioButton rb = new JRadioButton (name);
rb.setLocation (0, y);
rb.addItemListener (this);
rb.setSize (110, 19);
bg1.add (rb);
radioPanel.add (rb);
}
Calling code:
bgAdd ("RMA", 0);
bgAdd ("EMA", 30);
bgAdd ("SMA", 60);
bgAdd ("Stochastic", 90);
bgAdd ("Williams", 120);
Action:
public void itemStateChanged (ItemEvent e) {
Object button = e.getItemSelectable ();
String source = ((JRadioButton) button).getText ();
System.out.print (source + " ");
}
Then add BoxLayout to the page, for example.