Add a scroll bar to text area - java

I use Eclipse Window Builder. When I click the button, something will be written on the screen. But since my prints are long, I want to use a scroll pane.
public class uyg2 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
uyg2 frame = new uyg2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public uyg2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(32, 29, 89, 23);
contentPane.add(btnNewButton);
JTextArea textArea = new JTextArea();
textArea.setBounds(10, 63, 233, 173);
contentPane.add(textArea);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setBounds(249, 10, 173, 118);
contentPane.add(scrollPane);
}

So, based on...
public class uyg1 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
uyg1 frame = new uyg1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public uyg1() {
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));
setContentPane(contentPane);
JTextArea textArea = new JTextArea("Test");
textArea.setSize(400, 400);
JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scroll);
frame.setVisible(true);
}
}
textArea.setSize(400, 400); is irrelevant as the layout manager will deal with it. You can provide sizing hints via the JTextArea(String, int, int) constructer, but remember, this is the number of characters in width/height, not pixels.
The following are giving you issues...
frame.getContentPane().add(scroll);
frame.setVisible(true);
because frame is undefined. Since the class extends from JFrame, they are pointless, it should just be
getContentPane().add(scroll);
setVisible(true);
but, I'd add...
pack();
setLocationRelativeTo(null);
before it, as it will give you a generally better experience

You need to add the TextArea to the ScrollPane. Don't add the textarea tho the contentpane.

Related

how to change the content of a panel using another panel from another classe in java

On Eclipse, using JFrames, I'm using a mainFrame as the main user interface and in that frame i got a contentPanel (JPanel) containing a small panel (JPanel as well), under that panel there's a buttong, and i have another class which i named 'clients' (another frame) containting a contentPanel too (JPanel) with other compenents, i want to get the the second class contentPanel to show in the first's panel (the small panel i mentionned).
here's what i did, but it's not working!! any help?
MainF.java
public class mainF extends JFrame {
private JPanel contentPane;
public JPanel panel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainF frame = new mainF();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public mainF() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 404);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
panel = new JPanel();
panel.setBounds(10, 11, 414, 256);
contentPane.add(panel);
panel.setLayout(null);
JLabel Panel = new JLabel("Main Panel");
Panel.setHorizontalAlignment(SwingConstants.CENTER);
Panel.setBounds(10, 11, 69, 33);
panel.add(Panel);
JButton btnClients = new JButton("Clients");
btnClients.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel = new Clients().getContentPane();
panel.setVisible(true);
}
});
btnClients.setBounds(162, 296, 89, 23);
contentPane.add(btnClients);
}
}
Clients
public class Clients extends JFrame {
private JPanel contentPane;
public JPanel getContentPane() {
return contentPane;
}
public Clients() {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblClients = new JLabel("Clients");
lblClients.setHorizontalAlignment(SwingConstants.CENTER);
lblClients.setFont(new Font("Times New Roman", Font.BOLD, 18));
lblClients.setBounds(10, 11, 85, 37);
contentPane.add(lblClients);
}
}
Thanks Everyone!!
here's an image to explain the situation:
See the image here
You are using one JPanel reference panel, and using it to create 2 different panel objects. So first remove the panel from contentPane then add it back with new initialization.
public void actionPerformed(ActionEvent e) {
contentPane.remove(panel);
panel = new Clients().getContentPane();
panel.setVisible(true);
contentPane.add(panel);
contentPane.repaint();
contentPane.revalidate();
}
});
Also you require contentpane to revalidate() and repaint()

Java Swing resize panel inside contentPanel

I have the following structure in a frame:
Frame->contentPane panel->topPanel panel->table
This is the code:
private JPanel contentPane;
private JTable table;
private JPanel topPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShowList frame = new ShowList();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ShowList() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 675, 433);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
topPanel = new JPanel();
topPanel.setBounds(76, 76, 491, 245);
contentPane.add(topPanel);
topPanel.setLayout(null);
table = new JTable();
table.setBounds(0, 0, 491, 245);
topPanel.add(table);
}
What I want to achieve is that when I resize the frame making the window bigger, the topPanel and the table that it is contained by this one resize also and do not stay the same size as they were before resizing the frame. I have read about Layouts but I can not make it work. Any suggestion?
Use a LayoutManager to control the size and position of the components inside of your frame. Avoid setting the layout manager of your panels as null.
I recommend you to take a look at this link: "A Visual Guide to Layout Managers" to learn more about the different kinds of layouts you can use.
In your code, for example, I would use a BorderLayout:
contentPane.setLayout(new BorderLayout());
...
contentPane.add(topPanel, BorderLayout.CENTER);
topPanel.setLayout(new BorderLayout());
...
topPanel.add(table, BorderLayout.Center);
By the way, I suppose your class ShowList extends JFrame because methods like setDefaultCloseOperation gives you an error if not, right?
I hope it helps.

Adding a JTable to JFrame

I want to add test2 to my DFrametest class, so that the table of test2 shows up in my window. I get an empty window and can't find the mistake.
public class test2 extends JPanel {
JTable tbl;
DefaultTableModel dt;
public test2(){
JLabel label = new JLabel("Course Lookup GUI");
this.add( label );
tbl = new JTable();
dt = new DefaultTableModel();
dt.addColumn("ID");
dt.addColumn("Name");
tbl.setModel(dt);
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/lagerverwaltungwin", "root", "");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT ArtNr, Beschreibung FROM artikel");
}catch(Exception e){
e.printStackTrace();
}
JScrollPane jp = new JScrollPane();
jp.getViewport().add(tbl);
add(jp);
}
}
This is my Frameclass which should have the table from test2:
public class DFrametest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DFrametest frame = new DFrametest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public DFrametest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
test2 t = new test2();
this.add(t);
this.setVisible(true);
}
}
Never Use null Layout. Also donot use setBounds(100, 100, 450, 300);
use pack();
Change your DFrametest class like this
contentPane.setLayout(new BorderLayout());
contentPane.add(t, BorderLayout.CENTER);
Full Code of DFrametest class:
public class DFrametest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DFrametest frame = new DFrametest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public DFrametest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
test2 t = new test2();
contentPane.setLayout(new BorderLayout());
contentPane.add(t, BorderLayout.CENTER);
pack();
}
}
Try to use setBounds() method since you haven't mentioned any layout (setLayout is null).
Add setBounds method before adding the jpanel to the jframe
setBounds(x-axis,y-axis,width,height);
EX:-
setBounds(10,20,200,400);
Note:

Adding vertical spacing to NORTH component in BorderLayout

I have a JFrame with a BorderLayout. I added a JPanel to the NORTH side of the JFrame. In this panel I want to add components to it in an absolute positioning. In the Center side of the JFrame I added another JPanel which should take a huge space. However when I run the application I see nothing from the North JPanel as the Center JPanel occupied all the space of the JFrame! How can I give vertical space to the North JPanel?
I really need to used absolute positioning for the north JPanel.
Here's my code:
public class AAAA extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AAAA frame = new AAAA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AAAA() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1136, 520);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(0, 0, 117, 29);
panel.add(btnNewButton);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);
}
}
Update 1
I see you have already selected an answer (prematurely, I think). Here is the first iteration of what I believe you are trying to achieve. Without need for setting bounds or preferred sizes..
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class AAAA extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AAAA frame = new AAAA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AAAA() {
super("Laid Out");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// OMG! If you can make a GUI break at 1336 px wide, it should be
// possible to make it break at ..much smaller!
//setBounds(100, 100, 1136, 520);
setBackground(Color.YELLOW);
contentPane = new JPanel();
contentPane.setBackground(Color.BLUE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
// make it a FlowLayout as FlowLayout.LEADING with no spacing to
// make the button snug up against the top left
JPanel panel = new JPanel(
new FlowLayout(FlowLayout.LEADING, 0, 0));
panel.setBackground(Color.GREEN);
contentPane.add(panel, BorderLayout.NORTH);
//panel.setPreferredSize(new Dimension(1024,400));
JButton btnNewButton = new JButton("New button");
// we change the margin to make the button bigger than natural size.
btnNewButton.setMargin(new Insets(6, 22, 6, 22));
panel.add(btnNewButton);
JPanel panel_1 = new JPanel();
// create a solic color image to both pad the GUI and
// provide visual indication of where it is.
BufferedImage bi = new BufferedImage(
400,200,BufferedImage.TYPE_INT_RGB);
JLabel padder = new JLabel(new ImageIcon(bi));
panel_1.add(padder);
panel_1.setBackground(Color.RED);
contentPane.add(panel_1, BorderLayout.CENTER);
pack();
setMinimumSize(getSize());
}
}
I really need to used absolute positioning for the north JPanel.
Why? If we know why you think you need to do this we can probably offer a better approach.
Don't use a null layout. Swing was designed to be used with layout managers.
The BorderLayout will respect the preferred height of the component added to the NORTH. The preferred height is zero so nothing is displayed.
Note: I am not suggesting that you set the preferred height of the panel, that is the job of the layout manager and that is why you should always use a layout manager. Layout managers do more than just set the size/location of a component.

SWT widget into Swing

I have a simple JFrame and a JPanel inside it.
public class TestPanel extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestPanel frame = new TestPanel();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestPanel() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
}
}
And I have an SWT widget (Nebula Gantt Chart). -->
http://hexapixel.com/software/ganttwidget
I would like to add this SWT widget to the JPanel, actually embed it.
What are the possible ways to do it? Thank you for help! :-)

Categories

Resources