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.
Related
I am trying to create a register form for my application(school project), I wanted to set the layout to BoxLayout but the Jtextfields and combo box is having issue as you can see below, does this issue relates to setSize() or is it something I am doing incorrect,I just want the Jtextfields sorts vertically , I appreciate the support
private JPanel SetUpRegister() {
JLabel registerLabel = new JLabel("Registera");
registerLabel.setFont(new Font("Arial", Font.BOLD, 30));
loginRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
passwordRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
fnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
lnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
ageRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
String[] genderlist = new String[] { "Male", "Female", "Other" };
JComboBox<String> registerList = new JComboBox<>(genderlist);
JPanel registerPanel = new JPanel();
registerPanel.setBackground(new Color(255, 140, 0));
registerPanel.add(registerLabel);
registerPanel.add(loginRegisterInput);
registerPanel.add(passwordRegisterInput);
registerPanel.add(fnRegisterInput);
registerPanel.add(lnRegisterInput);
registerPanel.add(ageRegisterInput);
registerPanel.add(registerList);
registerPanel.setLayout(new BoxLayout(registerPanel,BoxLayout.Y_AXIS));
return registerPanel;
}
The input fields are huge
The BoxLayout will attempt to resize components when extra space is available on the panel. It will resize a component up to its maximum size.
For some reason the maximum height of a JTextField is Integer.MAX_VALUE which makes no sense to me, since the height of the text never changes as you enter more text.
In any case you have a couple of choices:
Use a different layout manager, like the GridBagLayout. The GridBagLayout, will respect the preferred size of the text fields.
Create a custom JTestField and override the getMaximumSize() method to return the preferred height of the component
Use a wrapper panel.
For the wrapper panel you could do:
JPanel wrapper = new JPanel( new BorderLayout() );
wrapper.add(registerPanel, BorderLayout.PAGE_START);
return wrapper;
//return registerPanel;
The BorderLayout will respect the preferred height of any component added to the PAGE_START, so there is no need for the BoxLayout to resize any component.
private JPanel SetUpRegister() {
JLabel registerLabel = new JLabel("Registera");
JLabel registerLabel1 = new JLabel("Login :");
JLabel registerLabel2 = new JLabel("Password :");
JLabel registerLabel3 = new JLabel("First Name :");
JLabel registerLabel4 = new JLabel("Last Name :");
JLabel registerLabel5 = new JLabel("Age :");
JLabel registerLabel6 = new JLabel("Gender :");
JLabel registerLabel7 = new JLabel("Bio :");
registerLabel.setFont(new Font("Arial", Font.BOLD, 30));
JButton createAccButton = new JButton("Create");
createAccButton.addActionListener(new CreateAccountListener());
loginRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
passwordRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
fnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
lnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
ageRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
bioRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
String[] genderlist = new String[] { "Male", "Female", "Other" };
registerList = new JComboBox(genderlist);
JPanel registerPanel = new JPanel();
registerPanel.setLayout(new BoxLayout(registerPanel, BoxLayout.Y_AXIS));
registerPanel.add(registerLabel);
JPanel registerLabPanel = new JPanel();
registerLabPanel.setLayout(new FlowLayout());
registerLabPanel.add(registerLabel);
JPanel usernamePanel = new JPanel();
usernamePanel.setLayout(new FlowLayout());
usernamePanel.add(registerLabel1);
usernamePanel.add(loginRegisterInput);
JPanel passwordPanel = new JPanel();
passwordPanel.setLayout(new FlowLayout());
passwordPanel.add(registerLabel2);
passwordPanel.add(passwordRegisterInput);
JPanel fnPanel = new JPanel();
fnPanel.setLayout(new FlowLayout());
fnPanel.add(registerLabel3);
fnPanel.add(fnRegisterInput);
JPanel lnPanel = new JPanel();
lnPanel.setLayout(new FlowLayout());
lnPanel.add(registerLabel4);
lnPanel.add(lnRegisterInput);
JPanel agePanel = new JPanel();
agePanel.setLayout(new FlowLayout());
agePanel.add(registerLabel5);
agePanel.add(ageRegisterInput);
JPanel genderPanel = new JPanel();
genderPanel.setLayout(new FlowLayout());
genderPanel.add(registerLabel6);
genderPanel.add(registerList);
JPanel bioPanel = new JPanel();
bioPanel.setLayout(new FlowLayout());
bioPanel.add(registerLabel7);
bioPanel.add(bioRegisterInput);
JPanel buttonLoginPanel = new JPanel();
buttonLoginPanel.setLayout(new FlowLayout());
buttonLoginPanel.add(createAccButton);
registerPanel.add(registerLabel);
registerPanel.add(usernamePanel);
registerPanel.add(passwordPanel);
registerPanel.add(fnPanel);
registerPanel.add(lnPanel);
registerPanel.add(agePanel);
registerPanel.add(genderPanel);
registerPanel.add(bioPanel);
registerPanel.add(buttonLoginPanel);
return registerPanel;
}
I fixed the issue by making a Panel for each input and label
I'm trying to left align the class and neutral buttons so they are in line with the left most card button. For some reason, setAlignmentX only shifts the buttons half way. Here is the code. Is there away to align the buttons?
private String[] listEntries = {"a","a","a","a","a"};
private JButton remove = new JButton("Remove");
private JList list;
private JButton b1 = new JButton("Class");
private JButton b2 = new JButton("Neutral");
private JPanel page = new JPanel(new CardLayout());
private DefaultListModel listModel = new DefaultListModel();
public Main () {
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.PAGE_AXIS));
leftPanel.setLayout(new BoxLayout(leftPanel,BoxLayout.PAGE_AXIS));
rightPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
leftPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel title = new JLabel("Deck Constructor", SwingConstants.CENTER);
title.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
//Set up Deck List
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(-1);
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(150, 80));
JLabel listTitle = new JLabel("List");
listTitle.setLabelFor(list);
listScroller.setAlignmentX(LEFT_ALIGNMENT);
rightPanel.add(listTitle);
rightPanel.add(listScroller);
rightPanel.add(remove);
//Set up Card Selection
JPanel buttonPanel = new JPanel();
buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
b1.setActionCommand("Class");
b2.setActionCommand("Neutral");
b1.addActionListener(this);
b2.addActionListener(this);
buttonPanel.add(b1);
buttonPanel.add(b2);
JPanel classCards = new JPanel(new GridLayout(2,3, 10, 10));
JButton card1 = new JButton("Card 1");
card1.addActionListener(this);
card1.setActionCommand("addCard");
JButton card2 = new JButton("Card 2");
JButton card3 = new JButton("Card 3");
JButton card4 = new JButton("Card 4");
JButton card5 = new JButton("Card 5");
JButton card6 = new JButton("Card 6");
classCards.add(card1);
classCards.add(card2);
classCards.add(card3);
classCards.add(card4);
classCards.add(card5);
classCards.add(card6);
JPanel neutral = new JPanel();
neutral.setBackground(Color.BLUE);
page.add(classCards, "Class");
page.add(neutral, "Neutral");
leftPanel.add(buttonPanel);
leftPanel.add(page);
setPreferredSize(new Dimension(640,640/12*9));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(leftPanel,BorderLayout.CENTER);
getContentPane().add(rightPanel,BorderLayout.EAST);
getContentPane().add(title,BorderLayout.NORTH);
pack();
setVisible(true);
}
It is not perfect solution, but you can use for example:
If you want to keep buttons default size:
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING,1,2));
and delete:
buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
If you want to fill buttonPanel with buttons:
JPanel buttonPanel = new JPanel(new GridLayout(1,2,2,2));
buttonPanel.setBorder(new EmptyBorder(2,1,2,1));
In (FlowLayout.LEADING,1,2) and in EmptyBorder(2,1,2,1))the 1,2 values are added to match buttonPanel and classCard hgap and vgap.
I am new to java. I am trying to implement scroll feature to my Jpanel but the schoolpane is not visible. I tried to many methods but nothing works.
public class test extends ContentPanel {
JPanel secondary;
JPanel customerType;
JPanel primary;
JPanel labelPanel;
JLabel lCustNo;
JLabel lCustName;
JLabel lTelNo;
JLabel lAddress;
JLabel lNationality;
JLabel lResident;
JLabel lVisitor;
JLabel lCustomerType;
JLabel lIdCard;
JLabel lBankName;
JLabel lPassportNo;
JLabel lVisitStart;
JLabel lVisitEnd;
JTextField tCustNo;
JTextField tCustName;
JTextField tTelNo;
JTextField tAddress;
JTextField tNationality;
JTextField tIdCard;
JTextField tBankName;
JTextField tPassportNo;
JTextField tVisitStart;
JTextField tVisitEnd;
JPanel radioButton;
JRadioButton rResident;
JRadioButton rVisitor;
ButtonGroup custType;
JScrollPane scrollBar;
JSeparator s1;
public test (String title, JPanel parent) {
super(title, parent);
secondary = new JPanel(new GridLayout(2,0));
secondary.setBounds(10, 10, 500, 800);
labelPanel = new JPanel(new GridLayout(5,2,300,20));
radioButton = new JPanel(new GridLayout(1, 4));
customerType = new JPanel(new GridLayout(3,4, 30, 20));
primary = new JPanel(new BorderLayout());
lCustNo = new JLabel("Customer No: ");
lCustName = new JLabel("Name: ");
lTelNo = new JLabel("Telephone Number: ");
lAddress = new JLabel("Address: ");
lNationality = new JLabel("Nationality: ");
lResident = new JLabel("Resident");
lVisitor = new JLabel("Visitor");
lCustomerType = new JLabel("Customer Type");
lIdCard = new JLabel("Id Card");
lBankName = new JLabel("Bank Name");
lPassportNo = new JLabel("Passport Number");
lVisitStart = new JLabel("Visit Start");
lVisitEnd = new JLabel("Visit End");
tCustNo = new JTextField(10);
tCustName = new JTextField(10);
tTelNo = new JTextField(10);
tAddress = new JTextField(10);
tNationality = new JTextField(10);
tIdCard = new JTextField(10);
tBankName = new JTextField(10);
tPassportNo = new JTextField(10);
tVisitStart = new JTextField(10);
tVisitEnd = new JTextField(10);
rResident = new JRadioButton();
rVisitor = new JRadioButton();
custType = new ButtonGroup();
customerType.add(lPassportNo);
customerType.add(tPassportNo);
customerType.add(lVisitStart);
customerType.add(tVisitStart);
customerType.add(lIdCard);
customerType.add(tIdCard);
customerType.add(lBankName);
customerType.add(tBankName);
customerType.add(lVisitEnd);
customerType.add(tVisitEnd);
custType.add(rResident);
custType.add(rVisitor);
radioButton.add(lVisitor);
radioButton.add(rVisitor);
radioButton.add(lResident);
radioButton.add(rResident);
labelPanel.add(lCustNo);
labelPanel.add(tCustNo);
labelPanel.add(lCustName);
labelPanel.add(tCustName);
labelPanel.add(lTelNo);
labelPanel.add(tTelNo);
labelPanel.add(lAddress);
labelPanel.add(tAddress);
labelPanel.add(lNationality);
labelPanel.add(tNationality);
secondary.add(customerType);
secondary.add(labelPanel);;
primary.add(secondary,BorderLayout.CENTER);
scrollBar= new JScrollPane(primary);
scrollBar.setBounds(10, 10, 400, 555);
this.add(scrollBar);
}
}
My code is above. This class extends contentPanel and contentPanel extends JPanel. Someone please help me.
Thanks.
Your scrollBar is not visible because it is not necessary. If you change JScrollPane settings, for example:
scrollBar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
you will see you have it, but not use it. It is because your scrollBar doesn't have a content which expands dynamically (like JTextArea for example). Your scrollBar has JPanel inside, which has size to fit all its components, so it will not be enough to add some text inside text components.
To make scroll bars visible, you can change a size of JSrollPane. However setBound() will not work if you JPanel use default FlowLayout. To use absolute positioning in JPanel you need to use null layout, what is generally not recommended and is seen as bad programming practice.
Try to delete setBound() lines and use for example scrollBar.setPreferredSize(new Dimension(400,555)); instead. You should see a difference.
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);
}
}
I have a question regarding how to align my components on my GUI.
Referring to the screen capture above, I want the checkboxes to be aligned with each other, but here I'm getting that one checkbox (LC Proxy) which is out of line, due to there being an additional combo box on the same line.
Can anyone suggest the necessary changes I should make to achieve what I want? (Other suggestions on how to improve the code/display are also welcome!)
(P/S Sorry if the title is misleading; not sure how else I can name it)
Here's the code for my GUI:
/**
* Builds the elements for the GUI
*/
private void build() {
// define properties
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(100, 100);
this.setSize(360, 250);
this.setResizable(false);
// build elements
movFileField = new JTextField(30);
movFileField.setEditable(false);
JLabel textFieldLabel = new JLabel("MOV file: ");
textFieldLabel.setLabelFor(movFileField);
JPanel filenamePane = new JPanel();
filenamePane.setLayout(new FlowLayout());
filenamePane.add(textFieldLabel);
filenamePane.add(movFileField);
JCheckBox checkIPV = new JCheckBox("IPV Proxy");
checkIPV.setSelected(configMap.get(EpoxyConfigField.IPV_PROXY.name()).isEnabled());
toggleMap.put(EpoxyConfigField.IPV_PROXY, checkIPV);
flickComps.add(checkIPV);
JCheckBox checkITX = new JCheckBox("ITX Proxy");
checkITX.setSelected(configMap.get(EpoxyConfigField.ITX_PROXY.name()).isEnabled());
toggleMap.put(EpoxyConfigField.ITX_PROXY, checkITX);
flickComps.add(checkITX);
JCheckBox checkLC = new JCheckBox("LC Proxy");
checkLC.setSelected(configMap.get(EpoxyConfigField.LC_PROXY.name()).isEnabled());
toggleMap.put(EpoxyConfigField.LC_PROXY, checkLC);
flickComps.add(checkLC);
JComboBox<String> comboLCProfile = new JComboBox<String>("A;C;D;Dxd;E".split(";"));
JCheckBox checkArchive = new JCheckBox("Archive Asset");
checkArchive.setSelected(configMap.get(EpoxyConfigField.ARCHIVE.name()).isEnabled());
toggleMap.put(EpoxyConfigField.ARCHIVE, checkArchive);
flickComps.add(checkArchive);
JPanel checkboxPane = new JPanel();
checkboxPane.setLayout(new BoxLayout(checkboxPane, BoxLayout.PAGE_AXIS));
checkboxPane.add(checkIPV);
checkboxPane.add(checkITX);
// This is the part I'm having problems with
JPanel lcproxyPane = new JPanel();
lcproxyPane.add(checkLC);
lcproxyPane.add(comboLCProfile);
checkboxPane.add(lcproxyPane);
checkboxPane.add(checkArchive);
checkboxPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
JButton processButton = new JButton("Process");
processButton.setActionCommand(COMMAND_PROCESS);
processButton.addActionListener(this);
flickComps.add(processButton);
JButton selectButton = new JButton("Select");
selectButton.setActionCommand(COMMAND_SELECT);
selectButton.addActionListener(this);
flickComps.add(selectButton);
statusField = new JTextArea(10,20);
statusField.setEditable(false);
statusField.setLineWrap(true);
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.add(Box.createRigidArea(new Dimension(50, 0)));
buttonPane.add(processButton);
buttonPane.add(Box.createHorizontalGlue());
buttonPane.add(selectButton);
buttonPane.add(Box.createRigidArea(new Dimension(50, 0)));
buttonPane.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
JPanel bottomPane = new JPanel();
bottomPane.setLayout(new BoxLayout(bottomPane, BoxLayout.PAGE_AXIS));
bottomPane.add(buttonPane);
bottomPane.add(new JScrollPane(statusField));
Container contentPane = this.getContentPane();
contentPane.add(filenamePane, BorderLayout.PAGE_START);
contentPane.add(checkboxPane, BorderLayout.CENTER);
contentPane.add(bottomPane, BorderLayout.PAGE_END);
this.pack();
}
The reason why you cannot align your components is because you used a layout which makes very difficult to align those components.
I suggest you to use GridBagLayout rather than FlowLayout.
Example:
JPanel checkboxPane = new JPanel();
GridBagLayout gbl_checkboxPane = new GridBagLayout();
gbl_checkboxPane.columnWidths = new int[]{430, 0, 0};
gbl_checkboxPane.rowHeights = new int[]{27, 27, 27, 27};
gbl_checkboxPane.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gbl_checkboxPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
checkboxPane.setLayout(gbl_checkboxPane);
JCheckBox checkIPV = new JCheckBox("IPV Proxy");
GridBagConstraints gbc_checkIPV = new GridBagConstraints();
gbc_checkIPV.anchor = GridBagConstraints.WEST;
gbc_checkIPV.fill = GridBagConstraints.VERTICAL;
gbc_checkIPV.insets = new Insets(0, 0, 5, 5);
gbc_checkIPV.gridx = 0;
gbc_checkIPV.gridy = 0;
checkboxPane.add(checkIPV, gbc_checkIPV);