Add a JScrollPane to avoid distortion of components - java

I am trying to make a variable size interface. However, currently, if the size of all components is greater than the dimension of the window, then those begin to
distort ! Therefore, I would like to add a JScrollPane to be displayed when the window size is no longer sufficient, in order to avoid this distortion.
I just started by adding a JScrollPane (as shown on the code bellow), but it does not seem to work, and I am not sure what to change now.
JScrollPane myScrollPane = new JScrollPane();
myWindow.getContentPane().add(myScrollPane, BorderLayout.CENTER);
EDIT 1: here is a simple example of what I have tried:
static void one(String[] ari, JLabel sess_nb, Box boxy, long[] res){
int p;
int length = ari.length;
Border cadre = BorderFactory.createLineBorder(Color.black);
Font police = new Font("Monospaced 13", Font.BOLD, 18);
Font police1 = new Font("Monospaced 13", Font.BOLD, 15);
Font font = new Font("Monospaced 13", Font.ITALIC, 13);
for (p=0;p<length;p++){
Box boxz = Box.createVerticalBox();
JLabel Rate = new JLabel("Rating Group "+r);
JLabel rg_reser = new JLabel();
JLabel switsh = new JLabel("1");
JLabel reser = new JLabel(); //in this label the resrvation for each update and each rating group will be stocked
JLabel consump = new JLabel(); //in this label will be stocked the consumption
//----------------------------------------------------
//choice of MB or kB
JComboBox combo = new JComboBox();
combo.addItem("Megabytes");
combo.addItem("Bytes");
combo.addItem("kilobytes");
combo.addItem("Gigabytes");
JLabel upload = new JLabel("Upload consumption:");
JTextField upload_entry = new JTextField("7.5");
JLabel download = new JLabel("Download consumption:");
JTextField download_entry = new JTextField("7.5");
JTextField total_entry = new JTextField();
JLabel total = new JLabel("Total consumption:");
JButton rg = new JButton("Next");
JLabel update = new JLabel("Result here");
boxz.add(Rate);
boxz.add(total);
boxz.add(total_entry);
boxz.add(upload);
boxz.add(upload_entry);
boxz.add(download);
boxz.add(download_entry);
boxz.add(combo);
boxz.add(rg);
boxz.add(update);
boxy.add(boxz);
scrollPane1.add(boxy);
}
EDIT 2: here is a new test code, but it also does not work:
public Fenetre(){
this.setTitle("Data Simulator");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
String hello = "hello";
int number = 69;
JPanel content = new JPanel();
content.setBackground(Color.LIGHT_GRAY);
//Box imad = Box.createHorizontalBox();
JTextArea textArea = new JTextArea(10, 10);
JLabel imad = new JLabel();
imad.setText(hello + " your favorite number is " + number + "\nRight?");
JScrollPane scrollPane = new JScrollPane();
setPreferredSize(new Dimension(450, 110));
scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setEnabled(true);
scrollPane.setWheelScrollingEnabled(true);
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
add(scrollPane, BorderLayout.CENTER);
//---------------------------------------------
//On ajoute le conteneur
content.add(imad);
content.add(textArea);
content.add(scrollPane);
this.setContentPane(content);
this.setVisible(true);
this.setResizable(false);
}
A small white scare appears (I think it is the scroll bar or scroll panel?) and the components in the window exceeds the windows size but nothing happens when I try to scroll (using the mouse or by clinking on the white scare). I don't know what is wrong.

Don't use scrollPanel.add(boxy) - you need to add boxy to the scrollPanel's viewport, not to the scrollPanel itself. Use scrollPanel.setViewportView(boxy) instead, or better yet create the ScrollPanel AFTER you create boxy and use the constructor that takes a Component argument.

Related

How to change the font of a string in gui and then change it back again to normal

Hello fellow programmers across the world I am currently having a problem with one of my frames in my GUI. I have managed to change the font of my messeges to itallic and the color to grey and i have set my textfield to editable(true). The problem is that when I delete the string in gray and italic and try to write something useful in the textfield, rather that plain font and a color of black for the string it always comes out italic and gray. Is there any way that a string in a text message can change font and color and then then next string you write to have the normal font and black color? Would appreciate any sugggestions at this point. Here is my code:
void makeSecondFrame(){
frame2 = new JFrame("Step one!Vertification!");
Container contentPane2 = frame2.getContentPane();
JLabel label2 = new JLabel("Welcome to step 1 of our algorithm, Verification!" + "To varify yourself of owning an account please enter the following:");
JLabel label3 = new JLabel("First Name:");
JLabel label4 = new JLabel("Middle Name:");
JLabel label5 = new JLabel("Surname:");
JLabel label6 = new JLabel("Account Number:");
JLabel label7 = new JLabel ("Registration Number:");
firstName = new TextField("Enter your name here");
firstName.setForeground(Color.gray);
firstName.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
firstName.setEditable(true);
middleName = new TextField("Enter your middle name here");
middleName.setForeground(Color.gray);
middleName.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
middleName.setEditable(true);
surname = new TextField("Enter your surname here");
surname.setForeground(Color.gray);
surname.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
surname.setEditable(true);
accountNumber = new TextField("Enter your main account number");
accountNumber.setForeground(Color.gray);
accountNumber.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
accountNumber.setEditable(true);
registrationNumber = new TextField("Enter your registration number");
registrationNumber.setForeground(Color.gray);
registrationNumber.setFont(new java.awt.Font("Arail", Font.ITALIC, 12));
registrationNumber.setEditable(true);
JPanel p2 = new JPanel(new GridLayout(1,1));
p2.add(label2);
contentPane2.add(p2, BorderLayout.NORTH);
JPanel p3 = new JPanel(new GridLayout(10,1));
p3.add(label3);
p3.add(firstName);
p3.add(label4);
p3.add(middleName);
p3.add(label5);
p3.add(surname);
p3.add(label7);
p3.add(registrationNumber);
p3.add(label6);
p3.add(accountNumber);
contentPane2.add(p3, BorderLayout.WEST);
JButton next = new JButton("Next!");
next.addActionListener(this);
JButton cancel = new JButton("Cancel");
cancel.addActionListener(this);
JPanel p4 = new JPanel(new GridLayout(1,1));
p4.add(next);
p4.add(cancel);
contentPane2.add(p4, BorderLayout.SOUTH);
frame2.pack();
frame2.setVisible(true);
}
You need to act on an appropriate action on the JTextField. The best I can think of is the FocusListener.focusGained():
firstName = new TextField("Enter your name here");
firstName.setForeground(Color.gray);
firstName.setFont(new java.awt.Font("Arial", Font.ITALIC, 12));
firstName.setEditable(true);
firstName.addFocuListener(new FocusAdapter(){
#Overwrite
public void focusGained(FocusEvent e){
firstName.setText("");// remove help message, will also remove previously entered text!
firstName.setFont(new JTextField().getFont()); // reset to default
}
}
Well that did remove the helper message – Христо Петков
as I mentioned...
and I want to keep it – Христо Петков
Then you might want to use the other method in the FocusListener interface too:
#Overwrite
public void focusLost(FocusEvent e){
if(userDidNotEnterTextIn(firstName){ // hopefully you know how to find out...
firstName.setText("Enter your name here");
// do formatting again
}
}

How to set text in a label on the right side?

I've divided my space into two cols and I'd like to set labels on the left and right side, like this: http://i.stack.imgur.com/LFY8S.png
My code:
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 2));
anotherPanel.add(panel);
JLabel label1 = new JLabel("text1");
panel.add(label1);
JLabel label2 = new JLabel("text2");
panel.add(label2);
I've tried with
label2.setAlignmentX(RIGHT_ALIGNMENT);
but it doesn't work.
JLabel label2 = new JLabel("text2", SwingConstants.RIGHT);
is all you need.
or
label2.setHorizontalAlignment(SwingConstants.RIGHT);
When in doubt -- check the JLabel API!

Aligning JLabels in BoxLayout

I've been searching around for days trying to find the answer to this, and I can't find out what's wrong. What I want to do is make it so the top JLabel (called display) align to the right and the bottom JLabel (called notice) to align to the left. Neither seems to want to do either. From what I've read, what I have should work, but it doesn't. Help?
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
public class Calculator {
private static JButton clear, add, subtract, multiply, divide, equals, point, zero, one, two, three, four, five, six, seven, eight, nine;
private static JLabel display, notice, blank1, blank2, blank3;
private static JPanel mainPanel, buttonPanel, topLabel, bottomLabel;
public static void goGUI() {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(600,300));
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
frame.setContentPane(mainPanel);
Border empty = BorderFactory.createEmptyBorder(10,10,10,10);
mainPanel.setBorder(empty);
buttonPanel = new JPanel(new GridLayout(5,4, 5,5));
Border buttonBorder = BorderFactory.createEmptyBorder(10,0,10,0);
buttonPanel.setBorder(buttonBorder);
topLabel = new JPanel();
bottomLabel = new JPanel();
clear = new JButton("C");
add = new JButton("+");
subtract = new JButton("-");
multiply = new JButton("*");
divide = new JButton("/");
equals = new JButton("=");
point = new JButton(".");
zero = new JButton("0");
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
// Here I added ActionListeners to all the buttons...
display = new JLabel("0");
display.setAlignmentX(Component.RIGHT_ALIGNMENT);
notice = new JLabel("*Maximum 19 digits - Order of operations not taken into account*");
notice.setAlignmentX(Component.LEFT_ALIGNMENT);
blank1 = new JLabel();
blank2 = new JLabel();
blank3 = new JLabel();
buttonPanel.add(clear);
buttonPanel.add(blank1);
buttonPanel.add(blank2);
buttonPanel.add(blank3);
buttonPanel.add(seven);
buttonPanel.add(eight);
buttonPanel.add(nine);
buttonPanel.add(divide);
buttonPanel.add(four);
buttonPanel.add(five);
buttonPanel.add(six);
buttonPanel.add(multiply);
buttonPanel.add(one);
buttonPanel.add(two);
buttonPanel.add(three);
buttonPanel.add(subtract);
buttonPanel.add(zero);
buttonPanel.add(point);
buttonPanel.add(equals);
buttonPanel.add(add);
topLabel.add(display);
bottomLabel.add(notice);
mainPanel.add(topLabel);
mainPanel.add(buttonPanel);
mainPanel.add(bottomLabel);
frame.pack();
frame.setVisible(true);
} //end goGUI
//ActionListener classes went here...
public static void main(String[] args) {
try {
// Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) {}
goGUI();
} //end main
} //end Calculator
I removed all ActionListener stuff for clarity. But this is the layout that I can't fix.
Consider having topLabel not use the default FlowLayout but rather something that makes its contents fill it up such as BorderLayout.
topLabel.setLayout(new BorderLayout());
Next make sure that you set the display's horizontal alignment, not its alignmentX to the right:
display.setHorizontalAlignment(SwingConstants.RIGHT);
Similar changes should be made for your notice JLabel and its container.
I add few line of code because I like BoxLayout. I have almost the same problems in align components few hours ago.
If we want to acheive something like this
// +----------------------------------+
// | Label1 | | Label2 |
// +----------------------------------+
You have only to put a "Box.createHorizontalGlue()" between the two.
If instead we want something like this:
// +----------------------------------+
// | Label1 | |
// |----------+ +----------|
// | | Label2 |
// +----------------------------------+
We have to put the labels inside two differen JPanel, setting a different AlignmentX to them.
This is because in the layout all the components should have the same alignmentX. To avoid this restriction we can do something like this:
// +-----------------------------------------+
// | Label1 | label1 is inside a JPanel |
// +----------+ - - - - - - - - - - - - - - -+
// + - - - - - - - - - - - - - - -+----------+
// | label2 also | Label2 |
// +-----------------------------------------+
(don't mind about the space between the two panel, is only for graphic)
This can be done with this code:
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.PAGE_AXIS));
JLabel lbl1 = new JLabel("Label1");
JPanel p1 = new JPanel();
p1.setOpaque(false);
p1.setLayout(new BorderLayout(0, 0));
p1.add(lbl1, BorderLayout.CENTER);
JLabel lbl2 = new JLabel("Label1");
JPanel p2 = new JPanel();
p2.setOpaque(false);
p2.setLayout(new BorderLayout(0, 0));
p2.add(lbl2, BorderLayout.CENTER);
mainPanel.add(p1);
mainPanel.add(p2);
I hope this is useful.
You can simply use BorderLayout() :
public static void goGUI() {
....
topLabel = new JPanel(new BorderLayout());
bottomLabel = new JPanel(new BorderLayout());
....
topLabel.add(display, BorderLayout.EAST);
bottomLabel.add(notice, BorderLayout.WEST);
}
Also remove these calls :
display.setAlignmentX(Component.RIGHT_ALIGNMENT);
notice.setAlignmentX(Component.LEFT_ALIGNMENT);

What layout on Java I need?

I have a JPanel and for example, if I click on the button "INSERT", I can add a JButton and a JLabel. My problem is I need to insert the JLabel under the JButton. The JLabel text must centred respect the JButton text. After that, I want a space around 10 pixels to use again my "INSERT" button and add horizontally a new pair on JButton and JLabel with the same orientation.
Thanks!
PD: Please, complement your question with an attempt.
Here is a quick example that shows a dynamic (which is what I assume you wanted) setup to allow insertion of an undefined number of panels:
public class AwesomeAnswer {
public static void main(String[] args) {
// please not that this is only an example and not a
// Swing thread safe way of starting a JFrame
JFrame frame = new JFrame();
JPanel content = (JPanel)frame.getContentPane();
// create our top panel that will hold all of the inserted panels
JPanel page = new JPanel();
page.setLayout( new BoxLayout( page, BoxLayout.Y_AXIS ) );
// add our page to the frame content pane
content.add( page );
// add two button/label panels
page.add( insert( "This is an awesome answer", "Accept" ) );
page.add( insert( "Say thank you", "Thank" ) );
frame.pack();
frame.setVisible( true );
}
public static final JPanel insert( String labelText, String buttonText ) {
// create the label and the button
JLabel lbl = new JLabel( labelText );
JButton btn = new JButton( buttonText );
// create the panel that will hold the label and the button
JPanel wrapPanel = new JPanel( new GridBagLayout() );
wrapPanel.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
// tell the grid bag how to behave
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 0;
gbc.gridheight = 2;
// make the button centered
JPanel buttonPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
buttonPanel.add( btn );
// make the label centered
JPanel labelPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
labelPanel.add( lbl );
// add our button and label to the grid bag with our constraints
wrapPanel.add( buttonPanel, gbc );
wrapPanel.add( labelPanel, gbc );
return wrapPanel;
}
}
I think that you have something like that
rootPane
+-----panelButton
| +------JButton
|
+-----panelPanels
+-----panel
+---JButton
+---JLabel
The SpringLayout can help you
SpringUtilities.makeGrid(panel,
2, 1, //rows, cols
0, 0, //initialX, initialY
5, 5);//xPad, yPad

Recording user choice java

I have a scroll box where a user can choose between options. If I want the user's choice to print elsewhere, what will I need to call? I already have where I want the user choice.
results.setText();
What would go in the parenthesis?
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.awt.event.*;
public class container implements ActionListener
{
JPanel panels;
Timer timer;
JTextField userTypingRegion;
JTextArea results;
JComboBox<Integer> ageEntries;
JComboBox<String> raceList;
public void init(Container pane)
{
JButton switcher = new JButton("Next / Back");
switcher.addActionListener(this);
JPanel infoPage = new JPanel();
infoPage.setBackground(Color.BLACK);
//CENTER
JPanel panelCenter = new JPanel();
panelCenter.setBackground(Color.GRAY);
panelCenter.setLayout(new BoxLayout(panelCenter, BoxLayout.Y_AXIS));
infoPage.add(BorderLayout.CENTER, panelCenter);
///Gender
JPanel genderSelection = new JPanel();
JLabel gender = new JLabel("Gender:");
JCheckBox checkGenderMale = new JCheckBox("Male");
JCheckBox checkGenderFemale = new JCheckBox("Female");
genderSelection.add(gender);
genderSelection.add(checkGenderMale);
genderSelection.add(checkGenderFemale);
panelCenter.add(genderSelection);
///Age
JPanel ageSelection = new JPanel();
JLabel age = new JLabel("Age:");
ArrayList<Integer> ageList = new ArrayList<Integer> ();
for (int i = 1; i <= 100; ++i)
{
ageList.add(i);
}
DefaultComboBoxModel<Integer> modelAge = new DefaultComboBoxModel<Integer>();
for (Integer i : ageList)
{
modelAge.addElement(i);
}
JComboBox<Integer> ageEntries = new JComboBox<Integer>();
ageEntries.setModel(modelAge);
ageSelection.add(age);
ageSelection.add(ageEntries);
panelCenter.add(ageSelection);
///Race
JPanel raceSelection = new JPanel();
JLabel race = new JLabel("Race/Ethnicity:");
String[] raceEntries = {"White", "Black", "Hispanic"
, "Asian/Pacific Islander"
, "Alaska Native/American Indian", "Confused"};
JComboBox<String> raceList = new JComboBox<String>(raceEntries);
raceList.addActionListener(new transferInfo());
raceSelection.add(race);
raceSelection.add(raceList);
panelCenter.add(raceSelection);
///Question 1
JPanel firstQuestion = new JPanel();
JLabel one = new JLabel("How often do you read?");
String[] oneEntries = {"Always", "Often", "Sometimes"
, "Not Often", "What is reading?"};
JComboBox<String> oneAnswer = new JComboBox<String>(oneEntries);
firstQuestion.add(one);
firstQuestion.add(oneAnswer);
panelCenter.add(firstQuestion);
///Question 2
JPanel secondQuestion = new JPanel();
JLabel two = new JLabel("Average time (in minutes) " +
"spent on the computer per day:");
ArrayList<Integer> hourList = new ArrayList<Integer>();
for (int z = 0; z <= 1440; z = z + 30)
{
hourList.add(z);
}
DefaultComboBoxModel<Integer> modelHour = new DefaultComboBoxModel<Integer>();
for (Integer z : hourList)
{
modelHour.addElement(z);
}
JComboBox<Integer> hourEntries = new JComboBox<Integer>();
hourEntries.setModel(modelHour);
secondQuestion.add(two);
secondQuestion.add(hourEntries);
panelCenter.add(secondQuestion);
///Question 3
JPanel thirdQuestion = new JPanel();
JLabel three = new JLabel("Favorite Subject");
String[] threeEntries = {"English", "Math", "Science"
, "Foreign Languages", "History", "Hate them all"};
JComboBox<String> threeAnswer = new JComboBox<String>(threeEntries);
thirdQuestion.add(three);
thirdQuestion.add(threeAnswer);
panelCenter.add(thirdQuestion);
///Question 4
JPanel fourthQuestion = new JPanel();
JLabel four = new JLabel("Average sleep (in minutes) per night:");
ArrayList<Integer> sleepTimeList = new ArrayList<Integer>();
for (int y = 0; y <= 1440; y = y + 30)
{
sleepTimeList.add(y);
}
DefaultComboBoxModel<Integer> modelSleepTime = new DefaultComboBoxModel<Integer>();
for (Integer z : sleepTimeList)
{
modelSleepTime.addElement(z);
}
JComboBox<Integer> sleepTimeEntries = new JComboBox<Integer>();
sleepTimeEntries.setModel(modelSleepTime);
fourthQuestion.add(four);
fourthQuestion.add(sleepTimeEntries);
panelCenter.add(fourthQuestion);
///Question 5
JPanel fifthQuestion = new JPanel();
JLabel five = new JLabel("I am...");
String [] fiveEntries = {"Outgoing", "In the middle", "Shy"};
JComboBox<String> fiveAnswer = new JComboBox<String>(fiveEntries);
fifthQuestion.add(five);
fifthQuestion.add(fiveAnswer);
panelCenter.add(fifthQuestion);
///Question 6
JPanel sixthQuestion = new JPanel();
JLabel six = new JLabel("I am...");
String [] sixEntries = {"Adventurous", "In the middle", "Lazy"};
JComboBox<String> sixAnswer = new JComboBox<String>(sixEntries);
sixthQuestion.add(six);
sixthQuestion.add(sixAnswer);
panelCenter.add(sixthQuestion);
///Question 7
JPanel seventhQuestion = new JPanel();
JLabel seven = new JLabel("I live in the...");
String [] sevenEntries = {"City", "Suburb", "Country", "Narnia"};
JComboBox<String> sevenAnswer = new JComboBox<String>(sevenEntries);
seventhQuestion.add(seven);
seventhQuestion.add(sevenAnswer);
panelCenter.add(seventhQuestion);
///Question 8
JPanel eighthQuestion = new JPanel();
JLabel eight = new JLabel("Please choose the painting you like.");
eighthQuestion.add(eight);
panelCenter.add(eighthQuestion);
///Adding Picture
JPanel pictures = new JPanel();
ImageIcon image = new ImageIcon("C:\\Users\\Kevin\\Desktop\\Left and Right.jpg");
JLabel imageButton = new JLabel();
imageButton.setIcon(image);
pictures.add(imageButton);
panelCenter.add(pictures);
///Question 9
JPanel ninthQuestion = new JPanel();
JCheckBox checkLeft = new JCheckBox("I like the left one!");
JCheckBox checkRight = new JCheckBox("I like the right one!");
ninthQuestion.add(checkLeft);
ninthQuestion.add(checkRight);
panelCenter.add(ninthQuestion);
////Second Card
JPanel programFrame = new JPanel();
programFrame.setBackground(Color.BLACK);
programFrame.setMinimumSize(new Dimension(200, 300));
programFrame.setMaximumSize(new Dimension(800, 700));
programFrame.setPreferredSize(new Dimension(500, 500));
///CENTER DATA COLLECTION REGION
JPanel dataCollectionRegion = new JPanel();
dataCollectionRegion.setBackground(Color.LIGHT_GRAY);
dataCollectionRegion.setLayout(
new BoxLayout(dataCollectionRegion, BoxLayout.Y_AXIS));
programFrame.add(BorderLayout.CENTER, dataCollectionRegion);
///South Region
JPanel southRegion = new JPanel();
southRegion.setBackground(Color.BLACK);
southRegion.setLayout(new BoxLayout(southRegion, BoxLayout.Y_AXIS));
programFrame.add(BorderLayout.NORTH, southRegion);
///Data Components
JLabel sampleWriting = new JLabel("<html>7 Dear friends, let us love one another, for love comes from God. <br>Everyone who loves has been born of God and knows God. 8 Whoever <br>does not love does not know God, because God is love. 9 This is how <br>God showed his love among us: He sent his one and only Son into <br>the world that we might live through him. 10 This is love: not that we <br>loved God, but that he loved us and sent his Son as an atoning sacrifice <br> for our sins. 11 Dear friends, since God so loved us, we also ought <br>to love one another. 12 No one has ever seen God; but if we love one <br> another, God lives in us and his love is made complete in us. <br> 1 Everyone who believes that Jesus is the Christ is born of God, and everyone <br> who loves the father loves his child as well. 2 This is how we know <br> that we love the children of God: by loving God and carrying out his commands. <br> 3 In fact, this is love for God: to keep his commands. And his commands <br> are not burdensome, 4 for everyone born of God overcomes the world. <br> This is the victory that has overcome the world, even our faith. 5 Who <br> is it that overcomes the world? Only the one who believes that Jesus is the Son of God.</html>");
userTypingRegion = new JTextField();
userTypingRegion.addActionListener( this);
dataCollectionRegion.add(sampleWriting);
dataCollectionRegion.add(userTypingRegion);
///Instructions South
JLabel instructions = new JLabel("Instruction: Type the " +
"passage as fast as possible in the space provided.");
instructions.setForeground(Color.white);
JLabel showResult = new JLabel("- - - Results - - -");
showResult.setForeground(Color.white);
showResult.setHorizontalTextPosition(JLabel.CENTER);
results = new JTextArea();
results.setEditable(false);
southRegion.add(instructions);
southRegion.add(Box.createRigidArea(new Dimension(0,5)));
southRegion.add(showResult);
southRegion.add(Box.createRigidArea(new Dimension(0,5)));
southRegion.add(results);
///add cards
panels = new JPanel(new CardLayout());
panels.add(infoPage);
panels.add(programFrame);
pane.add(switcher, BorderLayout.PAGE_START);
pane.add(panels, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt)
{
CardLayout layout = (CardLayout)(panels.getLayout());
layout.next(panels);
}
class transferInfo implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
results.setText("Selected: " + raceList.getSelectedItem());
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Biometric Keystroke Dynamics");
container TabChanger = new container();
TabChanger.init(frame.getContentPane());
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
}
You can see my attempt at resolving this issue near the end of the code. I apologize in advance for my messy coding. (it is my first)
First of all the mistakes you are committing in your code :
You are declaring JComboBox<Integer> ageEntries; and JComboBox<String> raceList; as your instance variables and as your localvariables inside your init(...) method. Don't declare them twice, use only the instance variables or the local variables, as the need be, but do not use them twice.
Always make this a habit of yours to add this ilne frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); as you make an object of JFrame, this helps in shutting down the JFrame window in a good manner.
Now for what to write in results.setText(); . You can make one validation method inside your container class, which will return boolean and will be called inside the actionPerformed(...) method of the NextButton and inside this method check the values as entered by the user as legitimate or not, if they are legal values then use StringBuilder to append it like say
StringBuilder sb = new StringBuilder(); // this will be your instance variable not local.
sb.append((String) ageEntries.getSelectedItem());
sb.append(" "); // Add this space to distinguish between two values.
// and so on for other values.
Once done you can write results.setText(containerObject.sb.toString()); to show these values.

Categories

Resources