JTextField: How to validate number input in JTextField - java

How to validate a textfield to enter only 10 digits mobile number in Swing?
I have three text field.
For name
For Contact
No. For Email
I want to click on submit button then check name, contact and email right or wrong. I also want to set limit of character
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class UpDateProfile extends JFrame implements ActionListener
{
JLabel name_lbl,email_lbl,contact_lbl;
JTextField name_text,email_text,contact_text;
JButton submit_btn;
public UpDateProfile()
{
super("Velidation demo");
setSize(650,450);
setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2-325,(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2-225);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
setResizable(false);
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.insets = new Insets(5,3,5,3);
name_lbl = new JLabel("Name :");
gbc1.gridx = 0;
gbc1.gridy = 0;
gbc1.ipadx = 0;
gbc1.ipady = 0;
gbc1.gridheight = 1;
gbc1.gridwidth = 1;
gbc1.fill = GridBagConstraints.HORIZONTAL;
gbc1.anchor = GridBagConstraints.WEST;
add(name_lbl,gbc1);
name_text = new JTextField(30);
gbc1.gridx = 1;
gbc1.gridy = 0;
gbc1.ipadx = 0;
gbc1.ipady = 0;
gbc1.gridheight = 1;
gbc1.gridwidth = 3;
gbc1.fill = GridBagConstraints.HORIZONTAL;
gbc1.anchor = GridBagConstraints.WEST;
add(name_text,gbc1);
email_lbl = new JLabel("E-mail :");
gbc1.gridx = 0;
gbc1.gridy = 1;
gbc1.ipadx = 0;
gbc1.ipady = 0;
gbc1.gridheight = 1;
gbc1.gridwidth = 1;
gbc1.fill = GridBagConstraints.HORIZONTAL;
gbc1.anchor = GridBagConstraints.WEST;
add(email_lbl,gbc1);
alt_email_text = new JTextField(30);
gbc1.gridx = 1;
gbc1.gridy = 1;
gbc1.ipadx = 0;
gbc1.ipady = 0;
gbc1.gridheight = 1;
gbc1.gridwidth = 3;
gbc1.fill = GridBagConstraints.HORIZONTAL;
gbc1.anchor = GridBagConstraints.WEST;
add(alt_email_text,gbc1);
contact_lbl = new JLabel("Contact No. :");
gbc1.gridx = 0;
gbc1.gridy = 2;
gbc1.ipadx = 0;
gbc1.ipady = 0;
gbc1.gridheight = 1;
gbc1.gridwidth = 1;
gbc1.fill = GridBagConstraints.HORIZONTAL;
gbc1.anchor = GridBagConstraints.WEST;
add(contact_lbl,gbc1);
contact_text = new JTextField(10);
gbc1.gridx = 1;
gbc1.gridy = 2;
gbc1.ipadx = 0;
gbc1.ipady = 0;
gbc1.gridheight = 1;
gbc1.gridwidth = 1;
gbc1.fill = GridBagConstraints.HORIZONTAL;
gbc1.anchor = GridBagConstraints.WEST;
add(contact_text,gbc1);
submit_btn = new JButton("Submit");
submit_btn.addActionListener(this);
gbc1.gridx = 2;
gbc1.gridy = 7;
gbc1.ipadx = 10;
gbc1.ipady = 0;
gbc1.gridheight = 1;
gbc1.gridwidth = 2;
gbc1.anchor = GridBagConstraints.CENTER;
add(submit_btn,gbc1);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==submit_btn)
{
}
}
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==submit_btn)
{
String text = contact_text.getText();
if(text.matches("\\d{10}")){
// The text entered is a 10-digit number
}else{
// The text is not valid
}
}
}

Related

Positioning components in java GridBagLayout

I am relatively new to Java and I am making an app that helps me estimate construction costs. I built the backend for it, but the buttons and text fields were all in one row, making the app so wide it didn't fit in my screen. I am now trying to use a 3x6 grid bag layout to organize the components like this:
Title
question1 *answer*
question2 *answer*
question3 *answer*
question4 *answer*
--Calculate--
Estimate: $***
When I run the program, I am given an "illegal component position"
error (pasted at the bottom) and nothing pops up anymore since
I started adding in the grid constraints. The problem is not with the JButton or its action listener. Here is the code, sorry for
the lack of comments:
import java.util.*;
import packagepackage.HintTextFieldUI;
import java.awt.*;
import java.awt.event.*;
import java.security.PublicKey;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class CP_GUI extends JFrame {
public JLabel tLabel;
public JTextField linear;
public JLabel liLabel;
public JComboBox<String> sump;
public JLabel suLabel;
public JComboBox<String> elec;
public JLabel elLabel;
public JTextField prep;
public JLabel prLabel;
public JTextField estimate;
public JLabel esLabel;
public CP_GUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(200, 200, 100, 100));
GridBagConstraints c = new GridBagConstraints();
String title = "Drain Tile Calculator";
tLabel = new JLabel(title);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.ipady = 40;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 0;
panel.add(tLabel, c);
frame.setTitle(title);
liLabel = new JLabel("Basement Perimeter Length");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
panel.add(liLabel, c);
linear = new JTextField(10);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
panel.add(linear, c);
prLabel = new JLabel("Time needed to prepare site:");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
panel.add(prLabel, c);
prep = new JTextField(10);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 2;
panel.add(prep, c);
suLabel = new JLabel("Using:");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 0;
panel.add(suLabel, c);
String[] sumpo = {"New sump pump","Existing sump pump"};
sump = new JComboBox<>(sumpo);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 2;
panel.add(sump, c);
elLabel = new JLabel("Location of electrical outlet:");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 4;
c.gridy = 0;
panel.add(elLabel, c);
String[] electo = {"There is no outlet within 6 feet of the sump pump","There is an outlet nearby, or I do not need a new pump"};
elec = new JComboBox<>(electo);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 4;
c.gridy = 2;
panel.add(elec, c);
estimate = new JTextField(10);
JButton calculate = new JButton("Calculate");
calculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event){
// TODO Auto-generated method stub
Integer linVar = Integer.parseInt(linear.getText());
linVar *= 13;
Object sumps = sump.getSelectedItem();
Integer sumpVar = 0;
if("New sump pump".equals(sumps)) {
sumpVar += 260;}
Object elecs = elec.getSelectedItem();
Integer elecsVar = 0;
if("There is no outlet within 6 feet of the sump pump".equals(elecs)) {
elecsVar += 280;}
Integer prepsVar = Integer.parseInt(prep.getText());
prepsVar += 30;
prepsVar *= 235;
prepsVar /= 100;
linVar += sumpVar += elecsVar += prepsVar;
/* overhead*/
linVar += 2428;
/* tax */
linVar *= 11;
linVar /= 10;
/* margin */
linVar *= 12;
linVar /= 10;
String toWords = String.valueOf(linVar);
estimate.setUI(new HintTextFieldUI(toWords, true));
}
});
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.weightx = 0.5;
c.gridwidth = 3;
c.gridx = 5;
c.gridy = 0;
panel.add(calculate, c);
esLabel = new JLabel("Estimated Cost:");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridwidth = 2;
c.gridx = 6;
c.gridy = 0;
panel.add(esLabel, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 6;
c.gridy = 2;
panel.add(estimate, c);
frame.add(panel, GridBagConstraints.CENTER);
frame.pack();
frame.setVisible(true);
linear.setUI(new HintTextFieldUI("Perimeter length", true));
prep.setUI(new HintTextFieldUI("Minutes of preptime", true));
}
public static void main(String[] args) {
new CP_GUI();
}
}
I suspect the issue might either have to with the fact that the size
of the grid itself (3x6) is never defined, or with the frame's layout
not being properly set to grid bags layout.
Error:
Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
at java.desktop/java.awt.Container.addImpl(Container.java:1115)
at java.desktop/java.awt.Container.add(Container.java:1033)
at java.desktop/javax.swing.JFrame.addImpl(JFrame.java:554)
at java.desktop/java.awt.Container.add(Container.java:493)
at craftsmanPeak/packagepackage.CP_GUI.<init>(CP_GUI.java:163)
at craftsmanPeak/packagepackage.CP_GUI.main(CP_GUI.java:173)
Thanks so much in advance! I really appreciate it!
frame.add(panel, GridBagConstraints.CENTER); JFrame uses a BorderLayout by default, get rid of the GridBagConstraints.CENTER
I've also cleaned up you layout (trust me (said some stranger on the internet 🤣), it was a mess)
You're messing up your x and y positions
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public JLabel tLabel;
public JTextField linear;
public JLabel liLabel;
public JComboBox<String> sump;
public JLabel suLabel;
public JComboBox<String> elec;
public JLabel elLabel;
public JTextField prep;
public JLabel prLabel;
public JTextField estimate;
public JLabel esLabel;
public TestPane() {
setLayout(new GridBagLayout());
setBorder(BorderFactory.createEmptyBorder(16, 16, 16, 16));
GridBagConstraints c = new GridBagConstraints();
String title = "Drain Tile Calculator";
tLabel = new JLabel(title, JLabel.CENTER);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.ipady = 40;
c.gridwidth = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.CENTER;
c.gridx = 0;
c.gridy = 0;
add(tLabel, c);
c = new GridBagConstraints();
c.anchor = GridBagConstraints.LINE_END;
c.gridy = 1;
c.gridx = 0;
liLabel = new JLabel("Basement Perimeter Length:");
prLabel = new JLabel("Time needed to prepare site:");
suLabel = new JLabel("Using:");
elLabel = new JLabel("Location of electrical outlet:");
add(liLabel, c);
c.gridy++;
add(prLabel, c);
c.gridy++;
add(suLabel, c);
c.gridy++;
add(elLabel, c);
linear = new JTextField(10);
prep = new JTextField(10);
String[] sumpo = {"New sump pump", "Existing sump pump"};
sump = new JComboBox<>(sumpo);
String[] electo = {"There is no outlet within 6 feet of the sump pump", "There is an outlet nearby, or I do not need a new pump"};
elec = new JComboBox<>(electo);
c.anchor = GridBagConstraints.LINE_START;
c.gridx++;
c.gridy = 1;
add(linear, c);
c.gridy++;
add(prep, c);
c.gridy++;
add(sump, c);
c.gridy++;
add(elec, c);
JButton calculate = new JButton("Calculate");
//// calculate.addActionListener(new ActionListener() {
//// public void actionPerformed(ActionEvent event) {
//// // TODO Auto-generated method stub
//// Integer linVar = Integer.parseInt(linear.getText());
//// linVar *= 13;
////
//// Object sumps = sump.getSelectedItem();
//// Integer sumpVar = 0;
//// if ("New sump pump".equals(sumps)) {
//// sumpVar += 260;
//// }
////
//// Object elecs = elec.getSelectedItem();
//// Integer elecsVar = 0;
//// if ("There is no outlet within 6 feet of the sump pump".equals(elecs)) {
//// elecsVar += 280;
//// }
////
//// Integer prepsVar = Integer.parseInt(prep.getText());
//// prepsVar += 30;
//// prepsVar *= 235;
//// prepsVar /= 100;
////
//// linVar += sumpVar += elecsVar += prepsVar;
//// /* overhead*/
//// linVar += 2428;
//// /* tax */
//// linVar *= 11;
//// linVar /= 10;
//// /* margin */
//// linVar *= 12;
//// linVar /= 10;
//// String toWords = String.valueOf(linVar);
////
//// estimate.setUI(new HintTextFieldUI(toWords, true));
//// }
////
//// });
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(10, 0, 10, 0);
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = 0;
c.gridy = 5;
add(calculate, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 6;
c.anchor = GridBagConstraints.LINE_END;
esLabel = new JLabel("Estimated Cost:");
estimate = new JTextField(10);
add(esLabel, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
add(estimate, c);
// linear.setUI(new HintTextFieldUI("Perimeter length", true));
// prep.setUI(new HintTextFieldUI("Minutes of preptime", true));
}
}
}

NullPointerException when adding a column to JTable

I am writing a program that displays patient records when entering a practitioner number (FHIR HAPI), but that is besides the point. I want to display basic patient details, and then through a checkbox, allow the user to add an extra column to the JTable that displays their details.
This extra column should be able to be added and removed depending on the state of the checkbox. However, when I try to use my DefaultTableModel variable to perform the model.addColumn() method, I always get a NullPointer exception.
The very last method is the one creating issues. I also provided code to the other methods that are called. I have tried using various types of arrays (Object and String) for the values and even arraylists. You will also see that in my second piece of code for the method getAllPatientObservationValues() I performed a println to see if it goes into the method and it does not. Any help will be much appreciated.
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.text.ParseException;
public class GUIManager extends JFrame{
private Operations op;
private JPanel pnPnlMain;
private JPanel pnPnlTop;
private JCheckBox cbChkCholesterol;
private JPanel pnPnlMiddle;
private JTable tblLeft;
private DefaultTableModel tblLeftModel;
private JTable tblRight;
private DefaultTableModel tblRightModel;
private JButton btBtnAdd;
private JButton btBtnRemove;
private JPanel pnPnlBottom;
private JButton btBtnExit;
private JLabel lbLblTime;
private JTextField tfTxtSeconds;
private JLabel lbLblThreshhold;
private JTextField tfTxtThreshold;
public GUIManager(Operations inOP)
{
setSize(1000, 650);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("FHIR Patient Monitor");
pnPnlMain = new JPanel();
pnPnlMain.setBorder( BorderFactory.createTitledBorder( "FHIR" ) );
GridBagLayout gbPnlMain = new GridBagLayout();
GridBagConstraints gbcPnlMain = new GridBagConstraints();
pnPnlMain.setLayout( gbPnlMain );
pnPnlTop = new JPanel();
pnPnlTop.setBorder( BorderFactory.createTitledBorder( "Column Details" ) );
GridBagLayout gbPnlTop = new GridBagLayout();
GridBagConstraints gbcPnlTop = new GridBagConstraints();
pnPnlTop.setLayout( gbPnlTop );
cbChkCholesterol = new JCheckBox( "Cholesterol");
gbcPnlTop.gridx = 2;
gbcPnlTop.gridy = 1;
gbcPnlTop.gridwidth = 6;
gbcPnlTop.gridheight = 1;
gbcPnlTop.fill = GridBagConstraints.BOTH;
gbcPnlTop.weightx = 1;
gbcPnlTop.weighty = 0;
gbcPnlTop.anchor = GridBagConstraints.NORTH;
gbPnlTop.setConstraints( cbChkCholesterol, gbcPnlTop );
pnPnlTop.add( cbChkCholesterol );
gbcPnlMain.gridx = 0;
gbcPnlMain.gridy = 0;
gbcPnlMain.gridwidth = 20;
gbcPnlMain.gridheight = 4;
gbcPnlMain.fill = GridBagConstraints.BOTH;
gbcPnlMain.weightx = 1;
gbcPnlMain.weighty = 0;
gbcPnlMain.anchor = GridBagConstraints.NORTH;
gbPnlMain.setConstraints( pnPnlTop, gbcPnlMain );
pnPnlMain.add( pnPnlTop );
pnPnlMiddle = new JPanel();
pnPnlMiddle.setBorder( BorderFactory.createTitledBorder( "Tables" ) );
GridBagLayout gbPnlMiddle = new GridBagLayout();
GridBagConstraints gbcPnlMiddle = new GridBagConstraints();
pnPnlMiddle.setLayout( gbPnlMiddle );
String [][]dataTblLeft = new String[1][2];
String []colsTblLeft = new String[] { "ID", "Full Name" };
tblLeftModel = new DefaultTableModel(dataTblLeft, colsTblLeft);
tblLeft = new JTable(tblLeftModel);
tblLeftModel.removeRow(0);
JScrollPane scpLeftTable = new JScrollPane(tblLeft);
scpLeftTable.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scpLeftTable.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
gbcPnlMiddle.gridx = 0;
gbcPnlMiddle.gridy = 0;
gbcPnlMiddle.gridwidth = 9;
gbcPnlMiddle.gridheight = 12;
gbcPnlMiddle.fill = GridBagConstraints.NONE;
gbcPnlMiddle.weightx = 1;
gbcPnlMiddle.weighty = 1;
gbcPnlMiddle.anchor = GridBagConstraints.CENTER;
gbPnlMiddle.setConstraints( scpLeftTable, gbcPnlMiddle );
pnPnlMiddle.add(scpLeftTable);
String [][]dataTblRight = new String[1][7] ;
String []colsTblRight = new String[] { "ID", "Full Name", "Birthdate","Gender","City","State","Country"};
tblRightModel = new DefaultTableModel(dataTblRight, colsTblRight);
tblRight = new JTable(tblRightModel);
tblRight.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tblRightModel.removeRow(0);
JScrollPane scpRightTable = new JScrollPane(tblRight);
scpRightTable.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scpRightTable.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
gbcPnlMiddle.gridx = 10;
gbcPnlMiddle.gridy = 0;
gbcPnlMiddle.gridwidth = 9;
gbcPnlMiddle.gridheight = 12;
gbcPnlMiddle.fill = GridBagConstraints.NONE;
gbcPnlMiddle.weightx = 1;
gbcPnlMiddle.weighty = 1;
gbcPnlMiddle.anchor = GridBagConstraints.CENTER;
gbPnlMiddle.setConstraints( scpRightTable, gbcPnlMiddle );
pnPnlMiddle.add( scpRightTable );
btBtnAdd = new JButton( "Add" );
gbcPnlMiddle.gridx = 2;
gbcPnlMiddle.gridy = 12;
gbcPnlMiddle.gridwidth = 5;
gbcPnlMiddle.gridheight = 2;
gbcPnlMiddle.fill = GridBagConstraints.NONE;
gbcPnlMiddle.weightx = 1;
gbcPnlMiddle.weighty = 0;
gbcPnlMiddle.anchor = GridBagConstraints.NORTH;
gbPnlMiddle.setConstraints( btBtnAdd, gbcPnlMiddle );
pnPnlMiddle.add( btBtnAdd );
btBtnRemove = new JButton( "Remove" );
gbcPnlMiddle.gridx = 12;
gbcPnlMiddle.gridy = 12;
gbcPnlMiddle.gridwidth = 5;
gbcPnlMiddle.gridheight = 2;
gbcPnlMiddle.fill = GridBagConstraints.NONE;
gbcPnlMiddle.weightx = 1;
gbcPnlMiddle.weighty = 0;
gbcPnlMiddle.anchor = GridBagConstraints.NORTH;
gbPnlMiddle.setConstraints( btBtnRemove, gbcPnlMiddle );
pnPnlMiddle.add( btBtnRemove );
gbcPnlMain.gridx = 0;
gbcPnlMain.gridy = 4;
gbcPnlMain.gridwidth = 20;
gbcPnlMain.gridheight = 15;
gbcPnlMain.fill = GridBagConstraints.BOTH;
gbcPnlMain.weightx = 1;
gbcPnlMain.weighty = 0;
gbcPnlMain.anchor = GridBagConstraints.NORTH;
gbPnlMain.setConstraints( pnPnlMiddle, gbcPnlMain );
pnPnlMain.add( pnPnlMiddle );
pnPnlBottom = new JPanel();
GridBagLayout gbPnlBottom = new GridBagLayout();
GridBagConstraints gbcPnlBottom = new GridBagConstraints();
pnPnlBottom.setLayout( gbPnlBottom );
btBtnExit = new JButton( "Exit" );
gbcPnlBottom.gridx = 16;
gbcPnlBottom.gridy = 0;
gbcPnlBottom.gridwidth = 4;
gbcPnlBottom.gridheight = 2;
gbcPnlBottom.fill = GridBagConstraints.NONE;
gbcPnlBottom.weightx = 1;
gbcPnlBottom.weighty = 0;
gbcPnlBottom.anchor = GridBagConstraints.EAST;
gbPnlBottom.setConstraints( btBtnExit, gbcPnlBottom );
pnPnlBottom.add( btBtnExit );
lbLblTime = new JLabel( "Refresh Rate:" );
gbcPnlBottom.gridx = 0;
gbcPnlBottom.gridy = 0;
gbcPnlBottom.gridwidth = 6;
gbcPnlBottom.gridheight = 1;
gbcPnlBottom.fill = GridBagConstraints.BOTH;
gbcPnlBottom.weightx = 1;
gbcPnlBottom.weighty = 1;
gbcPnlBottom.anchor = GridBagConstraints.NORTH;
gbPnlBottom.setConstraints( lbLblTime, gbcPnlBottom );
pnPnlBottom.add( lbLblTime );
tfTxtSeconds = new JTextField( );
gbcPnlBottom.gridx = 6;
gbcPnlBottom.gridy = 0;
gbcPnlBottom.gridwidth = 8;
gbcPnlBottom.gridheight = 1;
gbcPnlBottom.fill = GridBagConstraints.BOTH;
gbcPnlBottom.weightx = 1;
gbcPnlBottom.weighty = 0;
gbcPnlBottom.anchor = GridBagConstraints.WEST;
gbPnlBottom.setConstraints( tfTxtSeconds, gbcPnlBottom );
pnPnlBottom.add( tfTxtSeconds );
lbLblThreshhold = new JLabel( "Threshold:" );
gbcPnlBottom.gridx = 0;
gbcPnlBottom.gridy = 1;
gbcPnlBottom.gridwidth = 6;
gbcPnlBottom.gridheight = 1;
gbcPnlBottom.fill = GridBagConstraints.BOTH;
gbcPnlBottom.weightx = 1;
gbcPnlBottom.weighty = 1;
gbcPnlBottom.anchor = GridBagConstraints.NORTH;
gbPnlBottom.setConstraints( lbLblThreshhold, gbcPnlBottom );
pnPnlBottom.add( lbLblThreshhold );
tfTxtThreshold = new JTextField( );
gbcPnlBottom.gridx = 6;
gbcPnlBottom.gridy = 1;
gbcPnlBottom.gridwidth = 8;
gbcPnlBottom.gridheight = 1;
gbcPnlBottom.fill = GridBagConstraints.BOTH;
gbcPnlBottom.weightx = 1;
gbcPnlBottom.weighty = 0;
gbcPnlBottom.anchor = GridBagConstraints.NORTH;
gbPnlBottom.setConstraints( tfTxtThreshold, gbcPnlBottom );
pnPnlBottom.add( tfTxtThreshold );
gbcPnlMain.gridx = 0;
gbcPnlMain.gridy = 19;
gbcPnlMain.gridwidth = 20;
gbcPnlMain.gridheight = 2;
gbcPnlMain.fill = GridBagConstraints.BOTH;
gbcPnlMain.weightx = 1;
gbcPnlMain.weighty = 0;
gbcPnlMain.anchor = GridBagConstraints.NORTH;
gbPnlMain.setConstraints( pnPnlBottom, gbcPnlMain );
pnPnlMain.add( pnPnlBottom );
getContentPane().add(pnPnlMain);
}
public void addExitListener(ActionListener listen)
{
btBtnExit.addActionListener(listen);
}
public void addAddListener(ActionListener listen)
{
btBtnAdd.addActionListener(listen);
}
public void addRemoveListener(ActionListener listen)
{
btBtnRemove.addActionListener(listen);
}
public void addCholesterolListener(ActionListener listen) { cbChkCholesterol.addActionListener(listen);}
public JCheckBox getChkCholesterol() { return cbChkCholesterol;}
public DefaultTableModel getRightTableModel()
{
return this.tblRightModel;
}
public DefaultTableModel getLeftTableModel()
{
return this.tblLeftModel;
}
public JTable getLeftTable()
{
return this.tblLeft;
}
public JTable getRightTable()
{
return this.tblRight;
}
public void addRowToRightTable(Object[] newData)
{
tblRightModel.insertRow(tblRight.getRowCount(), newData);
}
public void populateLeftTable(String[][] data)
{
for(int i = 0; i < data.length; i++)
{
Object[] tempData = data[i];
tblLeftModel.insertRow(tblLeft.getRowCount(), tempData);
}
}
public void addColumnToRightTable(String code) throws ParseException {
tblRightModel.addColumn("Cholesterol",op.getAllPatientObservationValues(code));
}
}
public Object[] getAllPatientObservationValues(String code) throws ParseException {
System.out.println("In this method");
ArrayList<String> values = new ArrayList<>();
for (int i = 0; i < allPatients.size(); i++)
{
values.add(allPatients.get(i).getObservationValue(code));
}
return values.toArray();
}
Update: I saw that when I create my table I have a 2D array for the data set as one row 7 columns and thought that was the problem, but when I increased the size of it to say 8 or left it blank, then it still threw an error
It seems like you've forgotten to set the Operations object op to inOP.
This:
public GUIManager(Operations inOP)
{
setSize(1000, 650);
Should be this:
public GUIManager(Operations inOP)
{
op = inOP;
setSize(1000, 650);
I'm not sure what you were planning to do with inOP and op (deep copy of inOP?), but right now op is null. That's probably where your NullPointerException is coming from, in the line:
tblRightModel.addColumn("Cholesterol",op.getAllPatientObservationValues(code));

Does GridBagLayout affect JLabel images?

Currently trying to add a header image to my JFrame based GUI, I've developed the layout of the project and everything looks good, but every time I run the project the image does not load (no error messages).
My code (partial):
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class DeltaFlightFrame extends JFrame implements ActionListener, ChangeListener{
//<SNIPPED CODE FOR EASE OF VIEWING>
//Labels for inputs
//<SNIPPED CODE FOR EASE OF VIEWING>
private JLabel deltaLogo;
//icon
private Icon logo;
//<SNIPPED CODE FOR EASE OF VIEWING>
DeltaFlightFrame() {
GridBagConstraints layoutConst = null;
//<SNIPPED CODE FOR EASE OF VIEWING>
setTitle("Delta Flight Price Estimator");
//<SNIPPED CODE FOR EASE OF VIEWING>
//initialize delta logo
logo = new ImageIcon("../img/logo.png");
deltaLogo = new JLabel(logo);
System.out.println("Height" + logo.getIconHeight());
System.out.println("Width" + logo.getIconWidth());
//<SNIPPED CODE FOR EASE OF VIEWING>
// Create frame and add components using GridBagLayout
setLayout(new GridBagLayout());
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 1, 1);
//layoutConst.anchor = GridBagConstraints.LINE_START;
layoutConst.gridx = 0;
layoutConst.gridy = 0;
//layoutConst.gridwidth = 4;
add(deltaLogo, layoutConst);
//<SNIPPED CODE FOR EASE OF VIEWING>
}
//TODO: this
public void stateChanged(ChangeEvent event) {
}
//TODO, also: this
public void actionPerformed(ActionEvent event) {
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception ignored){}
DeltaFlightFrame myFrame = new DeltaFlightFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
}
}
My full code (I didn't want to put it all, it's a disgusting mess and I'm a first year student so this isn't... nice... hahaha):
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class DeltaFlightFrame extends JFrame implements ActionListener, ChangeListener{
//Text Fields
private JTextField desCityField; // Holds destination city abbreviation,
private JTextField depCityField; // Holds departure city abbreviation.
private JTextField finalOutputField; //Shows final price
//Drop Down
//=====TODO
//Labels for inputs
private JLabel desCityLabel;
private JLabel depCityLabel;
private JLabel tripTypeLabel;
private JLabel seatTypeLabel;
private JLabel adultTravelerCountLabel;
private JLabel childTravelerCountLabel;
private JLabel finalOutput; //final price
private JLabel flightTitle;
private JLabel passengerTitle;
private JLabel deltaLogo;
//icon
private Icon logo;
//dropdown
String[] seatClassChoices = { "Basic Economy", "Business+ Comfort", "First Class" };
private JComboBox seatClassDrop;
//JSpinners
private JSpinner adultTravelerCount;
private JSpinner childTravelerCount;
//radio button
private JRadioButton oneWay;
private JRadioButton roundTrip;
//Buttons
private JButton calculateButton;
/* Constructor creates GUI components and adds GUI components
using a GridBagLayout. */
DeltaFlightFrame() {
GridBagConstraints layoutConst = null;
SpinnerNumberModel spinnerModelAdult = null;
SpinnerNumberModel spinnerModelChild = null;
String desInit = "ATL";
String depInit = "JFK";
double priceInit = 150.00;
int passCountMin = 0;
int passCountMax = 9;
int passAdultInit = 1;
int passChildInit = 0;
//Set Frame Title
setTitle("Delta Flight Price Estimator");
//create labels
depCityLabel = new JLabel("Departure City: ");
desCityLabel = new JLabel("Destination City: ");
tripTypeLabel = new JLabel("Trip Type: ");
seatTypeLabel = new JLabel("Seat Class: ");
adultTravelerCountLabel = new JLabel("Travelling Adults: ");
childTravelerCountLabel = new JLabel("Travelling Children: ");
finalOutput = new JLabel("Price: ");
flightTitle = new JLabel("Flight Information");
flightTitle.setFont(new Font("Sans-Serif", Font.BOLD, 20));
passengerTitle = new JLabel("Passenger Information");
passengerTitle.setFont(new Font("Sans-Serif", Font.BOLD, 20));
//create dropdown
seatClassDrop = new JComboBox<String>(seatClassChoices);
//create spinners
spinnerModelAdult = new SpinnerNumberModel(passAdultInit, passCountMin, passCountMax, 1);
adultTravelerCount = new JSpinner(spinnerModelAdult);
spinnerModelChild = new SpinnerNumberModel(passChildInit, passCountMin, passCountMax, 1);
childTravelerCount = new JSpinner(spinnerModelChild);
//initialize delta logo
logo = new ImageIcon("../img/logo.png");
deltaLogo = new JLabel(logo);
System.out.println("Height" + logo.getIconHeight());
System.out.println("Width" + logo.getIconWidth());
//initialize text fields
desCityField = new JTextField("JFK");
desCityField.setEditable(true);
desCityField.setDocument(new LengthRestrictedDocument(3));
desCityField.setColumns(3);
depCityField = new JTextField("ATL");
depCityField.setEditable(true);
depCityField.setDocument(new LengthRestrictedDocument(3));
depCityField.setColumns(3);
//radio button
oneWay = new JRadioButton("One Way");
roundTrip = new JRadioButton("Round Trip");
//button
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
ButtonGroup tripType = new ButtonGroup();
tripType.add(oneWay);
tripType.add(roundTrip);
// Create frame and add components using GridBagLayout
setLayout(new GridBagLayout());
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 1, 1);
//layoutConst.anchor = GridBagConstraints.LINE_START;
layoutConst.gridx = 0;
layoutConst.gridy = 0;
//layoutConst.gridwidth = 4;
add(deltaLogo, layoutConst);
setLayout(new GridBagLayout());
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 1, 1);
layoutConst.anchor = GridBagConstraints.LINE_START;
layoutConst.gridx = 0;
layoutConst.gridy = 1;
layoutConst.gridwidth = 4;
add(flightTitle, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 1, 1);
layoutConst.anchor = GridBagConstraints.LINE_START;
layoutConst.gridx = 0;
layoutConst.gridy = 2;
layoutConst.gridwidth = 1;
add(desCityLabel, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 1, 1);
layoutConst.anchor = GridBagConstraints.LINE_START;
layoutConst.gridx = 2;
layoutConst.gridy = 2;
layoutConst.gridwidth = 1;
add(depCityLabel, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 1, 10);
layoutConst.fill = GridBagConstraints.HORIZONTAL;
layoutConst.gridx = 1;
layoutConst.gridy = 2;
layoutConst.gridwidth = 1;
add(desCityField, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 1, 10);
layoutConst.fill = GridBagConstraints.HORIZONTAL;
layoutConst.gridx = 3;
layoutConst.gridy = 2;
layoutConst.gridwidth = 1;
add(depCityField, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 1, 1);
layoutConst.fill = GridBagConstraints.HORIZONTAL;
layoutConst.gridx = 0;
layoutConst.gridy = 4;
layoutConst.gridwidth = 1;
add(roundTrip, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 1, 10);
layoutConst.fill = GridBagConstraints.HORIZONTAL;
layoutConst.gridx = 2;
layoutConst.gridy = 4;
layoutConst.gridwidth = 2;
add(seatClassDrop, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 1, 1);
layoutConst.fill = GridBagConstraints.HORIZONTAL;
layoutConst.gridx = 0;
layoutConst.gridy = 5;
layoutConst.gridwidth = 1;
add(oneWay, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(50, 10, 1, 1);
layoutConst.anchor = GridBagConstraints.LINE_START;
layoutConst.gridx = 0;
layoutConst.gridy = 6;
layoutConst.gridwidth = 4;
add(passengerTitle, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 10, 1);
layoutConst.fill = GridBagConstraints.LINE_START;
layoutConst.gridx = 0;
layoutConst.gridy = 7;
layoutConst.gridwidth = 1;
add(adultTravelerCountLabel, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 10, 10);
layoutConst.fill = GridBagConstraints.HORIZONTAL;
layoutConst.gridx = 1;
layoutConst.gridy = 7;
layoutConst.gridwidth = 1;
add(adultTravelerCount, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 10, 10);
layoutConst.fill = GridBagConstraints.LINE_START;
layoutConst.gridx = 2;
layoutConst.gridy = 7;
layoutConst.gridwidth = 1;
add(childTravelerCountLabel, layoutConst);
layoutConst = new GridBagConstraints();
layoutConst.insets = new Insets(10, 10, 10, 10);
layoutConst.fill = GridBagConstraints.HORIZONTAL;
layoutConst.gridx = 3;
layoutConst.gridy = 7;
layoutConst.gridwidth = 1;
add(childTravelerCount, layoutConst);
}
//TODO: this
public void stateChanged(ChangeEvent event) {
}
//TODO, also: this
public void actionPerformed(ActionEvent event) {
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception ignored){}
DeltaFlightFrame myFrame = new DeltaFlightFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
}
}
I did do some messing around, here:
logo = new ImageIcon("../img/logo.png");
deltaLogo = new JLabel(logo);
System.out.println("Height: " + logo.getIconHeight());
System.out.println("Width: " + logo.getIconWidth());
And the output of that was:
Height: -1
Width: -1
Which I do find odd, if that's a starting point.
SO, my question is: Does my setup with GridBagLayout affect the display of the of the JLabel image?
Using the website provided by #camickr, I was able to successfully render the image using their "createImageIcon" method.

Expand JButton in GridBagLayout

How to expand JButton in GridBagLayout?
I tried setting the weights of the GridBagConstraints to non-zero values:
gbc.weightx = gbc.weighty = 1.0;
But it's only expanding width of the buttons, I want buttons to expand vertically too.
Here is the code in which I add buttons in grid:
private void initGui(){
setTitle("Calculator");
setSize(400,500);
var grid = new GridBagLayout();
setLayout(grid);
GridBagConstraints gridCon = new GridBagConstraints();
gridCon.weightx = gridCon.weighty = 1.0;
gridCon.gridy = 0;
gridCon.gridx = 0;
gridCon.gridwidth = 4;
gridCon.fill = gridCon.HORIZONTAL;
add(text,gridCon);
gridCon.gridwidth = 1 ;
String names[] = {"+","-","/","*","="};
for(int i = 0;i < 5; i++){
opeButtons[i] = new JButton(names[i]);
}
gridCon.gridx = 3;
for( int y = 1; y < 6; y++ ){
gridCon.gridy = y;
add(opeButtons[y-1],gridCon);
}
for(int y = 2, i = 1; y < 5; y++ ){
for(int x = 0 ; x < 3; x++,i++) {
gridCon.gridx = x;
gridCon.gridy = y;
numButtons[i] = new JButton(Integer.toString(i));
add(numButtons[i],gridCon);
}
}
gridCon.gridx = 0;
gridCon.gridy = 1;
add(lb,gridCon);
gridCon.gridx = 1;
add(rb,gridCon);
gridCon.gridx = 2;
add(cb,gridCon);
numButtons[0] = new JButton("0");
gridCon.gridx = 0;
gridCon.gridy = 5;
gridCon.gridwidth = 2;
add(numButtons[0],gridCon);
gridCon.gridwidth = 1;
gridCon.gridx = 2;
add(decb,gridCon);
}
Replace
gridCon.fill = gridCon.HORIZONTAL;
with
gridCon.fill = GridBagConstraints.BOTH;

Wrong Position with BorderLayout/GridBagLayout(Java)

I have a Problem with my code.
When I launch it, 2 panels are overlapingeven though, I declared one Panel as North and one as South.
EingabePanel is suppose to be on top and SchnittpunktPanel is suppose to be on the bottom.
here is the code, maybe someone can help me.
It's the first time I'm working on an GUI
package P2_pruefung.Frame;
import java.awt.*;
import javax.swing.*;
import P2_pruefung.Frame.Funktionen.CustomMath;
import P2_pruefung.Frame.Funktionen.MyActionListener;
import P2_pruefung.Objekte.Ungleichung;
import P2_pruefung.Objekte.UngleichungListe;
#SuppressWarnings("serial")
public class LinearFrame extends JFrame{
LayoutManager design1 = new BorderLayout();
LayoutManager design2 = new GridBagLayout();
GridBagConstraints rules = new GridBagConstraints();
public static JTextField tf_a = null;
public static JTextField tf_b = null;
public static JTextField tf_c = null;
public static JTextField tf_g = null;
public static JTextField tf_h = null;
public static JPanel eingabe = null;
public static JPanel schnittp = null;
public static JPanel grafik = null;
public static JPanel rechnen = null;
public static UngleichungListe unge = new UngleichungListe();
public static double g = 1;
public static double h = 1;
public LinearFrame(){
// Voreinstellung des Fensters
super("Lineare Optimierung");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1024,760);
this.setLocation(400,250);
JPanel oben = EingabePanel();
this.add(oben, BorderLayout.NORTH);
JPanel schnittp = SchnittpunktePanel();
this.add(schnittp, BorderLayout.SOUTH);
}
public JPanel EingabePanel() {
//Voreinstellungen für das Panel
eingabe = new JPanel();
eingabe.setLayout(design2);
rules.insets = new Insets(5,5,5,5);
rules.fill = GridBagConstraints.HORIZONTAL;
//Inhalt des Panels
// 1.Label
JLabel label1 = new JLabel("Bitte geben sie füre eine Ungleichung \"ax + bx <= c\" jeweils a, b und c ein");
rules.gridx = 1;
rules.gridy = 1;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label1, rules);
// Eingabe
JLabel label2 = new JLabel("a = ");
rules.gridx = 2;
rules.gridy = 2;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label2, rules);
JLabel label3 = new JLabel(" b = ");
rules.gridx = 3;
rules.gridy = 2;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label3, rules);
JLabel label4 = new JLabel(" c = ");
rules.gridx = 4;
rules.gridy = 2;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label4, rules);
tf_a = new JTextField();
rules.gridx = 2;
rules.gridy = 3;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(tf_a, rules);
tf_b = new JTextField();
rules.gridx = 3;
rules.gridy = 3;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(tf_b, rules);
tf_c = new JTextField();
rules.gridx = 4;
rules.gridy = 3;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(tf_c, rules);
JButton hinzu = new JButton("hinzufügen");
rules.gridx = 4;
rules.gridy = 4;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(hinzu, rules);
hinzu.addActionListener(MyActionListener.getInstance());
JLabel label5 = new JLabel("Bitte geben sie für eine Zielfunktion \"f(x;y) = gx + hx\" jeweils g und h ein");
rules.gridx = 1;
rules.gridy = 5;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label5, rules);
JLabel label6 = new JLabel("g = ");
rules.gridx = 2;
rules.gridy = 6;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label6, rules);
tf_g = new JTextField("1");
rules.gridx = 2;
rules.gridy = 7;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(tf_g, rules);
JLabel label7 = new JLabel("h = ");
rules.gridx = 3;
rules.gridy = 6;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label7, rules);
tf_h = new JTextField("1");
rules.gridx = 3;
rules.gridy = 7;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(tf_h, rules);
JButton akt = new JButton("Aktualisieren");
rules.gridx = 4;
rules.gridy = 7;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(akt, rules);
akt.addActionListener(MyActionListener.getInstance());
return eingabe;
}
public JPanel SchnittpunktePanel(){
//Voreinstellung des Panels
schnittp = new JPanel();
schnittp.setLayout(design2);
rules.insets = new Insets(5,5,5,5);
rules.fill = GridBagConstraints.HORIZONTAL;
JLabel label1 = new JLabel("Folgende Schnittpunkte des Lösungsploynoms sind bekannt: ");
rules.gridx = 1;
rules.gridy = 1;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label1, rules);
JLabel label2 = new JLabel("");
rules.gridx = 2;
rules.gridy = 2;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label2, rules);
int anz = unge.getSize();
boolean test = false;
Ungleichung[] id = new Ungleichung[anz];
String eingabe = "";
for( int i = 0; i <= anz; i++){
for ( Ungleichung s: unge.UngeListe){
if(s.getI() == i) {
id[i] = s;
}
}
}
for( int i = 0; i <= anz; i++){
for(int j = i+1; j < anz; j++){
double[] spunkt = CustomMath.getSchnittpunkt(id[i], id[j]);
if(spunkt == null) //nichts tun
for(Ungleichung s: unge.UngeListe){
if(s.checkUngleichung(spunkt[0], spunkt[1])){
test = true;
}
else{
test = false;
break;
}
}
if(test){
eingabe = eingabe + "(" + (Math.round(spunkt[0]*100)/100) + ";" + (Math.round(spunkt[1]*100)/100) + "), ";
}
}
}
return schnittp;
}
}
You should add components not to JFrame itself, but to JFrame.getContentPane().
this.getContentPant().setLayout(new BorderLayout());
this.getContentPant().add(BorderLayout.NORTH, new EingabePanel());
this.getContentPant().add(BorderLayout.SOUTH, new SchnittpunktePanel());
Okay guys, thanks for the help. I found it out myself. the problem was this part of code:
schnittp = new JPanel();
schnittp.setLayout(design2);
rules.insets = new Insets(5,5,5,5);
rules.fill = GridBagConstraints.HORIZONTAL;
JLabel label1 = new JLabel("Folgende Schnittpunkte des Lösungsploynoms sind bekannt: ");
rules.gridx = 1;
rules.gridy = 1;
rules.weightx = 1;
rules.weighty = 1;
eingabe.add(label1, rules);
I didn't add the labels to the Panel "schnittp", but to the Panel "eingabe"

Categories