Use Spring layout For Hand GUI - java

I have a jframe that has a lable and two radiobuttons.
I use spring layout, But my second radioButton seen in top left of page!
public class tester extends JFrame {
public tester() {
add(create());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 500);
setVisible(true);
}
public JPanel create() {
JPanel panel = new JPanel();
ButtonGroup group = new ButtonGroup();
JRadioButton r1 = new JRadioButton("Yes");
JRadioButton r2 = new JRadioButton("No");
group.add(r1);
group.add(r2);
JLabel lable = new JLabel("Today is sunday?");
panel.add(lable);
// panel.add(group); // How add this?
panel.add(r1);
panel.add(r2);
JButton savebt= new JButton("Save");
JButton cancelbt=new JButton("Cancell");
panel.add(savebt);
panel.add(cancelbt);
panel.setLayout(new SpringLayout());
SpringUtilities.makeCompactGrid(panel, 1, 3, 50, 100, 25, 50);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new tester();
}
});
}
}
Now this exception occur:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: No such child: 5
I want to display my two buttons on below of radio button's line!

There are three items in your panel, so the number of columns should be 3:
SpringUtilities.makeCompactGrid(panel, 1, 3, 50, 100, 25, 50);
// panel.add(group); // How add this?
You don't need this. ButtonGroups don't get added to a panel. They are for button selection management and are not displayed.

Related

How to calculate summation values from multiple JTextFields using loop in java

I am trying to making a calculator.
Here the user can add multiple JTextFields to take his/her desired input with just one button click.
Now I want that the user will take the input in multiple JTextFields added by him and on clicking the Result button will show the sum of all. But I am always getting 0 as output.
Code:
public class Button extends JFrame {
private JPanel contentPane;
private JButton btnAdd;
private JButton btnResult;
private JTextField resultField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Button frame = new Button();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Button() {
initComponents();
}
static JTextField field = null;
//static JTextField fields[] = new JTextField[10];
private static int y = 0;
ArrayList<String> arr = new ArrayList<String>();
int ans, sum = 0;
private void initComponents() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 527, 414);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
field = new JTextField();
field.setBounds(45, y += 60, 284, 32);
field.setAlignmentX(Component.CENTER_ALIGNMENT);
contentPane.add(field);
contentPane.revalidate();
contentPane.repaint();
}
});
btnAdd.setBounds(170, 341, 89, 23);
contentPane.add(btnAdd);
btnResult = new JButton("Result");
btnResult.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < arr.size(); i++) {
arr.add(field.getText());
sum += Integer.parseInt(arr.get(i));
}
resultField.setText(String.valueOf(sum));
}
});
btnResult.setBounds(383, 306, 89, 23);
contentPane.add(btnResult);
resultField = new JTextField();
resultField.setBounds(361, 275, 129, 20);
contentPane.add(resultField);
resultField.setColumns(10);
}
}
Please help how can I find the correct output?
Suggestions:
Again, when you create a data-entry text field, add it to the GUI and add it to an ArrayList of the data entry field type.
Then in the result button's ActionListener, iterate through this list using a for loop.
Inside of the for loop, get the entry field, get its text (via .getText() if using a JTextField), parse it to number via Integer.parseInt(...), and add it to a sum variable that is initialized to 0 prior to the for loop. Then display the result after the loop.
Also,
Best to use JSpinners that use a SpinnerNumberModel such as JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 1000, 1)); instead of JTextField for number entry. This will limit the user to entering numbers only, and won't allow non-numeric text entry, a danger inherent in your current design.
Having to add your entry fields by button may be an over-complication
But if it is necessary, then best to add the spinners (or text fields if you must) to a JPanel that uses a proper layout manager, such a new GridLayout(0, 1) (variable number of rows, 1 column) and then add that to a JScrollPane so that you can see as many fields as has been entered.
If using a JSpinner, then you don't even need a "calculate result" button, since if you add a ChangeListener to each JSpinner, you can calculate the result on the fly whenever a spinner has had its data changed.
e.g.,
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
#SuppressWarnings("serial")
public class Button2 extends JPanel {
private List<JSpinner> spinnerList = new ArrayList<>();
private JButton resultButton = new JButton("Result");
private JButton addEntryFieldBtn = new JButton("Add Entry Field");
private JTextField resultField = new JTextField(6);
private JPanel fieldPanel = new JPanel(new GridLayout(0, 1, 4, 4));
public Button2() {
resultField.setFocusable(false);
resultButton.addActionListener(e -> calcResult());
resultButton.setMnemonic(KeyEvent.VK_R);
addEntryFieldBtn.addActionListener(e -> addEntryField());
JPanel topPanel = new JPanel();
topPanel.add(addEntryFieldBtn);
topPanel.add(resultButton);
topPanel.add(new JLabel("Result:"));
topPanel.add(resultField);
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.add(fieldPanel, BorderLayout.PAGE_START);
JScrollPane scrollPane = new JScrollPane(centerPanel);
scrollPane.setPreferredSize(new Dimension(300, 300));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
setLayout(new BorderLayout());
add(topPanel, BorderLayout.PAGE_START);
add(scrollPane);
}
private void calcResult() {
int sum = 0;
for (JSpinner spinner : spinnerList) {
sum += (int) spinner.getValue();
}
resultField.setText(String.valueOf(sum));
}
private void addEntryField() {
JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 1000, 1));
spinner.addChangeListener(evt -> {
calcResult();
});
fieldPanel.add(spinner);
spinnerList.add(spinner);
revalidate();
repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Button2 mainPanel = new Button2();
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}

How to add to a JFrame 3 JPanels: The 1st JPanel to take up 90% of the frame, the second to be on skinny top, 3rd to be drawer panel that can close

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);
}

final JPanel with added labels (java)

Iam trying to position my jlabel in a jpanel what is made final because of an actionlistener.
final JPanel panelPayDetails = new JPanel();
panelPayDetails.setBounds(250, 25, 350, 250);
JLabel lblnumber = new JLabel("Insert Number:");
lblnumber.setFont(Applicatie.FONT_12_BOLD);
lblnumber.setBounds(5, 5, 200, 20);
panelPayDetails.add(lblnumber);
panelPayDetails.setVisible(false);
jpExtraDetails.add(panelPayDetails);
bill.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(bill.getSelectedItem() == "CREDIT CARD")
{
panelPayDetails.setVisible(true);
}
}
});
im not luck so far.. because the label is positioned in the middle of the jpanel.. how come?
Because the default layout manager for JPanel is FlowLayout, which centers things. Read this tutorial.

Panels overlap each other when box not big enough

I have this gui; and when the height is not big enough the panes will overlap each other. I have to set it at least 200, so I can completely see the two rows; but when it is set at 200, then I have like a big empty row at the end, and I don't want that. How could I fix this? Thanks.
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JButton panicButton;
JButton dontPanic;
JButton blameButton;
JButton newsButton;
JButton mediaButton;
JButton saveButton;
JButton dontSave;
public MyFrame() {
super("Crazy App");
setSize(400, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel row1 = new JPanel();
panicButton = new JButton("Panic");
dontPanic = new JButton("No Panic");
blameButton = new JButton("Blame");
newsButton = new JButton("News");
//adding first row
GridLayout grid1 = new GridLayout(4, 2, 10, 10);
setLayout(grid1);
FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row1.setLayout(flow1);
row1.add(panicButton);
row1.add(dontPanic);
row1.add(blameButton);
row1.add(newsButton);
add(row1);
//adding second row
JPanel row2 = new JPanel();
mediaButton = new JButton("Blame");
saveButton = new JButton("Save");
dontSave = new JButton("No Save");
GridLayout grid2 = new GridLayout(3, 2, 10, 10);
setLayout(grid2);
FlowLayout flow2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row2.setLayout(flow2);
row2.add(mediaButton);
row2.add(saveButton);
row2.add(dontSave);
add(row2);
setVisible(true);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}
The original code set the layout for one panel on two separate occasions. For clarity, set it once in the constructor.
The 2nd layout specified 3 rows
Call pack() on the top-level container to have the GUI reduce to the minum sze needed for the components.
End result
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.*;
public class MyFrame17 extends JFrame {
JButton panicButton;
JButton dontPanic;
JButton blameButton;
JButton newsButton;
JButton mediaButton;
JButton saveButton;
JButton dontSave;
public MyFrame17() {
super("Crazy App");
setLayout(new GridLayout(2, 2, 10, 10));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel row1 = new JPanel();
panicButton = new JButton("Panic");
dontPanic = new JButton("No Panic");
blameButton = new JButton("Blame");
newsButton = new JButton("News");
FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row1.setLayout(flow1);
row1.add(panicButton);
row1.add(dontPanic);
row1.add(blameButton);
row1.add(newsButton);
add(row1);
//adding second row
JPanel row2 = new JPanel();
mediaButton = new JButton("Blame");
saveButton = new JButton("Save");
dontSave = new JButton("No Save");
FlowLayout flow2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row2.setLayout(flow2);
row2.add(mediaButton);
row2.add(saveButton);
row2.add(dontSave);
add(row2);
pack();
setVisible(true);
}
public static void main(String[] args) {
MyFrame17 frame = new MyFrame17();
}
}
Further tips
Don't extend frame, just use an instance of one.
Build the entire GUI in a panel which can then be added to a frame, applet, dialog..
When developing test classes, give them a more sensible name than MyFrame. A good word to add is Test, then think about what is being tested. This is about the layout of buttons, so ButtonLayoutTest might be a good name.
GUIs should be started on the EDT.

JRadioButton Will not appear until Mouse over

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.

Categories