Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem ze scroll -em w Eclipse

0 views
Skip to first unread message

adam....@gmail.com

unread,
Mar 30, 2006, 5:37:30 AM3/30/06
to
Witam

Dopiero zaczynam programować w Javie i zapewne dla Was jest to banalne
i proste ale ja juz siedze ładnych kilka godzin i probuje podpiąc
scroll do JTextArea. Znalazłem przykłady z JScrollBar -em i
JScrollPane ale nie umiem tego i tak odpalić. Proszę pomóżcie...
Poniżej zamieszczam fragment kodu:

private JTextArea getJTextArea_msg() {
if (jTextArea_msg == null) {
jTextArea_msg = new JTextArea();
jTextArea_msg.setLineWrap(true);
//JScrollPane scroll = new JScrollPane(jTextArea_msg);
//scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//jContentPane.add(scroll);

jTextArea_msg.setBounds(new java.awt.Rectangle(137,53,378,211));
jTextArea_msg.setEditable(false);

}
return jTextArea_msg;
}


private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel_msg = new JLabel();
jLabel_msg.setBounds(new java.awt.Rectangle(2, 60, 84, 15));
jLabel_msg.setText("komunikaty:");
jLabel_IP = new JLabel();
jLabel_IP.setBounds(new java.awt.Rectangle(3, 17, 131, 14));
jLabel_IP.setText("IP lub nazwa serwera:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getWybierz_klase(), null);
jContentPane.add(getJButton_OK(), null);
jContentPane.add(jLabel_IP, null);
jContentPane.add(getJTextField_IP(), null);
jContentPane.add(getJTextArea_msg(), null);
jContentPane.add(jLabel_msg, null);
jContentPane.add(getJButton_exit(), null);


}
return jContentPane;
}

Tak wygladaja moje klasy tworzące pole tekstowie i panel, ale nie wiem
jak dodać tego scrolla :(

Marek Puchalski

unread,
Mar 30, 2006, 6:52:46 AM3/30/06
to

setBounds powinieneś ustawiać na scroll pane, nie na text area.

Coś takiego działa:

public class MyFrame extends JFrame
{
public MyFrame()
{
setSize( 200, 200 );
setLayout( null );
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane( ta );
sp.setBounds( 10, 10, 100, 100 );
add( sp, BorderLayout.CENTER );
}

public static void main( String[] args )
{
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
}
}

Poza tym używanie absolute layout to nie jest dobry pomysł.

Pozdrawiam

Marek

--
# You can't run away. Everyone's connected.
# Marek Puchalski
# Proud linux user: 409592

Marek Puchalski

unread,
Mar 30, 2006, 6:56:46 AM3/30/06
to

Ten BorederLayout się przez przypadek ostał... Zignoruj to. :)

Krzysztof Wolny

unread,
Mar 30, 2006, 6:26:40 AM3/30/06
to
adam....@gmail.com napisał(a):

> private JPanel getJContentPane() {
> if (jContentPane == null) {
> jLabel_msg = new JLabel();
> jLabel_msg.setBounds(new java.awt.Rectangle(2, 60, 84, 15));
> jLabel_msg.setText("komunikaty:");
> jLabel_IP = new JLabel();
> jLabel_IP.setBounds(new java.awt.Rectangle(3, 17, 131, 14));
> jLabel_IP.setText("IP lub nazwa serwera:");
> jContentPane = new JPanel();
> jContentPane.setLayout(null);
> jContentPane.add(getWybierz_klase(), null);
> jContentPane.add(getJButton_OK(), null);
> jContentPane.add(jLabel_IP, null);
> jContentPane.add(getJTextField_IP(), null);

> jContentPane.add(getJTextArea_msg(), null);

dodajesz do contentPane textArea zamiast scrollPane'a ^^^
w jvaie scrolli sie nie 'podpina' do komponentu, ale scroll 'opakowuje'
komponent i uzywasz potem juz calego scrolla :)

> jContentPane.add(jLabel_msg, null);
> jContentPane.add(getJButton_exit(), null);
>


--
------------------------------------
Krzysztof Wolny

adam....@gmail.com

unread,
Mar 30, 2006, 6:43:02 AM3/30/06
to
wstawilem bez tego BorderLayout-a ale kompilator wywalil mi taki
blad:

Exception in thread "main" java.lang.Error: Do not use
client.sender_GUI.setLayout() use
client.sender_GUI.getContentPane().setLayout() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:479)
at javax.swing.JFrame.setLayout(JFrame.java:545)
at client.sender_GUI.<init>(sender_GUI.java:200)
at client.sender_GUI.main(sender_GUI.java:187)

adam....@gmail.com

unread,
Mar 30, 2006, 6:57:59 AM3/30/06
to
OK. Fajnie, ze sie to opakowuje... tylko, ze nie mam pojecia jak to
zrobic :/

package client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import common.Logger;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class sender_GUI extends JFrame implements Logger {

// private static final long serialVersionUID = 5798668869131098019L;

private JPanel jContentPane = null;

private JComboBox wybierz_klase = null;

private JButton jButton_OK = null;

private JLabel jLabel_IP = null;

private JTextField jTextField_IP = null;

private JTextArea jTextArea_msg = null;

private JLabel jLabel_msg = null;

private JButton jButton_exit = null;

/**
* This method initializes wybierz_klase
*
* @return javax.swing.JComboBox
*/
private JComboBox getWybierz_klase() {
if (wybierz_klase == null) {
wybierz_klase = new JComboBox();
wybierz_klase.setBounds(new java.awt.Rectangle(135,270,195,26));
wybierz_klase.addItem("Wyslij wiadomosc");
wybierz_klase.addItem("Wyslij plik");
wybierz_klase.addItem("Wyslij wykres");

}
return wybierz_klase;
}

/**
* This method initializes jButton_OK
*
* @return javax.swing.JButton
*/
private JButton getJButton_OK() {
if (jButton_OK == null) {
jButton_OK = new JButton();
jButton_OK.setBounds(new java.awt.Rectangle(343,270,78,26));
jButton_OK.setText("OK");

jButton_OK.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
createSender();
}

});

}
return jButton_OK;

}

private void createSender() {
if (wybierz_klase.getSelectedIndex() == 0) {
try {
new Sender(this, "transferables.Message", jTextField_IP
.getText()).run();

} catch (Exception ee) {
ee.printStackTrace();
// String msg_win=ee.toString();
// String msg_win = (String) ee.printStackTrace();
logMsg("blad przu uruchamianiu klasy do wysylania wiadomosci");

}
} else if (wybierz_klase.getSelectedIndex() == 1) {
try {
new Sender(this, "transferables.FileTransfer", jTextField_IP
.getText()).run();
} catch (Exception ee) {
ee.printStackTrace();
logMsg("blad przu uruchamianiu klasy do wysylania plików");

}

} else if (wybierz_klase.getSelectedIndex() == 2) {
try {
new Sender(this, "transferables.Graph", jTextField_IP.getText())
.run();
} catch (Exception ee) {
ee.printStackTrace();
logMsg("blad przu uruchamianiu klasy do wysylania wykresów");
}
}
}

/**
* This method initializes jTextField_IP
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField_IP() {
if (jTextField_IP == null) {
jTextField_IP = new JTextField();
jTextField_IP.setBounds(new java.awt.Rectangle(138,14,378,20));
jTextField_IP.setText("127.0.0.1");
}
return jTextField_IP;
}

/**
* This method initializes jTextArea_msg
*
* @return javax.swing.JTextArea
*/
/*public void scroll(){
JScrollPane scroll = new JScrollPane(jTextArea_msg);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

}*/

private JTextArea getJTextArea_msg() {
if (jTextArea_msg == null) {
jTextArea_msg = new JTextArea();
jTextArea_msg.setLineWrap(true);
//JScrollPane scroll = new JScrollPane(jTextArea_msg);
//scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//jContentPane.add(scroll);

jTextArea_msg.setBounds(new java.awt.Rectangle(137,53,378,211));
jTextArea_msg.setEditable(false);

}
return jTextArea_msg;
}

/**
* This method initializes jButton_exit
*
* @return javax.swing.JButton
*/
private JButton getJButton_exit() {
if (jButton_exit == null) {
jButton_exit = new JButton();
jButton_exit.setBounds(new java.awt.Rectangle(436,270,80,27));
jButton_exit.setText("Exit");
jButton_exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return jButton_exit;
}

/**
* @param args
*/
public static void main(String[] args) {
sender_GUI frame = new sender_GUI();


frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );

}

/**
* This is the default constructor
*/
public sender_GUI() {
super();
initialize();


setSize( 200, 200 );
setLayout( null );
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane( ta );
sp.setBounds( 10, 10, 100, 100 );


}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(535, 329);
this.setContentPane(getJContentPane());
this.setTitle("UDP - klient");
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/


private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel_msg = new JLabel();
jLabel_msg.setBounds(new java.awt.Rectangle(2, 60, 84, 15));
jLabel_msg.setText("komunikaty:");
jLabel_IP = new JLabel();
jLabel_IP.setBounds(new java.awt.Rectangle(3, 17, 131, 14));
jLabel_IP.setText("IP lub nazwa serwera:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getWybierz_klase(), null);
jContentPane.add(getJButton_OK(), null);
jContentPane.add(jLabel_IP, null);
jContentPane.add(getJTextField_IP(), null);
jContentPane.add(getJTextArea_msg(), null);

jContentPane.add(jLabel_msg, null);
jContentPane.add(getJButton_exit(), null);


}
return jContentPane;
}

public void logMsg(String message) {
System.out.println(message);
jTextArea_msg.append(message+"\n");

}

} // @jve:decl-index=0:visual-constraint="10,10"

Tak wyglada mój kod i niestety nie mam juz pomyslu jak to
"opakowac" :(

Marek Puchalski

unread,
Mar 30, 2006, 8:05:55 AM3/30/06
to

No tak... chodzę na skróty.

Zrób tak, jak napisało. Czyli zamiast setLayout( null ), daj
getContentPane().setLayout( null )

adam....@gmail.com

unread,
Mar 30, 2006, 7:09:40 AM3/30/06
to
no dzieki - teraz sie skompilowalo, ale niestety scrolla jak nie
bylo tak nie ma :(. Cos jeszcze zle robie :(
Wstawilem tu ponizej caly kod tej klasy. Moze zerkniesz ?
Dzieki


Pozdrawiam
Adam

Marek Puchalski

unread,
Mar 30, 2006, 8:22:22 AM3/30/06
to

Miałbym trochę zastrzeżeń do tego kodu, ale aby uruchomić to, na czym Ci
zależy, wystarczy zmodyfikować jedną metodę.

private JScrollPane getJTextArea_msg() {


jTextArea_msg = new JTextArea();
jTextArea_msg.setLineWrap(true);

JScrollPane scroll = new JScrollPane(jTextArea_msg);

scroll.setBounds(new java.awt.Rectangle(137,53,378,211));
jContentPane.add(scroll);
jTextArea_msg.setEditable(false);

return scroll;
}

Rzuć okiem co tu się dzieje.

adam....@gmail.com

unread,
Mar 30, 2006, 7:29:15 AM3/30/06
to
Dziala dzieki za pomoc. Co do kodu to napewno mozna by to zrobic
inaczej i lepiej, ale sie dopiero ucze Javy i mam nadzieje, ze z
czasem bedzie lepiej :). Jeszcze raz DZIEKI.

Pozdrawiam
Adam

Krzysztof Wolny

unread,
Mar 30, 2006, 7:32:00 AM3/30/06
to
adam....@gmail.com napisał(a):

> OK. Fajnie, ze sie to opakowuje... tylko, ze nie mam pojecia jak to
> zrobic :/

> Tak wyglada mój kod i niestety nie mam juz pomyslu jak to
> "opakowac" :(

wybacz ale chyba nie wykazujesz checi zrobienia tego tylko chcesz dostac
gotowy kod. napisalem ze blad jest tu:

jContentPane.add(getJTextArea_msg(), null);

bo wrzucasz *pole tekstowe* zamiast wrzucac *scrollpane z polem tektowym*

nie wiem co za edytor/kreatorz generuje taki dziwnawy kod, ale razcej nie
piszesz tego sam. moze zobacz wpierw w tym edytorze/kreatorze czy nie ma
mozliwosc dodania tego z jego poziomu. jesli nie ma to zrob mniej wiecej tak:

- dorob pole JScrollPane w klasie sender_GUI
- dorob do niego metode dostepowa
- zmien wywolanie jContentPane.add(getJTextArea_msg(), null);
na jContentPane.add(getJScrollPane_msg(), null);


--
------------------------------------
Krzysztof Wolny

0 new messages