Alignment of items in a Java Applet JPanel - java

I'm trying to get the top right square to be the same width as the two below it and I'd like to get the JTextArea below to match width as well. Any ideas?
It seems like no matter what I set the sizes to it's doing whatever it wants. For instance the Output JTextArea is set to just one column. The top image is (700x250) and the two half-images are (350x250).
public class MyApplet extends Applet{
private static final long serialVersionUID = 1L;
private JTextArea input_data;
private JTextArea input_jmax;
private JTextArea input_gibbs;
private JTextArea input_burnin;
private JTextArea output_text;
private JLabel output_graph;
private JLabel output_burn1;
private JLabel output_burn2;
private static Graphics g=null;
public void init () {
//INPUT
this.input_data = new JTextArea("Enter Data", 30, 30);
JScrollPane data_pane= new JScrollPane(input_data);
this.input_jmax = new JTextArea("Polya-Tree Levels", 1, 30);
this.input_gibbs = new JTextArea("Gibbs Iterates", 1, 30);
this.input_burnin = new JTextArea("Burnin", 1, 30);
//OUTPUT
Dimension D;
D = new Dimension(700, 250);
Image start;
this.output_text = new JTextArea("####################Output####################",15,1);
this.output_text.setEditable(false);
JScrollPane output_pane= new JScrollPane(output_text);
this.output_burn1 = new JLabel();
D = new Dimension(345,250);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,345,250);
output_burn1.setIcon(new ImageIcon(start));
this.output_burn2 = new JLabel();
D = new Dimension(345, 250);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,345,250);
output_burn2.setIcon(new ImageIcon(start));
this.output_graph = new JLabel();
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,700,250);
output_graph.setIcon(new ImageIcon(start));
//BUTTON
JButton b = new JButton("Process Data");
//set size
setSize(1200, 600);
setBackground(Color.lightGray);
JPanel burninPanel = new JPanel();
burninPanel.setLayout(new BoxLayout(burninPanel, BoxLayout.X_AXIS));
burninPanel.add(output_burn1);
burninPanel.add(Box.createHorizontalStrut(10));
burninPanel.add(output_burn2);
/*
//Create Input Side
* */
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
inputPanel.setBackground(Color.LIGHT_GRAY);
inputPanel.add(data_pane);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_jmax);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_gibbs);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_burnin);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(b);
//Create Output Side
JPanel outputPanel = new JPanel();
outputPanel.setBackground(Color.lightGray);
outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
outputPanel.add(output_graph);
outputPanel.add(Box.createVerticalStrut(10));
outputPanel.add(burninPanel);
outputPanel.add(Box.createVerticalStrut(10));
outputPanel.add(output_pane);
this.setLayout(new GridLayout(1, 2));
this.setVisible(true);
GroupLayout layout = new GroupLayout(this);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(inputPanel)
.addComponent(outputPanel)
)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(inputPanel)
.addComponent(outputPanel)
)
);
this.setLayout(layout);
this.setVisible(true);
// specify that action events sent by the
// button or the input TextField should be handled
// by the same CapitalizerAction object
Multiplicity ca = new Multiplicity(input_data, input_jmax, input_gibbs, input_burnin, output_text,output_graph);
b.addActionListener(ca);
//this.input.addActionListener(ca);
}
}

It turns out that you can't add an image of the same size to the stack. Instead of placing all the items an "outputPanel" I put each one in it's own panel and added all the panels to the outputPanel. Such a stupid thing!
public class MyApplet extends Applet{
private static final long serialVersionUID = 1L;
private JTextArea input_data;
private JTextArea input_jmax;
private JTextArea input_gibbs;
private JTextArea input_burnin;
private JTextArea output_text;
private JLabel output_graph;
private JLabel output_burn1;
private JLabel output_burn2;
private static Graphics g=null;
public void init () {
//INPUT
this.input_data = new JTextArea("Data", 30, 30);
JScrollPane data_pane= new JScrollPane(input_data);
this.input_jmax = new JTextArea("Polya-Tree Levels", 1, 30);
this.input_gibbs = new JTextArea("Gibbs Iterates", 1, 30);
this.input_burnin = new JTextArea("Burn-in", 1, 30);
//OUTPUT
Dimension D;
Image start;
this.output_burn1 = new JLabel();
D = new Dimension(345,200);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,345,200);
output_burn1.setIcon(new ImageIcon(start));
this.output_burn2 = new JLabel();
D = new Dimension(345, 200);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,345,200);
output_burn2.setIcon(new ImageIcon(start));
this.output_graph = new JLabel();
D = new Dimension(700, 200);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,700,200);
output_graph.setIcon(new ImageIcon(start));
//BUTTON
JButton b = new JButton("Process Data");
//set size
setSize(1200, 600);
setBackground(Color.lightGray);
//Create Input Side
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.LIGHT_GRAY);
buttonPanel.add(b);
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
inputPanel.setBackground(Color.LIGHT_GRAY);
inputPanel.add(data_pane);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_jmax);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_gibbs);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_burnin);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(buttonPanel);
//Create Output Side
JPanel estPanel = new JPanel();
estPanel.setBackground(Color.LIGHT_GRAY);
estPanel.add(output_graph);
JPanel burninPanel = new JPanel();
burninPanel.setBackground(Color.LIGHT_GRAY);
burninPanel.setLayout(new BoxLayout(burninPanel, BoxLayout.X_AXIS));
burninPanel.add(output_burn1);
burninPanel.add(Box.createHorizontalStrut(10));
burninPanel.add(output_burn2);
this.output_text = new JTextArea("",15,1);
this.output_text.setEditable(false);
JScrollPane output_pane= new JScrollPane(output_text);
JPanel outputPanel = new JPanel();
outputPanel.setBackground(Color.lightGray);
outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
outputPanel.add(estPanel);
outputPanel.add(Box.createVerticalStrut(10));
outputPanel.add(burninPanel);
outputPanel.add(Box.createVerticalStrut(10));
outputPanel.add(output_pane);
this.setVisible(true);
GroupLayout layout = new GroupLayout(this);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(inputPanel)
.addComponent(outputPanel)
)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(inputPanel)
.addComponent(outputPanel)
)
);
this.setLayout(layout);
this.setVisible(true);
// specify that action events sent by the
// button or the input TextField should be handled
// by the same CapitalizerAction object
Multiplicity ca = new Multiplicity(input_data, input_jmax, input_gibbs, input_burnin, output_text,output_graph,output_burn1,output_burn2);
b.addActionListener(ca);
}
}

Related

Adding components, like buttons, onto others, like JTextAreas, in code is not appearing when I run

Below is a picture of the basic interface I want created. Everything works fine except for the following:-
The JScrollPane isn't appearing when I try inserting lots of text
in the InformationDisplay JTextArea and I don't get why
The buttons I inserted in the unitButtonPanel in the availableUnits JTextArea isn't appearing at all
This is the part in my code that tackles point #1
InfoPanel with informationDisplay TextArea and JScrollPane
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
JTextArea informationDisplay= new JTextArea();
JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollbar1.setSize( 20, 20 );
informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n");
informationDisplay.setPreferredSize(new Dimension(getWidth(), 200));
infoPanel.add(scrollbar1);
add(infoPanel, BorderLayout.SOUTH);
This is the part in my code that tackles point #2
Adding a textPanel 3 rows, 1 column to encapsulate 3 TextAreas
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(3, 1, 4, 4));
TextArea1 "availableUnits" 2 rows and 2 columns to encapsulate 4 buttons
JTextArea availableUnits = new JTextArea(2,2);
//availableUnits.setPreferredSize(new Dimension(200, 200));
JPanel unitButtonPanel = new JPanel();
unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2));
JButton ambulanceUnit = new JButton("Ambulance");
JButton diseaseControlUnit = new JButton ("diseaseControlUnit");
JButton gasControlUnit = new JButton("gasControlUnit");
JButton fireTruck = new JButton("fireTruck");
unitButtonPanel.add(ambulanceUnit);
unitButtonPanel.add(diseaseControlUnit);
unitButtonPanel.add(gasControlUnit);
unitButtonPanel.add(fireTruck);
availableUnits.add(unitButtonPanel);
textPanel.add(availableUnits);
This is the full code ready for compilation:-
import java.awt.*;
import javax.swing.*;
public class GameView extends JFrame {
private JPanel buttonPanel;
private JPanel infoPanel;
private JTextArea informationDisplay;
private JTextArea availableUnits;
private JPanel unitButtonPanel;
private JPanel disasterPanel;
private JTextArea disasterDisplay;
private JButton ambulanceUnit;
private JButton diseaseControlUnit;
private JButton gasControlUnit;
private JButton fireTruck;
public static void main(String[] args) {
new GameView();
}
public GameView() {
setSize(1000, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
int xPos = (dim.width/2) - (this.getWidth()/2);
int yPos = (dim.height/2) - (this.getHeight()/2);
this.setLocation(xPos,yPos);
JPanel title = new JPanel();
JLabel label1 = new JLabel("Invaded City");
title.add(label1);
add(title, BorderLayout.NORTH);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(10, 10, 2, 2));
for (int i = 0; i < 100; i++) {
JButton mapButton = new JButton();
addButton(mapButton);
}
add(buttonPanel, BorderLayout.CENTER);
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(3, 1, 4, 4));
availableUnits = new JTextArea(2,2);
//availableUnits.setPreferredSize(new Dimension(200, 200));
unitButtonPanel = new JPanel();
unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2));
ambulanceUnit = new JButton("Ambulance");
diseaseControlUnit = new JButton ("diseaseControlUnit");
gasControlUnit = new JButton("gasControlUnit");
fireTruck = new JButton("fireTruck");
unitButtonPanel.add(ambulanceUnit);
unitButtonPanel.add(diseaseControlUnit);
unitButtonPanel.add(gasControlUnit);
unitButtonPanel.add(fireTruck);
availableUnits.add(unitButtonPanel);
textPanel.add(availableUnits);
JTextArea respondingUnits = new JTextArea(2,2);
//respondingUnits.setPreferredSize(new Dimension(200, 200));
textPanel.add(respondingUnits);
JTextArea treatingUnits = new JTextArea(2,2);
//treatingUnits.setPreferredSize(new Dimension(200, 200));
textPanel.add(treatingUnits);
add(textPanel, BorderLayout.EAST);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
JTextArea informationDisplay= new JTextArea();
JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollbar1.setSize( 20, 20 );
informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n");
informationDisplay.setPreferredSize(new Dimension(getWidth(), 200));
infoPanel.add(scrollbar1);
add(infoPanel, BorderLayout.SOUTH);
disasterPanel = new JPanel();
disasterPanel.setLayout(new BorderLayout());
disasterDisplay = new JTextArea();
disasterDisplay.setPreferredSize(new Dimension(getWidth(), 200));
disasterPanel.add(disasterDisplay);
add(disasterPanel, BorderLayout.WEST);
pack();
setVisible(true);
}
public void addButton(JButton b) {
buttonPanel.add(b);
}
}
Output:-

Is there an easier way to shift 'older' components down when adding newer components (whenever a button is pressed)?

I've tried looking for an answer to my question, but couldn't find anything similar. If it's already been asked, please link. Thanks in advance.
The layout of the main panel, mainPanel, is GridBagLayout. It has three buttons. Two of them are duds (for the purpose of this question). The middle button, butt2, creates a JPanel with other components in it every time butt2 is pressed.
Because butt2 is in the middle, and butt3 is directly below it, I have an int variable, tracker2, that tracks the gridy of butt2. Every time butt2 is pressed, I create a new JPanel that goes under butt2, increment tracker2, and then remove butt3 and add it below the newer component.
import java.util.List;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Demo implements ActionListener
public static void main(String[] args) {
Demo demo = new Demo();
}
private JFrame frame;
private JPanel mainPanel;
private JButton butt1, butt2, butt3;
private GridBagConstraints gb;
private List<JTextField> list;
private int count, tracker2;
public Demo() {
frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setBounds(0, 0, 800, 800);
list = new ArrayList<JTextField>();
count = 0;
tracker2 = 0;
commence();
}
private void commence() {
gb = new GridBagConstraints();
gb.anchor = GridBagConstraints.FIRST_LINE_START;
gb.weightx = 1;
gb.insets = new Insets(50, 5, 0, 20);
mainPanel = new JPanel();
mainPanel.setBackground(Color.white);
mainPanel.setLayout( new GridBagLayout() );
butt1 = new JButton("One");
butt1.setPreferredSize( new Dimension(100, 50) );
// Add to panel
gb.gridx = 0;
gb.gridy = 0;
mainPanel.add( butt1, gb);
butt2 = new JButton("Two");
butt2.setPreferredSize( new Dimension(100, 50) );
butt2.addActionListener(this);
// Add to panel
gb.gridy++;
tracker2 = gb.gridy;
mainPanel.add( butt2, gb );
butt3 = new JButton("Three");
butt3.setPreferredSize( new Dimension(100, 50) );
// Add to panel
gb.gridy++;
mainPanel.add( butt3, gb );
frame.add(mainPanel);
frame.setVisible(true);
frame.repaint();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals( butt2 )) {
commence2();
}
}
private void commence2() {
gb.insets = new Insets( 0, 0, 0, 0 );
list.add( new JTextField(30) );
JLabel label = new JLabel("LABEL 2 ");
label.setDisplayedMnemonic( KeyEvent.VK_N );
label.setLabelFor( list.get(count) );
JPanel panel = new JPanel( new FlowLayout(FlowLayout.LEFT, 10, 3));
panel.setBackground( Color.white );
panel.add(label);
panel.add(list.get( count ));
// Add to mainPanel
tracker2++;
gb.gridy = tracker2;
mainPanel.add( panel, gb );
updateFrame();
// Increment count
count++;
frame.revalidate();
frame.repaint();
}
private void updateFrame() {
mainPanel.remove( butt3 );
gb.insets = new Insets(50, 5, 0, 20);
gb.gridy = tracker2 + 1;
mainPanel.add( butt3, gb );
}
}
Is there an easier way to do this or a Layout that automatically does this for me?
Yes, there is an easier way. Instead of adding the new text fields to your mainPanel use an additional Container. E.g.
public class Demo2 implements ActionListener {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Demo2();
}
});
}
private JFrame frame;
private JPanel textPanel;
private JButton butt1, butt2, butt3;
public Demo2() {
frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setBounds(0, 0, 800, 800);
commence();
}
private void commence() {
GridBagConstraints gb = new GridBagConstraints();
gb.anchor = GridBagConstraints.FIRST_LINE_START;
gb.weightx = 1;
gb.insets = new Insets(50, 5, 0, 20);
JPanel mainPanel = new JPanel();
mainPanel.setBackground(Color.white);
mainPanel.setLayout( new GridBagLayout() );
butt1 = new JButton("One");
butt1.setPreferredSize( new Dimension(100, 50) );
// Add to panel
gb.gridx = 0;
gb.gridy = 0;
mainPanel.add( butt1, gb);
butt2 = new JButton("Two");
butt2.setPreferredSize( new Dimension(100, 50) );
butt2.addActionListener(this);
// Add to panel
gb.gridy++;
gb.insets = new Insets(50, 5, 0, 0);
mainPanel.add( butt2, gb );
textPanel = new JPanel(new GridLayout(0, 1));
// Add to panel
gb.gridy++;
gb.insets = new Insets(0, 5, 0, 20);
mainPanel.add( textPanel, gb );
butt3 = new JButton("Three");
butt3.setPreferredSize( new Dimension(100, 50) );
// Add to panel
gb.gridy++;
gb.insets = new Insets(50, 5, 0, 20);
mainPanel.add( butt3, gb );
frame.add(mainPanel);
frame.setVisible(true);
frame.repaint();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals( butt2 )) {
commence2();
}
}
private void commence2() {
JTextField jtf = new JTextField(30);
JLabel label = new JLabel("LABEL 2 ");
label.setDisplayedMnemonic(KeyEvent.VK_N );
label.setLabelFor( jtf );
JPanel panel = new JPanel( new FlowLayout(FlowLayout.LEFT, 10, 3));
panel.setBackground( Color.white );
panel.add(label);
panel.add( jtf );
// Add to mainPanel
textPanel.add( panel );
textPanel.revalidate();
frame.revalidate();
frame.repaint();
}
}
In the above code, textPanel serves as container for the new text fields.

JFreeChart hide behind other one

I was trying to build UI in IntelliJ but it was hard so I tried to build it in Eclipse. Now UI looks little better but still poor. One of chart (the cener one) should be biger and second one on south should be smaller but flat and long. I tried to use preffered size or validate or repaint but still nothing. How to anchor them to side borders, and avoid to cover them self?
public class MainWindow extends JFrame{
private JPanel contentPane;
private XYSeries daneXYSciezki;
private XYSeries daneXYWys;
private final XYSeriesCollection wykCenter;
private final XYSeriesCollection wykSouth;
public MainWindow(){
setTitle("ParserGPS");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 880, 640);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel panelNorth = new JPanel();
FlowLayout flowLayout = (FlowLayout) panelNorth.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
contentPane.add(panelNorth, BorderLayout.NORTH);
JButton btnWczytajPlik = new JButton("Wczytaj plik");
panelNorth.add(btnWczytajPlik);
JLabel lblOdlego = new JLabel("Odległość:");
panelNorth.add(lblOdlego);
JLabel lblm = new JLabel("0m");
panelNorth.add(lblm);
JLabel lblCzas = new JLabel("Czas:");
panelNorth.add(lblCzas);
JLabel label = new JLabel("00:00:00");
panelNorth.add(label);
JLabel lblPrdko = new JLabel("Prędkość:");
panelNorth.add(lblPrdko);
JLabel lblkmh = new JLabel("0km/h");
panelNorth.add(lblkmh);
JLabel lblNrSat = new JLabel("Nr sat:");
panelNorth.add(lblNrSat);
JLabel label_1 = new JLabel("0");
panelNorth.add(label_1);
JLabel lblGga = new JLabel("GGA:");
lblGga.setHorizontalAlignment(SwingConstants.CENTER);
panelNorth.add(lblGga);
JLabel label_2 = new JLabel("0/0");
panelNorth.add(label_2);
JLabel lblGsa = new JLabel("GSA:");
panelNorth.add(lblGsa);
JLabel label_3 = new JLabel("0/0");
panelNorth.add(label_3);
JLabel lblNewLabel = new JLabel("RMC:");
panelNorth.add(lblNewLabel);
JLabel label_4 = new JLabel("0/0");
panelNorth.add(label_4);
JLabel lblGll = new JLabel("GLL:");
panelNorth.add(lblGll);
JLabel label_5 = new JLabel("0/0");
panelNorth.add(label_5);
JLabel lblVtg = new JLabel("VTG:");
panelNorth.add(lblVtg);
JLabel label_6 = new JLabel("0/0");
panelNorth.add(label_6);
JPanel jpCenter = new JPanel();
contentPane.add(jpCenter, BorderLayout.CENTER);
jpCenter.setPreferredSize(new Dimension(785, 440));
JPanel jpSouth = new JPanel();
FlowLayout flowLayout_1 = (FlowLayout) jpSouth.getLayout();
flowLayout_1.setAlignment(FlowLayout.TRAILING);
contentPane.add(jpSouth, BorderLayout.SOUTH);
this.daneXYSciezki = new XYSeries("Trasa", false);
wykCenter = new XYSeriesCollection(this.daneXYSciezki);
final JFreeChart jfcWykCenter = createChart(wykCenter);
final ChartPanel jfcPanelCenter = new ChartPanel(jfcWykCenter);
jpCenter.add(jfcPanelCenter,BorderLayout.CENTER);
this.daneXYSciezki = new XYSeries("Wysokość", false);
wykSouth = new XYSeriesCollection(this.daneXYSciezki);
final JFreeChart jfcWykSouth = createChart(wykSouth);
final ChartPanel jfcPanelSouth = new ChartPanel(jfcWykSouth);
jpSouth.add(jfcPanelSouth,BorderLayout.CENTER);
repaint();
revalidate();
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createXYLineChart(
"",
"Szerokość",
"Długość",
dataset);
final XYPlot plot = result.getXYPlot();
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
yAxis.setAutoRangeIncludesZero(false);
yAxis.setAutoRange(true);
customizeChart(result);
return result;
}
private void customizeChart(JFreeChart chart) {
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer;
renderer = new XYLineAndShapeRenderer(true, true);
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesShapesVisible(1, false);
renderer.setSeriesPaint(0, Color.RED);
renderer.setSeriesStroke(0, new BasicStroke(1.0f));
plot.setRenderer(renderer);
plot.setBackgroundPaint(Color.WHITE);
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.BLACK);
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.BLACK);
}
}
After #m.cekiera advices
public class MainWindow extends JFrame{
private JPanel contentPane;
private XYSeries daneXYSciezki;
private XYSeries daneXYWys;
private final XYSeriesCollection wykCenter;
private final XYSeriesCollection wykSouth;
public MainWindow(){
setTitle("ParserGPS");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 880, 640);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel panelNorth = new JPanel();
FlowLayout flowLayout = (FlowLayout) panelNorth.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
contentPane.add(panelNorth,BorderLayout.NORTH);
JButton btnWczytajPlik = new JButton("Wczytaj plik");
panelNorth.add(btnWczytajPlik);
JLabel lblOdlego = new JLabel("Odległość:");
panelNorth.add(lblOdlego);
JLabel lblm = new JLabel("0m");
panelNorth.add(lblm);
JLabel lblCzas = new JLabel("Czas:");
panelNorth.add(lblCzas);
JLabel label = new JLabel("00:00:00");
panelNorth.add(label);
JLabel lblPrdko = new JLabel("Prędkość:");
panelNorth.add(lblPrdko);
JLabel lblkmh = new JLabel("0km/h");
panelNorth.add(lblkmh);
JLabel lblNrSat = new JLabel("Nr sat:");
panelNorth.add(lblNrSat);
JLabel label_1 = new JLabel("0");
panelNorth.add(label_1);
JLabel lblGga = new JLabel("GGA:");
//lblGga.setHorizontalAlignment(SwingConstants.CENTER);
panelNorth.add(lblGga);
JLabel label_2 = new JLabel("0/0");
panelNorth.add(label_2);
JLabel lblGsa = new JLabel("GSA:");
panelNorth.add(lblGsa);
JLabel label_3 = new JLabel("0/0");
panelNorth.add(label_3);
JLabel lblNewLabel = new JLabel("RMC:");
panelNorth.add(lblNewLabel);
JLabel label_4 = new JLabel("0/0");
panelNorth.add(label_4);
JLabel lblGll = new JLabel("GLL:");
panelNorth.add(lblGll);
JLabel label_5 = new JLabel("0/0");
panelNorth.add(label_5);
JLabel lblVtg = new JLabel("VTG:");
panelNorth.add(lblVtg);
JLabel label_6 = new JLabel("0/0");
panelNorth.add(label_6);
JPanel jpCenter = new JPanel(new BorderLayout());
contentPane.add(jpCenter, BorderLayout.CENTER);
//jpCenter.setPreferredSize(new Dimension(785, 440));
JPanel jpSouth = new JPanel(new BorderLayout());
//FlowLayout flowLayout_1 = (FlowLayout) jpSouth.getLayout();
// flowLayout_1.setAlignment(FlowLayout.TRAILING);
contentPane.add(jpSouth, BorderLayout.SOUTH);
this.daneXYSciezki = new XYSeries("Trasa", false);
wykCenter = new XYSeriesCollection(this.daneXYSciezki);
final JFreeChart jfcWykCenter = createChart(wykCenter);
final ChartPanel jfcPanelCenter = new ChartPanel(jfcWykCenter);
jpCenter.add(jfcPanelCenter,BorderLayout.NORTH);
this.daneXYSciezki = new XYSeries("Wysokość", false);
wykSouth = new XYSeriesCollection(this.daneXYSciezki);
final JFreeChart jfcWykSouth = createChart(wykSouth);
final ChartPanel jfcPanelSouth = new ChartPanel(jfcWykSouth);
jpCenter.add(jfcPanelSouth,BorderLayout.SOUTH);
}
...
}
As your code is not MCVE I could not run it with changes, but I think your problem is bad layout settings. For example, you create a new JPanel:
JPanel jpSouth = new JPanel();
FlowLayout flowLayout_1 = (FlowLayout) jpSouth.getLayout();
flowLayout_1.setAlignment(FlowLayout.TRAILING);
You are aware that it is using FlowLayout by defaul, by later you use:
jpSouth.add(jfcPanelSouth,BorderLayout.CENTER);
but you never change layout to BorderLayout. You cannot mix layout of one particular container. But this is not what couse you problem directly.
Your BorderLayout.SOUTH area stretch horizontally, to fit components inside, and BorderLayout.CENTER takes all spece left(or not). This is how those parts of BorderLayout works. So you can change where you put particular components, or change layout at all.
I suggest to try with:
JPanel jpCenter = new JPanel(new BorderLayout());
...
jpCenter.add(jfcPanelCenter,BorderLayout.NORTH);
...
jpCenter.add(jfcPanelSouth,BorderLayout.SOUTH); //not to contentPane!
this will change an arrangement. However I am not sure, is it what you want. If not, try with different layout, for example BoxLayout.
Also I would rather use pack() and set size of components, than setBounds on a countainer.
You can also use GUI builders, like Netbeans IDE GUI Builder if you want to create GUI but don't care how.

Setting the size of JPanel that contains JRadioButton

I have some problem , i want my jpanel to look like this
but after codING i had this view and i didnt find the solution ,
i need some helps please.
i put the radio button inside jScrollPan
//listmateriel is a list (vector) of radio button ----------
//imagebox is jpanel contains jradio buttons and the image ---------
//gridlayoutPanel2 is Jpanel contains the two text fields and jlabel and a JComboBox
package tpjava;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
i.......
public class Frame extends JFrame implements ActionListener {
private static final int Haut = 550;
private static final int Larg = 720;
private final JPanel utilisateur;
private final JButton recherche;
private final JRadioButton radioButton,radioButton2;
private Vector listmateriel;
private final JLabel label;
public Frame()
{
super("Gestion des materiels");
setSize(Larg,Haut);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane menu = new JTabbedPane();
JPanel emprunt = new JPanel();
emprunt.setPreferredSize(new Dimension(Larg, Haut ));
emprunt.setLayout(new BoxLayout(emprunt, BoxLayout.Y_AXIS));
emprunt.setBorder(BorderFactory.createEmptyBorder(20,20, 20, 20));
JPanel emp1 = new JPanel();
emp1.setPreferredSize(new Dimension(Larg-40, 200 ));
emp1.setLayout(new BorderLayout());
JLabel title2 = new JLabel("Nouvel emprunt:");
title2.setBorder(BorderFactory.createEmptyBorder(20,20,0,0));
JPanel box2 = new JPanel();
box2.setPreferredSize(new Dimension(Larg, 40 ));
box2.setLayout(new BoxLayout(box2, BoxLayout.X_AXIS));
box2.add(title2);
box2.add(Box.createHorizontalStrut(650));
emprunt.add(box2);
JLabel label3 = new JLabel("test");
JButton dispo = new JButton("Disponibilité");
dispo.addActionListener((ActionListener) this);
dispo.setPreferredSize(new Dimension(200, 30));
JPanel box1 = new JPanel();
box1.setLayout(new BoxLayout(box1, BoxLayout.X_AXIS));
box1.add(dispo);
box1.add(Box.createHorizontalStrut(450));
box1.add(label3);
emp1.add(box1,BorderLayout.SOUTH);
listmateriel = new Vector();
for(int k =0 ;k <10 ; k++)
listmateriel .add(new JRadioButton(res.getString("nom"+k)));
......
ImageIcon newIcon = new ImageIcon(bi);
JLabel picture = new JLabel(newIcon);
picture.setBorder(BorderFactory.createEmptyBorder(0,450,0,0));
JPanel jplRadio = new JPanel();
jplRadio.setLayout(new GridLayout(0, 1));
for(int i = 0 ; i < listmateriel.size() ;i++)
{
jplRadio.add((Component) listmateriel.elementAt(i));
}
JPanel imagebox = new JPanel();
imagebox.setLayout(new BorderLayout());
JScrollPane scroll3 = new JScrollPane(jplRadio);
scroll3.setPreferredSize(new Dimension(200, 30));
imagebox.add(scroll3, BorderLayout.WEST);
imagebox.add(picture, BorderLayout.CENTER);
emp1.add(imagebox,BorderLayout.CENTER);
JPanel box3 = new JPanel();
box3.setLayout(new BoxLayout(box3, BoxLayout.X_AXIS));
JLabel title3 = new JLabel("Remplir les champs suivants:");
box3.add(title3);
box3.add(Box.createHorizontalStrut(650));
JPanel gridlayoutPanel2 = new JPanel();
........
JPanel util5 = new JPanel();
util5.setLayout(new GridLayout(0,1));
util5.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel resul2 = new JLabel("bsfgbsdg");
JScrollPane scroll2 = new JScrollPane();
util5.add(resul2);
emprunt.add(emp1);
emprunt.add(box3);
emprunt.add(gridlayoutPanel2);
emprunt.add(util5);
menu.add("emprunts",emprunt);
//---------------------
getContentPane().add(menu);
pack();
setVisible(true);
}
}
GridLayout(0, 1) instead of BoxLayout is the solution thank you #trashgod

adding component to the frame using borderlayout

how to get the same look using using layout
package item;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* #author isslam
*/
public class MyFrameMain extends JFrame{
Equipment newq = new Equipment();
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
public MyFrameMain(String title){
super(title);
setTitle(title);
setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);
iJTextField = new JTextField();
nJTextField = new JTextField();
swTextField = new JTextField();
hwTextField = new JTextField();
nLabel = new JLabel("ID: ");
iLabel = new JLabel("Name: ");
jItemCounter = new JLabel("Number of current Item");
reSoulte = new JTextArea(15,20);
reSoulte.setEditable(false);
addButton = new JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");
JRadioButton rButton1 = new JRadioButton("SW Version",false);
JRadioButton rButton2 = new JRadioButton("HW Type",false);
JRadioButton rButton3 = new JRadioButton("General",true);
ButtonGroup BGroup = new ButtonGroup();
BGroup.add(rButton1);
BGroup.add(rButton2);
BGroup.add(rButton3);
JPanel panel = new JPanel(new GridLayout(5,1));
panel.add(nLabel);
panel.add(iLabel);
panel.add(rButton1);
panel.add(rButton2);
panel.add(rButton3);
JPanel bpanel = new JPanel(new GridLayout(2,2));
bpanel.add(addButton);
bpanel.add(showButton);
bpanel.add(copyButton);
bpanel.add(exitButton);
JPanel jtfPanel = new JPanel(new GridLayout(5,1));
jtfPanel.add(iJTextField);
jtfPanel.add(nJTextField);
jtfPanel.add(swTextField);
jtfPanel.add(hwTextField);
jtfPanel.add(jItemCounter);
JPanel jtaPanel = new JPanel(new GridLayout(5,1));
jtaPanel.add(reSoulte);
Container pane = getContentPane();
pane.setLayout(null);
pane.add(panel);
pane.add(bpanel);
pane.add(jtfPanel);
pane.add(jtaPanel);
Insets insets = pane.getInsets();
setSize(500 + insets.left + insets.right,500 + insets.top + insets.bottom);
Dimension size = panel.getPreferredSize();
panel.setBounds(0 + insets.left, 18 + insets.top,size.width, size.height);
size = bpanel.getPreferredSize();
bpanel.setBounds(0 + insets.left, 409 + insets.top,115+size.width, size.height);
size = jtfPanel.getPreferredSize();
jtfPanel.setBounds(180 + insets.left, 25 + insets.top,170 +size.width, size.height);
size = jtaPanel.getPreferredSize();
jtaPanel.setBounds(0 + insets.left, 150 + insets.top,265 +size.width, size.height);
exitButton.addActionListener(new ButtonWatcher());
}
private class ButtonWatcher implements ActionListener{
public void actionPerformed(ActionEvent a){
System.exit(0);
}
}
}
can you tell me how to replace absolute position to be borderlayout
The way I see your code:
"panel" is displayed in the top left
"jtfPanel" is displayed in the top right
"jtaPanel" is displayed in the center
"bPanel" is displayed in the bottom
BorderLayout has 3 vertical areas to work with NORTH, CENTER and SOUTH so I would suggest you combine "panel" and "jtfPanel" into a another panel, then you will have 3 panels to add to the BorderLayout. Something like:
JPanel north = new JPanel( new BorderLayout() );
north.add(panel, BorderLayout.WEST);
north.add(jtfPanel, BorderLayout.CENTER);
Container contentPane = getContentPane();
contentPane.add(north, BorderLayout.NORTH);
contentPane.add(jtaPanel, BorderLayout.CENTER);
contentPane.add(bPanel, BorderLayout.SOUTH);
The main point is that you can nest multiple panels to achieve your desired effect.
Here check this out. I added a main method to run it. I used EmptyBorders for needed spacing. I also grouped panels together which makes layouts easier to work with.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;
/**
*
* #author isslam
*/
public class MyFrameMain extends JFrame {
// Equipment newq = new Equipment();
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
public MyFrameMain(String title) {
//super(title);
setTitle(title);
setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);
iJTextField = new JTextField();
nJTextField = new JTextField();
swTextField = new JTextField();
hwTextField = new JTextField();
nLabel = new JLabel("ID: ");
iLabel = new JLabel("Name: ");
jItemCounter = new JLabel("Number of current Item");
reSoulte = new JTextArea(15, 45);
reSoulte.setEditable(false);
addButton = new JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");
JRadioButton rButton1 = new JRadioButton("SW Version", false);
JRadioButton rButton2 = new JRadioButton("HW Type", false);
JRadioButton rButton3 = new JRadioButton("General", true);
ButtonGroup BGroup = new ButtonGroup();
BGroup.add(rButton1);
BGroup.add(rButton2);
BGroup.add(rButton3);
JPanel panel = new JPanel(new GridLayout(5, 1));
panel.setBorder(new EmptyBorder(0, 0, 0, 75));
panel.add(nLabel);
panel.add(iLabel);
panel.add(rButton1);
panel.add(rButton2);
panel.add(rButton3);
JPanel bpanel = new JPanel(new GridLayout(2, 2));
bpanel.setBorder(new EmptyBorder(15, 0, 0, 0));
bpanel.add(addButton);
bpanel.add(showButton);
bpanel.add(copyButton);
bpanel.add(exitButton);
JPanel jtfPanel = new JPanel(new GridLayout(5, 1));
jtfPanel.add(iJTextField);
jtfPanel.add(nJTextField);
jtfPanel.add(swTextField);
jtfPanel.add(hwTextField);
jtfPanel.add(jItemCounter);
//JPanel jtaPanel = new JPanel(new GridLayout(5, 1));
//jtaPanel.add(reSoulte);
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.setBorder(new EmptyBorder(0, 0, 20, 0));
topPanel.add(panel, BorderLayout.WEST);
topPanel.add(jtfPanel, BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setBorder(new EmptyBorder(20, 10, 10, 10));
mainPanel.add(bpanel, BorderLayout.SOUTH);
mainPanel.add(reSoulte, BorderLayout.CENTER);
mainPanel.add(topPanel, BorderLayout.NORTH);
Container pane = getContentPane();
//pane.setLayout(null);
//pane.add(panel);
pane.add(mainPanel);
//pane.add(jtfPanel);
//Insets insets = pane.getInsets();
//setSize(500 + insets.left + insets.right, 500 + insets.top + insets.bottom);
//Dimension size = panel.getPreferredSize();
//panel.setBounds(0 + insets.left, 18 + insets.top, size.width, size.height);
//size = bpanel.getPreferredSize();
// bpanel.setBounds(0 + insets.left, 409 + insets.top, 115 + size.width, size.height);
// size = jtfPanel.getPreferredSize();
// jtfPanel.setBounds(180 + insets.left, 25 + insets.top, 170 + size.width, size.height);
//size = jtaPanel.getPreferredSize();
//jtaPanel.setBounds(0 + insets.left, 150 + insets.top, 265 + size.width, size.height);
exitButton.addActionListener(new ButtonWatcher());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
MyFrameMain frame = new MyFrameMain("Title");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
//frame.setSize(500, 500);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
private class ButtonWatcher implements ActionListener {
public void actionPerformed(ActionEvent a) {
System.exit(0);
}
}
}

Categories