JTextArea parameter and user input issues - java

I have a JTextArea where I want to allow the user to input any number of strings up to 100 but it could be less. When I set the JTextArea as I have in my code below where it is commented out (i.e. //tfResult= new JTextArea(10, 0);) and the user inputs ten lines of strings then my code runs exactly as expected and prints out what I need it to.
But if I try to input more of less lines I get
java.lang.ArrayIndexOutOfBoundsException
followed by the number of lines of user input, whether I have it declared with no bounds or as I have it commented out.
I am new to graphics in java and I can't figure out why this is happening and I have searched everywhere for answers. Do I have the bounds set wrong or have I declared the JTextArea wrong?
I also am trying to include a JScrollPane but I am having issues with that also as its not showing up.
I would really appreciate any help as I am struggling to solve this issue.
class Window {
JFrame windowFrame;
Panel bottomPanel;
JScrollPane scroll;
JTextArea tfResult;
Button btnPlayAgain;
Font font;
Window(int width, int height, String title)
{
windowFrame = new JFrame();
windowFrame.setTitle(title);
windowFrame.setBounds(0,0,width,height);
windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
windowFrame.setResizable(true);
windowFrame.setCursor(new Cursor(Cursor.HAND_CURSOR)); // setting cursor to hand
windowFrame.setLayout(null);
createBottomPanel();
windowFrame.add(bottomPanel);
//windowFrame.add(field.getCanvas());
windowFrame.setVisible(true);
}
private void createBottomPanel()
{
JButton b = new JButton("Compute");
bottomPanel = new Panel();
bottomPanel.setBackground(Color.PINK);
bottomPanel.setBounds(0,400,800,140);
bottomPanel.setLayout(null);
//*********
//tfResult= new JTextArea(10, 0);
tfResult= new JTextArea();
tfResult.setBounds(10,10,600,100);
tfResult.setFont(new Font("SansSerif", Font.BOLD, 16));
tfResult.setFocusable(true);
scroll = new JScrollPane(tfResult);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
btnPlayAgain = new Button("Compute");
btnPlayAgain.setBounds(620,10,150,100);
btnPlayAgain.setBackground(Color.RED);
btnPlayAgain.setFont(new Font("SansSerif", Font.BOLD, 24));
btnPlayAgain.setFocusable(true);
bottomPanel.add(tfResult);
bottomPanel.add(btnPlayAgain);
bottomPanel.add(b);
bottomPanel.add(scroll);
tfResult.setVisible(true);
scroll.setVisible(true);
btnPlayAgain.setVisible(true);
bottomPanel.setVisible(true);
btnPlayAgain.addActionListener(new ActionListener()
{
//#Override
public void actionPerformed(ActionEvent e)
{
//should include the code to genrate the output inside here
String input = tfResult.getText();
Mat xy;
xy = new Mat();
//String output = xy.getOutput(input).toString();
String output = xy.getOutput(input);
//String output = Output(input);
tfTarget.setText(output);
}
});
}
}

I went ahead and created the following GUI that allows you to enter data with a JTextArea.
The first thing I did was start my Swing application with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
Next, I created a JFrame. Then I created a JPanel with a BorderLayout. The JTextArea is inside of a JScrollPane, which is then placed inside of the center of a JPanel.
The JButton is placed after the last line of the JPanel.
Here's the complete runnable code, otherwise known as a minimal runnable example.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class JTextAreaInputGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JTextAreaInputGUI());
}
private JTextArea textArea;
#Override
public void run() {
JFrame frame = new JFrame("JTextArea Input GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
textArea = new JTextArea(10, 40);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("Submit");
button.addActionListener(new ButtonListener());
panel.add(button, BorderLayout.AFTER_LAST_LINE);
return panel;
}
public class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
System.out.println(textArea.getText().trim());
}
}
}

Related

Jpanel textbox no output

I'm working in java using Jpanel and my work is compiling fine however is showing no output. hopefully, someone could tell me why this is. I'm using jscrollpane and I'm calling it at the end idk if it's something to do with the listener or what.
FileDemoPanel.java
package Tutoiral03Task01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JPanel implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton openBtn, saveBtn;
JTextArea workTa;
openBtn = new JButton ("Open");
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn = new JButton ("Save");
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
add(logScrollPane);
}
}
FileDemo.java
package Tutoiral03Task01;
import javax.swing.*;
public class FileDemo {
public static void main (String[] args){
JFrame frame = new JFrame("Working with files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new FileDemoPanel());
frame.pack();
frame.setVisible(true);
}
}
The problem is that you create all the buttons and others in a actionperformed method.
This is wrong, because that is used as a ButtonListener, so if you dont press a button nothing will happened. We use to write the GUI frame in the constructor of the class.Then we create an object type of the GUI class. So i think i fixed it and i did some extra changes to make the program more simple. The step i didnt do is to add a ButtonListener, so Buttons do nothing.
i wish it will helps you.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JFrame {
private JPanel panel = new JPanel();
private JButton openBtn = new JButton("Open");
private JButton saveBtn = new JButton ("Save");
private JTextArea workTa;
public FileDemoPanel(){
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
panel.add(openBtn);
panel.add(saveBtn);
panel.add(logTA);
panel.add(logScrollPane);
this.setContentPane(panel);
this.setVisible(true);
this.setResizable(true);
this.setSize(350,150);
this.setTitle("Κεντρική Σελίδα");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
And the mainclass.As you can see is too small.
public class FileDemo {
public static void main (String[] args){
new FileDemoPanel();
}
}

Adding an action listener to JButtons created by a loop

I am having trouble finding a way to invoke an action listener that returns the value of the button clicked in the text area at the bottom.
I made the buttons using a for loop and did not expressly give the buttons a name so I do not know how to reference them when trying to incorporate an ActionListener.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class buttoner implements ActionListener {
//JFrame
JFrame frame = new JFrame("Button Game");
//Make JPanels
JPanel panelLabel = new JPanel();
JPanel buttonGrid = new JPanel(new GridLayout(0,10));
JPanel bottomPanel = new JPanel();
//JLabel
private JLabel label1 = new JLabel("The Button Game");
public buttoner() {
//set layout
frame.setLayout(new BorderLayout());
frame.add(panelLabel, BorderLayout.NORTH);
frame.add(buttonGrid, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
//Set stuff
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,700);
frame.setVisible(true);
//Change label color
label1.setForeground(Color.RED);
//add Label
panelLabel.add(label1);
//add Buttons
for (int i = 1; i <= 60; i++) {
String val = Integer.toString(i);
buttonGrid.add(new JButton(val));
}
//Add JText Area to bottom JPanel
String num = "value";
JTextArea jta = new JTextArea(num, 1, 1);
bottomPanel.add(jta);
frame.pack();
}
public static void main(String args[]){
buttoner gui = new buttoner();
}
public void actionPerformed(ActionEvent a) {
}
}
I created an action listener to put the value in the text area at the bottom of the GUI.
I fixed a few problems with your code.
In the main method, I called the SwingUtilities invokeLater method to put the Swing GUI on the Event Dispatch thread (EDT). Swing components must be created and updated on the EDT.
The name of a Java class must start with a capital letter.
It's safer to put your Swing components on a JPanel, rather than add them directly to a JFrame.
I separated the code that creates the JFrame from the code that creates the JPanels. It should be easier for any reader of your code, including yourself, to understand what's going on.
In the createMainPanel method, I grouped the code so that everything having to do with the buttonGrid JPanel, to take one instance, is in one place in the code.
I added the action listener to the code that creates the buttonGrid JPanel.
I wrote action listener code that updates the JTextArea with the left clicked button label.
Here's the corrected code.
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class Buttoner implements ActionListener {
// JFrame
private JFrame frame = new JFrame("Button Game");
// Make JPanels
private JPanel panelLabel = new JPanel();
private JPanel buttonGrid = new JPanel(new GridLayout(0, 10));
private JPanel bottomPanel = new JPanel();
// JLabel
private JLabel label1 = new JLabel("The Button Game");
private JTextArea jta;
public Buttoner() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
// Change label color
label1.setForeground(Color.RED);
// add Label
panelLabel.add(label1);
panel.add(panelLabel, BorderLayout.NORTH);
// add Buttons
for (int i = 1; i <= 60; i++) {
String val = Integer.toString(i);
JButton button = new JButton(val);
button.addActionListener(this);
buttonGrid.add(button);
}
panel.add(buttonGrid, BorderLayout.CENTER);
// Add JText Area to bottom JPanel
String num = "value";
jta = new JTextArea(num, 1, 1);
jta.setEditable(false);
bottomPanel.add(jta);
panel.add(bottomPanel, BorderLayout.SOUTH);
return panel;
}
public static void main(String args[]) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Buttoner();
}
};
SwingUtilities.invokeLater(runnable);
}
public void actionPerformed(ActionEvent a) {
JButton button = (JButton) a.getSource();
jta.setText(button.getText());
}
}
Try creating an array of buttons and add the newly created button to the array. See comments.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class buttoner implements ActionListener {
//JFrame
JFrame frame = new JFrame("Button Game");
//Make JPanels
JPanel panelLabel = new JPanel();
JPanel buttonGrid = new JPanel(new GridLayout(0,10));
JPanel bottomPanel = new JPanel();
//JLabel
private JLabel label1 = new JLabel("The Button Game");
private JButton buttons[] = new JButton[60]; //create an array of button for future reference
public buttoner() {
//set layout
frame.setLayout(new BorderLayout());
frame.add(panelLabel, BorderLayout.NORTH);
frame.add(buttonGrid, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
//Set stuff
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,700);
frame.setVisible(true);
//Change label color
label1.setForeground(Color.RED);
//add Label
panelLabel.add(label1);
//add Buttons
for (int i = 1; i <= 60; i++) {
String val = Integer.toString(i);
JButton btn = new JButton(val);
btn.addActionListener(this); //add an actionListener right away
buttons[i] = btn; //add the button in the array for future reference
buttonGrid.add(btn);
}
//Add JText Area to bottom JPanel
String num = "value";
JTextArea jta = new JTextArea(num, 1, 1);
bottomPanel.add(jta);
frame.pack();
}
public static void main(String args[]){
buttoner gui = new buttoner();
}
public void actionPerformed(ActionEvent a) {
}
}

JPanel does not appear when i try to add it to ContentPane

I'm having a problem trying to change JPanels by using buttons. I have a JFrame with 2 panels, 1 of them is for the buttons, which i want them to always be showed. The other one is the one that i will be switching everytime i press one ot the buttons of the other panel. The problem is that everytime i press them nothing really ever displays, i keep my buttons but the other panel that i call does not appear.
Code for one of the buttons is as follows
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ReparacaoPanel r = new ReparacaoPanel(this, this.jPanel1);
this.getContentPane().remove(this.jPanel1);
this.getContentPane().add(r);
//this.setContentPane(r);
this.visiblePanel.setVisible(false);
this.visiblePanel = r;
this.pack();
this.setVisible(true);
r.setLocation(200, 200);
this.getContentPane().revalidate();
this.repaint();
}
If i try to use "this.setContentPane(r);" (it sets the frame to only show the panel) the panel shows. But when i try to call it as i'm trying to do in the code above nothing is showed apart from the panel that has the buttons.
I have no idea what i'm doing wrong, it does not seem to be a problem with the JPanel that i'm trying to call as it shows if used alone.
Anyone can help me out?
Consider this working example for switching manually between panels. Which produces this output.
.........
Some tiny NumberPanel
Every new instance shows another number in the center.
import javax.swing.JPanel;
public class NumberPanel extends JPanel {
private static int counter = 0;
public NumberPanel() {
setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel = new JLabel("" + counter++);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(lblNewLabel);
}
}
Setting up a frame
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.SOUTH);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.getContentPane().remove(numberPanel);
numberPanel = new NumberPanel();
frame.getContentPane().add(numberPanel, BorderLayout.CENTER);
frame.pack();
}
});
panel.add(btnNewButton);
numberPanel = new NumberPanel();
frame.getContentPane().add(numberPanel, BorderLayout.CENTER);
frame.pack();
}
Testprogram
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestPanelSwitch {
private JFrame frame;
private NumberPanel numberPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestPanelSwitch window = new TestPanelSwitch();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestPanelSwitch() {
initialize();
}
private void initialize() {
// see above
}
}
Back to the Question
I think you only need to pack your frame, like in the anonymous ActionListener.
frame.getContentPane().remove(numberPanel);
numberPanel = new NumberPanel();
frame.getContentPane().add(numberPanel, BorderLayout.CENTER);
frame.pack();
EDIT
As leonidas mentioned it is also possible to revalidate the frame. This requires only to replace the upper call to pack by theese.
frame.invalidate();
frame.validate();

I wanted to ask about java program

I am a java programmer and i want to ask that why is my program not working:
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Cool extends JFrame{
public void AL(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500 , 200);
frame.setVisible(true);
frame.setTitle("Java");
JTextArea textarea = new JTextArea();
textarea.setEditable(false);
textarea.setLineWrap(true);
JTextField field = new JTextField();
field.setText("Hi! This is the text!");
}
}
this is my main class:
#SuppressWarnings("serial")
public class Main {
public static void main(String[] args){
Cool dude = new Cool();
dude.AL();
}
}
It does not display anything just a blank JFrame.It does give the title but nothing else it has everything.
You have to add the JTextArea and the JTextField to the JFrame.
I don't see you adding the widgets to the JFrame, so why should they appear?
frame.add(textarea);
frame.add(field);
You are not adding the JTextArea nor the JTextField to the JFrame. You can do it through the method add which is inherited from Container. According to Java Docs:
Appends the specified component to the end of this container.
Code:
frame.add(textarea);
frame.add(field);
I know there are a good half-dozen answers with the same answer: you should add the elements to your frame. But I also wanted to add that you usually want to encapsulate your JTextArea inside a JScrollPane so that you can write more text that will be shown using scroll bars.
You should also make sure that you're adding your elements to a content pane with a layout manager so that the elements will get arranged in the way that you expect. Take a look at A Visual Guide to Layout Managers to familiarize yourself with the available layout managers. An additional, popular layout manager is MigLayout although it requires an external Jar file.
Below is a simple example with my suggestions:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Cool extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cool frame = new Cool();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Cool() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
textField = new JTextField();
contentPane.add(textField, BorderLayout.NORTH);
textField.setColumns(30);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea txtrImAText = new JTextArea();
txtrImAText.setRows(10);
txtrImAText.setColumns(30);
scrollPane.setViewportView(txtrImAText);
pack();
}
}
You forgot to add the elements to the frame:
frame.add(textarea );
frame.add(field);
Add thise lines and you will see a result.
I haven't used swing, but it seems that you have to add textarea and field to your JFrame frame.
Looking on the net i have seen this example:
frame.getContentPane().add(textarea);
You have to add text area and text field to the JFrame in AL method
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500 , 200);
frame.setVisible(true);
frame.setTitle("Java");
JTextArea textarea = new JTextArea();
textarea.setEditable(false);
textarea.setLineWrap(true);
JTextField field = new JTextField();
field.setText("Hi! This is the text!");
frame.add(textarea); // <-- here
frame.add(field);

Java Swing : JSplitPane split two component equals size at start up

I'm using JSplitPane includes two JScrollPane at each side. I don't know how to make them at equals size at start up. Here is my main code:
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
inputTextArea = new JTextArea();
outputTextArea = new JTextArea();
// put two TextArea to JScrollPane so text can be scrolled when too long
JScrollPane scrollPanelLeft = new JScrollPane(inputTextArea);
JScrollPane scrollPanelRight = new JScrollPane(outputTextArea);
// put two JScrollPane into SplitPane
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
scrollPanelLeft, scrollPanelRight);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(650); // still no effect
contentPane.add(splitPane, BorderLayout.CENTER);
I have used splitPane.setDividerLocation(getWidth() / 2); but still no effect.
Please tel me how to fix this.
For more detail. Here is my full code:
package com.view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
//import com.controller.Controller;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public JTextArea inputTextArea;
public JTextArea outputTextArea;
private JButton inputBtn;
private JButton outputBtn;
private JButton sortBtn;
public JRadioButton firstButton;
public JRadioButton secondButton;
public JRadioButton thirdButton;
JSplitPane splitPane;
//Controller controller;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
// controller = new Controller(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
/**
* center
* include two TextArea for display text
*/
inputTextArea = new JTextArea();
outputTextArea = new JTextArea();
// put two TextArea to JScrollPane so text can be scrolled when too long
JScrollPane scrollPanelLeft = new JScrollPane(inputTextArea);
JScrollPane scrollPanelRight = new JScrollPane(outputTextArea);
// put two JScrollPane into SplitPane
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
scrollPanelLeft, scrollPanelRight);
splitPane.setOneTouchExpandable(true);
contentPane.add(splitPane, BorderLayout.CENTER);
/**
* Top
* Include two button : SelectFile and WriteToFile
* this layout includes some tricky thing to done work
*/
// create new input button
inputBtn = new JButton("Select File");
// declare action. when user click. will call Controller.readFile() method
// (see this method for detail)
inputBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// controller.readFile();
}
});
// create new output button
outputBtn = new JButton("Write To File");
// declare action. when user click. will call Controller.writeFile() method
// (see this method for detail)
outputBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// controller.writeFile();
}
});
// put each button into seperate panel
JPanel tmpPanel1 = new JPanel();
tmpPanel1.add(inputBtn);
JPanel tmpPanel2 = new JPanel();
tmpPanel2.add(outputBtn);
// finnally. put those two pane into TopPane
// TopPane is GridLayout
// by using this. we can sure that both two button always at center of screen like Demo
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1, 2));
topPanel.add(tmpPanel1);
topPanel.add(tmpPanel2);
contentPane.add(topPanel, BorderLayout.NORTH);
/**
* Bottom panel
* Include all radionbutton and sortbutton
*/
// Group the radio buttons.
firstButton = new JRadioButton("Last Name");
secondButton = new JRadioButton("Yards");
thirdButton = new JRadioButton("Rating");
// add those button into a group
// so . ONLY ONE button at one time can be clicked
ButtonGroup group = new ButtonGroup();
group.add(firstButton);
group.add(secondButton);
group.add(thirdButton);
// create sor button
sortBtn = new JButton("Sort QB Stats");
// add action for this button : will Call Controller.SortFile()
sortBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// controller.sortFile();
}
});
// add all to bottomPanel
JPanel bottomPanel = new JPanel(new FlowLayout());
bottomPanel.add(firstButton);
bottomPanel.add(secondButton);
bottomPanel.add(thirdButton);
bottomPanel.add(sortBtn);
contentPane.add(bottomPanel, BorderLayout.SOUTH);
setContentPane(contentPane);
setTitle("2013 College Quarterback Statistics");
setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
setVisible(true);
System.out.println("getwidth: " + getWidth());
splitPane.setDividerLocation(getWidth()/2);
}
}
Thanks :)
I got it right for you. I add this;
contentPane.add(splitPane, BorderLayout.CENTER);
splitPane.setResizeWeight(0.5); <------- here :)
And I got rid of the setDviderLocation() at the bottom
Inititally sets the resize wieght property. values are 0.0 to 1.0, a double value percentage to split the pane. There's a whole lot to exaplain about preferred sizes and such that I read about in the JSplitPane tutorial, so you can check it out for yourself.
It really depends on the exact behaviour you want for the split pane.
You can use:
splitPane.setResizeWeight(0.5f);
when you create the split pane. This affects how the space is allocated to each component when the split pane is resized. So at start up it will be 50/50. As the split pane increased in size the extra space will also be split 50/50;
splitPane.setDividerLocation(.5f);
This will only give an initial split of 50/50. As the split pane size is increased, the extra space will all go to the last component. Also, note that this method must be invoked AFTER the frame has been packed or made visible. You can wrap this statement in a SwingUtilities.invokeLater() to make sure the code is added to the end of the EDT.
Initially getWidth() size is 0. Add splitPane.setDividerLocation(getWidth()/2); after setvisible(true). Try,
// put two JScrollPane into SplitPane
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
scrollPanelLeft, scrollPanelRight);
splitPane.setOneTouchExpandable(true);
// still no effect
add(splitPane, BorderLayout.CENTER);
setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
setVisible(true);// JFrame setVisible
splitPane.setDividerLocation(getWidth()/2); //Use setDividerLocation here.

Categories

Resources